-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Reduce startup time. #1288
Reduce startup time. #1288
Conversation
I think @Adlai-Holler mentioned that dispatch_once is faster than function local static because the latter uses a global mutex for all statics. Maybe switch to dispatch_once to get the tiny perf gain? |
Make global static a function local static. This stops it from being initialized premain and affecting startup time.
I would be interested to get some benchmarks numbers around this area. @dmaclach Did you profile by any chance? Looking at http://www.modernescpp.com/index.php/thread-safe-initialization-of-a-singleton it seems static vars with block scope is faster as using |
I looked a bit more into benchmarking C++ static variables with block scope and dispatch_once initialization. Here is a small benchmark script and the results running it three times at the end. Pretty interesting based on the results we probably should go with
Runs:1. Run:
2. Run:
3. Run:
|
Also interesting moving this to multithreading access via
1. Run
2. Run:
|
Thanks folks. Moved over to dispatch_once. |
Very interesting numbers, thanks for sharing @maicki. |
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.
LGTM now. It's great that we can reclaim some pre-main time. Thank you, @dmaclach!
FWIW https://opensource.apple.com/source/libcppabi/libcppabi-26/src/cxa_guard.cxx.auto.html Is how the static initialization is done. There does appear to be at large recursive lock. |
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.
@dmaclach Thanks for improving!
cool stuff! |
@maicki can you merge? I do not have privs. |
Make global static a function local static. This stops it from being initialized premain and affecting startup time. (cherry picked from commit 7cddc2b)
Make global static a function local static. This stops it from being initialized premain and affecting startup time.