⚡️ Faster fc.integer on generate#7035
Merged
Merged
Conversation
Compute the default shrink target once in the constructor and store it as a private readonly field, rather than recomputing it on every `shrink()` call. `shrink()` reads the field directly; the `defaultTarget()` method is removed. ALONE vs origin/main: # integer - generate default (i32 range): +10.2% # integer - generate small range (0..100): +16.0% # integer - generate signed (-100..100): +11.9% # integer - shrink default mid value: +10.3% # nat - generate default: +19.8% CUMULATIVE vs origin/main (chain state at this commit): # integer - generate default (i32 range): +55.9% # integer - shrink default mid value: +8.5% # nat - generate default: +47.1% INCREMENTAL (vs origin/main, first commit): same as ALONE. Noise floor: hot integer/nat benches show run-to-run swings up to ~±40% on this hardware; the slower `shrink` bench is stable to ~±10%. Why it works: the default shrink target depends only on `min` and `max`, which are immutable on `IntegerArbitrary`. Hoisting the conditional into the constructor turns three branches into a single field load on every `shrink()` call.
`biasNumericRange(min, max, integerLogLike)` depends only on `min`/`max`, which never change for a given `IntegerArbitrary`. Compute the list once on the first biased call and reuse it for every subsequent biased `generate()`. ALONE vs origin/main: # integer - generate default (i32 range): +16.1% # integer - generate biased default: +17.8% # integer - shrink default mid value: +28.0% (noisy) # nat - generate default: +69.3% (noisy) CUMULATIVE vs origin/main (chain state at this commit): # integer - generate default (i32 range): +70.7% # integer - generate small range (0..100): +21.0% # integer - generate biased default: +14.8% # nat - generate default: +11.7% INCREMENTAL (vs previous commit): # integer - generate biased default: ~+15-20% Other benches within noise of commit 1. Noise floor: ~±40% on hot generate benches, ~±10% on shrink. Reported ALONE numbers are best signal across multiple runs. Why it works: removes a `biasNumericRange()` call plus its allocations from the biased branch hot path, leaving a single field load and the range selection.
…teger)
Inline the unbiased fast path directly into `generate()` and call
`mrng.nextInt(this.min, this.max)` without going through
`computeGenerateRange`. The biased branch is also inlined back into
`generate()`. `computeGenerateRange` is removed.
ALONE vs origin/main:
# integer - generate default (i32 range): +48.6%
# integer - generate signed (-100..100): +30.2%
# integer - generate biased default: within noise
# nat - generate default: +12.3%
# integer - shrink default mid value: neutral
CUMULATIVE vs origin/main (chain state at this commit):
# integer - generate default (i32 range): +92.1%
# integer - generate biased default: +58.9%
# integer - generate signed (-100..100): +29.6%
# nat - generate default: +62.6%
# integer - shrink default mid value: +9.9%
INCREMENTAL (vs previous commit):
# integer - generate default (i32 range): ~+13% on top of commit 2
# integer - generate biased default: ~+38% on top of commit 2
# nat - generate default: ~+46% on top of commit 2
Noise floor: ~±40% on hot generate benches.
Why it works: drops a per-call `{ min, max }` object allocation, plus
the method call/return overhead. The unbiased path becomes a single
branch + `nextInt` + `new Value`.
…n shrink) Replace the two `function*` generators (`shrinkDecr` / `shrinkIncr`) in `shrinkInteger` with a single `ShrinkIntegerIterator` class. The class holds the loop state on the instance and exposes `next()` / `[Symbol.iterator]()` directly, eliminating the generator-frame and closure allocations done on every call. The shrink sequence is identical for any (current, target, tryTargetAsap): the direction is picked once in the constructor by storing the appropriate `halve` / `stopAtZero` callbacks, and `next()` applies them step by step. The same `toremove === realGap` overflow guard is preserved. ALONE vs origin/main: # integer - shrink default mid value: +1948.8% (x20.5) # integer - generate default (i32 range): +52.6% (likely partly noise) # nat - generate default: +64.1% (likely partly noise) CUMULATIVE vs origin/main (chain state at this commit): # integer - shrink default mid value: +2119.7% (x22.2) # integer - generate biased default: +24.7% # nat - generate default: +114.6% INCREMENTAL (vs previous commit): # integer - shrink default mid value: ~+1900% absolute jump Generate benches are within the ±40% noise of commit 3. Noise floor: ±40% on hot generate benches; the shrink result is so large it dwarfs the noise. Why it works: each call to a `function*` generator allocates a fresh generator-state object and a fresh closure capturing `current`, `target`, `tryTargetAsap`, etc. A class instance with the same state on `this` lets V8 inline `next()` and removes the suspend/resume machinery — for a sub-microsecond shrink step that overhead dominated.
🦋 Changeset detectedLatest commit: e3249fd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@fast-check/ava
fast-check
@fast-check/jest
@fast-check/packaged
@fast-check/poisoning
@fast-check/vitest
@fast-check/worker
commit: |
Contributor
⏱️ Benchmark ResultsClick to expand |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7035 +/- ##
==========================================
- Coverage 94.87% 94.86% -0.01%
==========================================
Files 212 212
Lines 5888 5884 -4
Branches 1545 1543 -2
==========================================
- Hits 5586 5582 -4
Misses 294 294
Partials 8 8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
dubzzz
enabled auto-merge (squash)
June 3, 2026 21:45
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.
Description
The suggestion come from a first proposal made by Claude. We then refined it to keep the legitimate part from it and put for later next parts of it.
Related to #7022
Checklist
— Don't delete this checklist and make sure you do the following before opening the PR
pnpm run bumpor by following the instructions from the changeset bot🐛(vitest) Something...) when the change targets a package other thanfast-check