Shrink derived code size with with_recursive_count.#229
Merged
fitzgen merged 1 commit intorust-fuzz:mainfrom Aug 13, 2025
Merged
Shrink derived code size with with_recursive_count.#229fitzgen merged 1 commit intorust-fuzz:mainfrom
with_recursive_count.#229fitzgen merged 1 commit intorust-fuzz:mainfrom
Conversation
Contributor
Author
|
An example: for fn arbitrary(
u: &mut arbitrary::Unstructured<'arbitrary>,
) -> arbitrary::Result<Self> {
let guard_against_recursion = u.is_empty();
if guard_against_recursion {
RECURSIVE_COUNT_S
.with(|count| {
if count.get() > 0 {
return Err(arbitrary::Error::NotEnoughData);
}
count.set(count.get() + 1);
Ok(())
})?;
}
let result = (|| {
Ok(
S(
arbitrary::Arbitrary::arbitrary(u)?,
arbitrary::Arbitrary::arbitrary(u)?,
),
)
})();
if guard_against_recursion {
RECURSIVE_COUNT_S
.with(|count| {
count.set(count.get() - 1);
});
}
result
}it now looks like this: fn arbitrary(
u: &mut arbitrary::Unstructured<'arbitrary>,
) -> arbitrary::Result<Self> {
arbitrary::details::with_recursive_count(
u,
&RECURSIVE_COUNT_S,
|mut u| {
Ok(
S(
arbitrary::Arbitrary::arbitrary(u)?,
arbitrary::Arbitrary::arbitrary(u)?,
),
)
},
)
}The story is very similar for |
The amount of code generated by `derive(Arbitrary)` is large, mostly due to the recursion count guard. This commit factors out that code with a new hidden function `with_recursive_count`. Because `with_recursive_count` is mark with `inline`, the generated code should end up being much the same. But compile times are reduced by 30-40% because rustc's frontend has much less code to chew through.
07f9c25 to
40fe82d
Compare
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.
The amount of code generated by
derive(Arbitrary)is large, mostly due to the recursion count guard. This commit factors out that code with a new hidden functionwith_recursive_count.Because
with_recursive_countis mark withinline, the generated code should end up being much the same. But compile times are reduced by 30-40% because rustc's frontend has much less code to chew through.