Last active
June 11, 2021 06:13
-
-
Save AlexisTM/2754f6b373bf40b544408a631abeb5ba to your computer and use it in GitHub Desktop.
C/C++ performance magic functions (prevent optimization)
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
/** | |
* Those functions are related to: https://www.youtube.com/watch?v=nXaxk27zwlk&t=2446s | |
* | |
* They are used to allow to benchmark functions with -O3 that would otherwise be removed because it is unused. | |
* | |
* escape(&object) prevents object to be optimized out | |
* "This ASM code has some unknowable side effects and possibly stored the pointer globally" | |
* clobber() tells the compiler some asm might be reading the whole memory | |
* "This ASM code could probably read/write to all memory" => observe all memory | |
*/ | |
void escape(void* p) { | |
asm volatile("" : : "g"(p) : "memory"); | |
} | |
void clobber() { | |
asm volatile("" : : : "memory"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: