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(sanity): optimise getLeafWeights to not stack overflow #7999

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

nikaspran
Copy link
Contributor

Description

When working on schemas that branch out a lot (i.e. have arrays that can hold many different types), the current getLeafWeights implementation throws with a RangeError: Maximum call stack size exceeded after a while.

This PR optimises the method to use an accumulator instead, which keeps the stack usage flat and no longer overflows for the same amount of data. I have also added a test to verify this (it used to explode with the previous version, see screenshots).

In addition, this PR optimises the method to be much more efficient by:

  • Merging the loops for objectTypes and arrayTypes into one
  • Adding a few extra tiny optimisations

As a result, this should be much faster and no longer explode the stack.

Before

Screenshot 2024-12-09 at 18 26 37

After

Screenshot 2024-12-09 at 22 56 15

What to review

  • We originally ran into this when rendering a document list once the schema got "branchy" enough, for a lack of a better word

Testing

  • Have added a unit test that would fail under the previous implementation but passes current one
  • This should retain exactly the same functionality as before, just be faster and more memory conscious

Notes for release

Fixes a crash when rendering a document list within Studio given a schema that branches out a lot

@nikaspran nikaspran requested a review from a team as a code owner December 9, 2024 21:16
@nikaspran nikaspran requested review from bjoerge and removed request for a team December 9, 2024 21:16
Copy link

vercel bot commented Dec 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:37pm
performance-studio ✅ Ready (Inspect) Visit Preview Dec 9, 2024 9:37pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:37pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:37pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Dec 9, 2024 9:37pm

Copy link

vercel bot commented Dec 9, 2024

@nikaspran is attempting to deploy a commit to the Sanity Sandbox Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@bjoerge bjoerge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid to me. Appreciate you taking the time to add tests as well @nikaspran 🙏🏼

Would love a second pair of eyes from @juice49 as well.

@bjoerge bjoerge requested a review from juice49 December 10, 2024 09:56
@nikaspran
Copy link
Contributor Author

I can confirm this also fixes our actual issue my company's setup, so would love to get this in and unblock 🙏 Happy to make any changes if necessary.

Copy link
Contributor

@juice49 juice49 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic! Thank you so much for the contribution, @nikaspran.

@bjoerge bjoerge added this pull request to the merge queue Dec 10, 2024
Merged via the queue into sanity-io:next with commit ac2ab18 Dec 10, 2024
33 of 46 checks passed
@nikaspran nikaspran deleted the fix/optimise-getLeafWeights-stack branch December 10, 2024 12:14
bjoerge added a commit that referenced this pull request Dec 12, 2024
* next: (49 commits)
  fix: delays rendering the Start in Create banner until document is ready (#8020)
  fix(deps): update dependency @sanity/ui to ^2.10.9 (#8009)
  chore(deps): dedupe pnpm-lock.yaml (#8023)
  test(playwright-ct): fix issues and flake (#8016)
  feat(cli): misc copy changes (#8003)
  feat(cli): remove is-builtin-module (#6579)
  v3.67.1
  chore(deps): bump minimum requirement of @sanity/import and @sanity/export (#8012)
  chore(deps): update dependency @sanity/pkg-utils to v6.12.0 (#8010)
  fix(core): tasks UpdatedTimeAgo should be a hook (#8011)
  v3.67.0
  chore(prettier): fix unformatted files 🤖 ✨ (#8006)
  chore: reduce renovate double PR noise
  fix(deps): update dependency @sanity/ui to ^2.10.7 (#8005)
  fix(deps): update dependency @sanity/ui to ^2.10.7 (#7998)
  feat(cli): use `@vercel/frameworks` in `bootstrapRemoteTemplate` (#8001)
  feat: switch create integration to opt-out flow (#8002)
  feat: use eslint 9 for new studios (#7978)
  fix(sanity): optimise getLeafWeights to not stack overflow (#7999)
  feat(test-studio): enable `groq2024` search strategy
  ...
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.

3 participants