Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

WIP cached twister #2700

Closed
wants to merge 1 commit into from
Closed

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

@znja znja closed this Oct 1, 2024
@shopify-shipitnext
Copy link

🔎 View this PR in Shipit Next.

ℹ️ Expand to learn how to deploy and handle emergencies using Shipit Next

Overview

Shipit Next will merge your code on your behalf because this repository uses Shipit Next and its merge queue.

To ship this PR, you can either:

Comment Commands

  • /shipit: Enqueue this PR into the merge queue where it will eventually be merged and deployed.
  • /cancel: Eject this PR from the merge queue and rebuild PRs that were enqueued after this PR.
  • /shipit --jump-queue: Enqueue this PR at the top of the merge queue where it will be included in the next deploy. Use this for non-emergency situations.
    - Emergency handling procedure for this command can be found here.
  • /shipit --emergency: Merge this PR directly into main and deploy to all environments once all require_for_emergency CI checks pass. Please be aware that changes deployed with this command will not be automatically rolled back.

Commands exclusive to Deploy Before Merge

  • /cancel --emergency: Eject this PR from the merge and rollback any deployments containing this PR.

Documentation

Questions or feedback?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants