-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Help the compiler avoid inlining lazy init functions (#443)
Before this change, the compiler generates code that looks like this: ``` if not initialized: goto init do_work: do the actual work goto exit init: inilned init() goto do_work exit: ret ``` If the initialization code is small, this works fine. But, for (bad) reasons, is_rdrand_good is particularly huge. Thus, jumping over its inined code is wasteful because it puts bad pressure on the instruction cache. With this change, the generated code looks like this: ``` if not initialized: goto init do_work: do the actual work goto exit init: call init() goto do_work exit: ret ``` I verified these claims by running: ``` $ cargo asm --rust getrandom_inner --lib --target=x86_64-fortanix-unknown-sgx ``` This is also what other implementations (e.g. OnceCell) do. While here, I made the analogous change to LazyPtr, and rewrote LazyPtr to the same form as LazyUsize. I didn't check the generated code for LazyPtr though. (Why is `is_rdrand_good` huge? The compiler unrolls the 10 iteration retry loop, and then it unrolls the 8 iteration self-test loop, so the result is `rdrand()` is inlined 80 times inside is_rdrand_good. This is something to address separately as it also affects `getrandom_inner` itself.)
- Loading branch information
1 parent
a4b1f2f
commit 8933c05
Showing
2 changed files
with
53 additions
and
32 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