Mark RECURSIVE_COUNT_* as const.#227
Merged
fitzgen merged 1 commit intorust-fuzz:mainfrom Aug 7, 2025
Merged
Conversation
From the `thread_local` docs:
> This macro supports a special `const {}` syntax that can be used when
> the initialization expression can be evaluated as a constant. This can
> enable a more efficient thread local implementation that can avoid
> lazy initialization. For types that do not need to be dropped, this
> can enable an even more efficient implementation that does not need to
> track any additional state.
The `RECURSIVE_COUNT_*` values currently don't use this, but they can.
This changes the generated code from this:
```
#[allow(non_upper_case_globals)]
const RECURSIVE_COUNT_Example: ::std::thread::LocalKey<::core::cell::Cell<u32>> = {
#[inline]
fn __init() -> ::core::cell::Cell<u32> {
::core::cell::Cell::new(0)
}
unsafe {
::std::thread::LocalKey::new(const {
if ::std::mem::needs_drop::<::core::cell::Cell<u32>>() {
|init| {
#[thread_local]
static VAL: ::std::thread::local_impl::LazyStorage<
::core::cell::Cell<u32>,
(),
> = ::std::thread::local_impl::LazyStorage::new();
VAL.get_or_init(init, __init)
}
} else {
|init| {
#[thread_local]
static VAL: ::std::thread::local_impl::LazyStorage<
::core::cell::Cell<u32>,
!,
> = ::std::thread::local_impl::LazyStorage::new();
VAL.get_or_init(init, __init)
}
}
})
}
};
```
to this:
```
#[allow(non_upper_case_globals)]
const RECURSIVE_COUNT_Example: ::std::thread::LocalKey<::core::cell::Cell<u32>> = {
const __INIT: ::core::cell::Cell<u32> = { ::core::cell::Cell::new(0) };
unsafe {
::std::thread::LocalKey::new(const {
if ::std::mem::needs_drop::<::core::cell::Cell<u32>>() {
|_| {
#[thread_local]
static VAL: ::std::thread::local_impl::EagerStorage<
::core::cell::Cell<u32>,
> = ::std::thread::local_impl::EagerStorage::new(__INIT);
VAL.get()
}
} else {
|_| {
#[thread_local]
static VAL: ::core::cell::Cell<u32> = __INIT;
&VAL
}
}
})
}
};
```
Note in both cases the `else` block is the relevant one because
`Cell<u32>` doesn't need drop.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
From the
thread_localdocs:The
RECURSIVE_COUNT_*values currently don't use this, but they can. This changes the generated code from this:to this:
Note in both cases the
elseblock is the relevant one becauseCell<u32>doesn't need drop.