Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from facebook:master #16

Merged
merged 2 commits into from
Jun 11, 2020
Merged

Conversation

pull[bot]
Copy link

@pull pull bot commented Jun 11, 2020

See Commits and Changes for more details.


Created by pull[bot]. Want to support this open source service? Please star it : )

cpojer added 2 commits June 11, 2020 07:23
Summary: Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D21995098

fbshipit-source-id: b387bd45b44fe93743d34d2a3435c165d15f054f
Summary: Changelog: [Internal]

Reviewed By: makovkastar

Differential Revision: D21996455

fbshipit-source-id: 8fc339f987957cf58b6ff56c1b4d28f8725d70c9
@pull pull bot added the ⤵️ pull label Jun 11, 2020
@pull pull bot merged commit b4c1392 into MLH-Fellowship:master Jun 11, 2020
pull bot pushed a commit that referenced this pull request Oct 16, 2020
Summary:
changelog: [internal]

Prevents 2 type converions:
1. int <-> size_t
2. int <-> int32_t

# Why is using size_t better when working with indexes.

## 1. Type conversion isn't for free.

Take this example

```
size_t calculate(int number) {
  return number + 1;
}
```

It generates following assembly (generated with armv8-a clang 10.0.0):

```
calculate(int):                          // calculate(int)
sub     sp, sp, #16                     // =16
str     w0, [sp, #12]
ldr     w8, [sp, #12]
add     w9, w8, #1                      // =1
mov     w8, w9
sxtw    x0, w8
add     sp, sp, #16                     // =16
ret
```

That's 9 instructions.

If we get rid of type conversion:

```
size_t calculate(size_t number) {
  return number + 1;
}
```

Assembly (generated with armv8-a clang 10.0.0):

```
calculate(unsigned long):                          // calculate(unsigned long)
sub     sp, sp, #16             // =16
str     x0, [sp, #8]
ldr     x8, [sp, #8]
add     x0, x8, #1              // =1
add     sp, sp, #16             // =16
ret
```

Compiler now produces only 7 instructions.

## Semantics

When using int for indexing, the type doesn't say much. By using `size_t`, just by looking at the type, it gives the reader more information about where it is coming from.

Reviewed By: JoshuaGross

Differential Revision: D24332248

fbshipit-source-id: 87ef982829ec14906ed9e002ea2e875fda4a0cd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant