Skip to content

Conversation

@Conaclos
Copy link
Member

@Conaclos Conaclos commented Nov 8, 2025

Summary

This PR ensures that we allocate exactly the amount of memory needed for Vec when we deserialize a JSON array (assuming that every element is deserialized).
This makes sure that converting a Vec into a boxed slice doesn't re-allcoate.

I had to add impl ExactSizeIterator for iterators in biome_rowan.

  • AstSeparatedListElementsIterator can implement ExactSizeIterator because it builds on SyntaxSlots that implements ExactSizeIterator and no elements from SyntaxSlots is skipped.
  • AstSeparatedListNodesIterator can implement ExactSizeIterator because it builds on AstSeparatedListElementsIterator that implements ExactSizeIterator and it just maps iterated elements in its Ietartor::next function.

Note: we could certainly improve the implementation of Ietrator for AstSeparatedListElementsIterator and AstSeparatedListNodesIterator by implementing methods like last, ... This is out of scope of this PR.

I changed the signature of Deserializable::visit_array and Deserializable::visit_map to accept ExactSizeIterator instead of Iterator.
This is a backward compatible change because an implementor can still use impl Iterator (we have a few places where it is still the case).

Test Plan

CI should still be green.

Docs

No change.

@changeset-bot
Copy link

changeset-bot bot commented Nov 8, 2025

⚠️ No Changeset found

Latest commit: 2e1bdb2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 8, 2025

CodSpeed Performance Report

Merging #8040 will not alter performance

Comparing conaclos/deserialize-exact-allocation (2e1bdb2) with main (78011b1)

Summary

✅ 53 untouched
⏩ 85 skipped1

Footnotes

  1. 85 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 8, 2025

Walkthrough

Trait methods visit_array and visit_map in crates/biome_deserialize now accept ExactSizeIterator instead of Iterator. Implementations for Vec, smallvec::SmallVec, HashSet, BTreeSet, indexmap::IndexSet, HashMap, BTreeMap and indexmap::IndexMap were updated to match, with several constructors now pre‑allocating and extending. In crates/biome_rowan, two AST iterators gained precise size_hint implementations and ExactSizeIterator conformance.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.36% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarises the main change: refactoring deserialization to use exact allocation for arrays and maps via ExactSizeIterator.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, explaining the motivation, implementation details, and rationale for API signature changes.
✨ Finishing touches
  • 📝 Docstrings were successfully generated.
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch conaclos/deserialize-exact-allocation

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2025

Parser conformance results on

js/262

Test result main count This PR count Difference
Total 50977 50977 0
Passed 49764 49764 0
Failed 1171 1171 0
Panics 42 42 0
Coverage 97.62% 97.62% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6320 6320 0
Passed 2106 2106 0
Failed 4214 4214 0
Panics 0 0 0
Coverage 33.32% 33.32% 0.00%

ts/babel

Test result main count This PR count Difference
Total 835 835 0
Passed 742 742 0
Failed 93 93 0
Panics 0 0 0
Coverage 88.86% 88.86% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 18813 18813 0
Passed 14065 14065 0
Failed 4747 4747 0
Panics 1 1 0
Coverage 74.76% 74.76% 0.00%

coderabbitai[bot]

This comment was marked as resolved.

@Conaclos Conaclos marked this pull request as draft November 8, 2025 12:47
@Conaclos Conaclos force-pushed the conaclos/deserialize-exact-allocation branch from 0020ea1 to 90e4f1f Compare November 8, 2025 12:50
@Conaclos Conaclos marked this pull request as ready for review November 8, 2025 12:52
coderabbitai[bot]

This comment was marked as resolved.

@ematipico
Copy link
Member

We could implement #8040 (review) while we're at it

@Conaclos Conaclos force-pushed the conaclos/deserialize-exact-allocation branch from 885edf0 to 9ded9fb Compare November 8, 2025 15:29
@Conaclos Conaclos requested review from a team November 8, 2025 15:31
@Conaclos Conaclos changed the title refactor(deserialize): exact allocation for arrays refactor(deserialize): exact allocation for arrays and maps Nov 8, 2025
coderabbitai[bot]

This comment was marked as resolved.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 8, 2025

Warning

Docstrings generation - IN PROGRESS

Generating docstrings for this pull request

@Conaclos Conaclos force-pushed the conaclos/deserialize-exact-allocation branch from 9ded9fb to 2e1bdb2 Compare November 8, 2025 15:36
@Conaclos Conaclos merged commit 4b8ba8b into main Nov 9, 2025
28 checks passed
@Conaclos Conaclos deleted the conaclos/deserialize-exact-allocation branch November 9, 2025 12:02
@arendjr
Copy link
Contributor

arendjr commented Nov 19, 2025

Had overlooked this PR, but that's some nice work!

ematipico pushed a commit to hamirmahal/biome that referenced this pull request Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Core Area: core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants