Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Rework initialization of constants & class variables #15333
Rework initialization of constants & class variables #15333
Changes from 8 commits
b55fc14
55228fd
9c1da89
6ebfcf4
71a1c4d
2296069
2c1c20b
273978b
e7527d3
e42a575
4f5092e
cf58312
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very nit-picky, but in my v2 branch, I replaced this trinary enum with a
Int8
so all comparisons are done with 0 (== Initialized
becomes> 0
).Comparing with
0
results in fewer (or smaller) assembly instructions on most CPUsThe only arch I'm really familiar with is risc-v, so here's an example in risc-v:
Of course this is micro-optimization, but if this is inlined into every const access, it could be noticable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: I fixed the examples below... I stupidly used a UInt8 🤦
On x86_64 the only difference is in the jump instruction:
Same for ARM32:
But AArch64 indeed only needs one instruction instead of two for the equality check:And same for AArch64:NOTE: I'm not fluent in the assembly of each arch. I compiled a tiny program with
--cross-compile --target=...
then usedobjdump --disassemble
from a crosschain build ofbinutils
to compare the LLVM generated assembly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me that looks like an unsigned comparison. So it only checks
x != 0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wrote a commit, but didn't push it (yet): I'm wondering if we'd really benefit from the change in practice 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested again, it's actually a
jle
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BlobCodes I run my tests again and updated my comment above.
We can probably do better in manually written assembly, but LLVM actually generates the same assembly for all 3 architectures. The only difference stands in the jump instruction 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see in a follow up if we can squeeze even more performance with this idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: This is pretty darn clever 👏 Kudos to @BlobCodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second that: thanks a lot @BlobCodes 🙇