fix(stega): 2x faster encoding of portable text #920
Merged
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.
We got a report of a really slow request that only happens if stega is enabled, here's how to repro:
Turns out the root cause was a lot of portable text arrays that were deeply recursed.
This PR adds short circuit logic for when a PT
block
orspan
is detected, and traverses them more efficiently.To measure the impact I've added a
npm run benchmark
command:✓ test/stega/stegaEncodeSourceMap.bench.ts (2) 5795ms ✓ stegaEncodeSourceMap can handle Portable Text in a performant way (2) 5793ms name hz min max mean p75 p99 p995 p999 rme samples · @sanity/client@latest 4.3355 221.82 282.11 230.65 228.15 282.11 282.11 282.11 ±5.67% 10 · src 9.3105 105.54 109.67 107.41 107.81 109.67 109.67 109.67 ±0.84% 10 fastest
It uses the benchmarking feature in Vitest, so we can add more benchmarks in the future 🙌
As a side-effect of the more efficient recursion there's no longer a need to handle PT edge cases in the stega
filterDefault
function 😌