forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent type conversion in Differentiator
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, facebook#16 // =16 str w0, [sp, facebook#12] ldr w8, [sp, facebook#12] add w9, w8, #1 // =1 mov w8, w9 sxtw x0, w8 add sp, sp, facebook#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, facebook#16 // =16 str x0, [sp, #8] ldr x8, [sp, #8] add x0, x8, #1 // =1 add sp, sp, facebook#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
- Loading branch information
1 parent
b70152c
commit 81a97de
Showing
2 changed files
with
25 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters