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

fix(stega): 2x faster encoding of portable text #920

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Nov 18, 2024

We got a report of a really slow request that only happens if stega is enabled, here's how to repro:

const {createClient, stegaEncodeSourceMap} = await import('https://esm.sh/@sanity/client/stega')

const client = createClient({
  projectId: '4n2hubkf',
  dataset: 'production',
  apiVersion: '2024-11-15',
  useCdn: true,
  stega: {studioUrl: '/studio'}
})

const {result, resultSourceMap} = await client.fetch(`{
    "entries": *[_type == "post"] | order(publishedAt desc) {
    ...,
    title,
    excerpt,
    date,
    author-> {
        ...,
    },
},
}`, {}, {resultSourceMap: 'withKeyArraySelector', filterResponse: false})

console.profile()
stegaEncodeSourceMap(result, resultSourceMap, {enabled: true, studioUrl: '/studio'})
console.profileEnd()

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 or span 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 😌

@stipsan stipsan force-pushed the fix-portable-text-stega-bottleneck branch from 8a27802 to bb87833 Compare November 18, 2024 19:14
@stipsan stipsan merged commit 8ae6d30 into main Nov 18, 2024
15 checks passed
@stipsan stipsan deleted the fix-portable-text-stega-bottleneck branch November 18, 2024 19:20
@ecospark ecospark bot mentioned this pull request Nov 18, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 26, 2024
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.

1 participant