Skip to content

Reuse parser scratch buffers - #26798

Merged
charliermarsh merged 10 commits into
mainfrom
micha/parser-reuse-scratch-buffers
Jul 16, 2026
Merged

Reuse parser scratch buffers#26798
charliermarsh merged 10 commits into
mainfrom
micha/parser-reuse-scratch-buffers

Conversation

@MichaReiser

@MichaReiser MichaReiser commented Jul 14, 2026

Copy link
Copy Markdown
Member

Summary

Use scratch buffers instead of allocating new Vecs and shrinking them when constructing the AST node. While this doesn't remove the need for copying the elements, it reduces the allocations within our parser (shrink_to_fit often needs to allocate a new buffer and move all elements). One shared scratch buffer per node type is sufficient, because our parser is recursive (more nested parse rule always push to the end, and parse function drain from the end as they complete).

Performance

Very consistent perf improvement

Codspeed memory regression

This can cause the parser's peak memory to increase, because we retain the scratch buffer allocation after parsing a clause completed. However, that memory is only temporary and released as soon as the parser completes. Most project-level memory reports show a memory reduction. It's only specific parser benchmarks that show a memory increase

@astral-sh-bot

astral-sh-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 29.18MB 29.13MB -0.17% (50.62kB) ⬇️
trio 70.84MB 70.78MB -0.08% (59.25kB) ⬇️
sphinx 167.24MB 167.18MB -0.04% (66.00kB) ⬇️
prefect 453.90MB 453.83MB -0.01% (69.25kB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
parsed_module 9.88MB 9.83MB -0.50% (50.62kB) ⬇️

trio

Name Old New Diff Outcome
parsed_module 15.19MB 15.13MB -0.38% (59.25kB) ⬇️

sphinx

Name Old New Diff Outcome
parsed_module 18.52MB 18.46MB -0.35% (66.00kB) ⬇️

prefect

Name Old New Diff Outcome
parsed_module 19.58MB 19.51MB -0.35% (69.25kB) ⬇️

@astral-sh-bot

astral-sh-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@codspeed-hq

codspeed-hq Bot commented Jul 14, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 4 improved benchmarks
❌ 2 (👁 2) regressed benchmarks
✅ 149 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation parser[large/dataset.py] 3.3 ms 3.1 ms +7.13%
Simulation parser[numpy/ctypeslib.py] 657.1 µs 619.1 µs +6.14%
Memory parser[large/dataset.py] 776.3 KB 735.4 KB +5.56%
Simulation parser[pydantic/types.py] 1.4 ms 1.3 ms +5.47%
👁 Memory parser[unicode/pypinyin.py] 45.6 KB 49.8 KB -8.39%
👁 Memory parser[numpy/globals.py] 13.8 KB 18.6 KB -25.99%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing micha/parser-reuse-scratch-buffers (87174d6) with main (9e06f27)

Open in CodSpeed

Footnotes

  1. 2 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.

@astral-sh-bot

astral-sh-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.70%. The percentage of expected errors that received a diagnostic held steady at 90.27%. The number of fully passing files held steady at 97/134.

@MichaReiser MichaReiser added performance Potential performance improvement parser Related to the parser labels Jul 15, 2026
@MichaReiser
MichaReiser marked this pull request as ready for review July 15, 2026 11:59
@MichaReiser
MichaReiser requested a review from dhruvmanila as a code owner July 15, 2026 11:59
@charliermarsh

Copy link
Copy Markdown
Member

This is a great idea.

@charliermarsh
charliermarsh merged commit 9a5220e into main Jul 16, 2026
63 checks passed
@charliermarsh
charliermarsh deleted the micha/parser-reuse-scratch-buffers branch July 16, 2026 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants