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
WIP cached twister #2700
Draft
Flufd
wants to merge
1
commit into
main
Choose a base branch
from
graphql-fixtures/cached-twister
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP cached twister #2700
Conversation
This file contains 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
Flufd
commented
Dec 7, 2023
Comment on lines
+358
to
+370
seed(seed: number | number[]): void { | ||
if (typeof seed === 'number') { | ||
// if we already have a twister state for the seed, use it | ||
if (cachedTwisterStates[seed]) { | ||
const {mti, mt} = cachedTwisterStates[seed]; | ||
twister.initState({mti, mt: [...mt]}); | ||
} else { | ||
twister.initGenrand(seed); | ||
cachedTwisterStates[seed] = twister.getTwisterState(); | ||
} | ||
} else if (Array.isArray(seed)) { | ||
twister.initByArray(seed, seed.length); | ||
} |
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.
Here is the modification to the seed
function to grab a cached version of the twister state.
Comment on lines
+84
to
+91
getTwisterState() { | ||
return {mt: this.mt, mti: this.mti}; | ||
} | ||
|
||
initState(state: {mt: number[]; mti: number}) { | ||
this.mt = state.mt; | ||
this.mti = state.mti; | ||
} |
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.
added getters and setters for the state
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
I tried to optimise the twister itself, or rather, caching the seeded states of it. This has good speed ups.
WIP PR
In this implementation we hit the cache often within a single test, and I think over a test suite with lots of repeated fills of the same data, we'd see good speed ups too. The cache for the pre-seeded states are hit quite often, I think because of the seed collisions identified in this issue.
This needs the latest version of faker though, as we are then able to instantiate the faker instance with a custom RNG, which is what I did here. This is a copy of the twister in faker itself, with a modification to cache the state of the twister after each seeding.
This update is breaking though, because of the bump to faker, the random values are not guaranteed to be the same. In admin tests, we are (unfortunately, and mistakenly) relying on the random data generated by the filler.
I believe relying on the random data returned by the filler is a bad practice as it blocks any changes to the generation of the random data that we make here, and if we assert on that random data in the tests, it's not clear in where that data is coming from.
So actually I question the value of the randomness of the filler in general. We can fill with valid data for the document without generating it randomly. It adds a lot of overhead and IMO not much value.
More discussion about if we should use faker here #2161