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

WIP cached twister #2700

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

WIP cached twister #2700

wants to merge 1 commit into from

Conversation

Flufd
Copy link
Contributor

@Flufd Flufd commented Dec 7, 2023

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

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);
}
Copy link
Contributor Author

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;
}
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant