refactor: Avoid sending settings from worker threads #827
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.
Each time the worker threads communicate results back to the main thread, they currently include the
settings
object on every character in the list of (up to) 50 character results, even though those settings will be the same for every result from a certain combination.Back when the calculation was not run in a worker, this just meant that every character object had a pointer to the same settings object. But data sent via postMessage is cloned, meaning that every character object includes its own fresh copy of the identical settings object once it's sent back from a worker thread.
This moves the "attach settings object" behavior out of optimizercore, which is definitely run in a worker thread, and into optimizer.ts (the coordinator that manages state between workers and sorts results), which doesn't have to be in a worker (I'm currently undecided on whether it's better to or not).
This should save some postMessage traffic and memory/garbage collection, but it seems to have no meaningful effect on performance so far.
[draft previews]