-
Notifications
You must be signed in to change notification settings - Fork 50
Move static
local variable out of templated function
#299
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
Move static
local variable out of templated function
#299
Conversation
Rboolean* should_unwind_protect = | ||
reinterpret_cast<Rboolean*>(LOGICAL(should_unwind_protect_sexp)); | ||
should_unwind_protect[0] = TRUE; |
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 was previously overwriting the global option to TRUE
every time a new compilation unit needed to initialize its static local variable. I don't think we want to do that. I think we only want to initialize to TRUE
the very first time this is created, i.e. in the if branch above.
inline Rboolean* access_should_unwind_protect() { | ||
// Setup is run once per compilation unit, but all compilation units | ||
// share the same global option, so each compilation unit's static pointer | ||
// will point to the same object. | ||
static Rboolean* p_should_unwind_protect = setup_should_unwind_protect(); |
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.
The function holding the static local variable is no longer templated, so each compilation unit will only have to run setup_should_unwind_protect()
1 time
LGTM. Not strictly related to this PR's business, but should we be concerned about the option being set to something that is not a logical ? |
I can confirm the performance gain:
|
@romainfrancois we should also try looking at this comment from Jim, possibly as a (nicer?) alternative to this PR 9a62c3a#r90124412 |
* Revert #299 * Remove unwind protect global variable altogether * Add a descriptive test * Add FAQ bullets about `unwind_protect()` * Tweak internals vignette advice * NEWS bullet * Tweaks from code review
Closes #298
@jimhester would you mind reviewing this for me? You are the last one to have touched this code, and probably understand it better than anyone else. The #298 issue provides the backstory.
A rerun of the benchmark in #298 gives me:
Which better matches the performance we had in 0.4.0
I also confirmed that I can't reproduce the original issue in #244 that forced us to move away from a global static variable.
FWIW the pattern I've used here is fairly similar to what is done in
<date>
to determine the tzdb file pathhttps://github.com/HowardHinnant/date/blob/22ceabf205d8d678710a43154da5a06b701c5830/src/tz.cpp#L394-L429