Skip to content
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

use OnceLock instead of Box::leak #276

Closed
wants to merge 1 commit into from

Conversation

folkertdev
Copy link
Contributor

saves an allocation, generally a nice way to get a static reference without any scary functions

saves an allocation, generally a nice way to get a static reference without any scary functions
@codecov
Copy link

codecov bot commented Oct 5, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (3f7ced5) 70.71% compared to head (bd34f24) 70.73%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #276      +/-   ##
==========================================
+ Coverage   70.71%   70.73%   +0.01%     
==========================================
  Files          48       48              
  Lines        5505     5504       -1     
==========================================
  Hits         3893     3893              
+ Misses       1612     1611       -1     
Files Coverage Δ
statime-linux/src/main.rs 0.00% <0.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@davidv1992
Copy link
Member

Disagree with oncelock being the better option. This gives a global which is not needed here (and can produce unexpected behavior should actual_main somehow get called twice) and involves os locking primitives which shouldn't be needed with a decent allocator.

@folkertdev
Copy link
Contributor Author

This gives a global which is not needed here (and can produce unexpected behavior should actual_main somehow get called twice)

the behavior is entirely predictable? you get or initialize the static. So in this case (which should not happen, it's non-sensical) then for the second call either it's already initialized, or if the second call is concurrent to the first it will block until the first call initializes the value. That sounds fine to me? initializing it yourself is no better, really?

and involves os locking primitives which shouldn't be needed with a decent allocator.

So in the normal case, there is only one concurrent access to this value. That means that it's a lock that always succeeds. Locks are not a problem, contention is. It's a static though, so it has to be protected agains concurrent initialization.

I maintain that this is now the idiomatic way to aquire a static reference to a value. But maybe it takes LazyCell to convince

@folkertdev folkertdev closed this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants