Skip to content

⚡ Optimize AST Display implementation to reduce allocations#32

Merged
ashyanSpada merged 1 commit into
masterfrom
perf/optimize-ast-display-1488106855483112256
Jan 23, 2026
Merged

⚡ Optimize AST Display implementation to reduce allocations#32
ashyanSpada merged 1 commit into
masterfrom
perf/optimize-ast-display-1488106855483112256

Conversation

@google-labs-jules
Copy link
Copy Markdown
Contributor

@google-labs-jules google-labs-jules Bot commented Jan 23, 2026

This PR optimizes the Display implementation for ExprAST variants (Function, List, Map, Stmt) and Literal types.

Problem:
The previous implementation constructed intermediate String buffers and performed deep clones of AST nodes (param.clone()) during formatting. This led to O(N) allocations and redundant copying, which was inefficient for large or deeply nested expressions.

Solution:
Refactored the fmt methods to write directly to the fmt::Formatter and iterate over references. This eliminates the intermediate allocations and clones.

Performance Impact:
A new benchmark display_expression was added.

  • Baseline: ~11.5 µs
  • Optimized: ~2.95 µs
  • Improvement: ~75% speedup

Verification:
Existing tests passed (cargo test), confirming that the output format remains exactly the same.


PR created automatically by Jules for task 1488106855483112256 started by @ashyanSpada


Note

Improves formatting performance by eliminating unnecessary clones and intermediate String buffers in AST display logic.

  • Refactors fmt::Display for Literal and ExprAST variants to write directly to fmt::Formatter and iterate over references (Function, List, Map, Stmt, plus Unary/Binary/Postfix/Ternary paths)
  • Adds Criterion benchmark benches/display_expression.rs and registers it in Cargo.toml

Written by Cursor Bugbot for commit ccc3ba2. This will update automatically on new commits. Configure here.

Refactored the `Display` implementation for `ExprAST` and `Literal` in `src/parser.rs` to write directly to the formatter (`f`) instead of creating intermediate strings and cloning AST nodes. This significantly reduces allocations and CPU usage during formatting.

Key changes:
- Removed unnecessary `.clone()` calls in `Display` implementations.
- Replaced string concatenation in loops with direct `write!` calls.
- Added a benchmark `benches/display_expression.rs` to measure formatting performance.

Performance improvement: ~75% reduction in execution time for complex expressions.
@google-labs-jules
Copy link
Copy Markdown
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.74%. Comparing base (a656565) to head (ccc3ba2).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #32   +/-   ##
=======================================
  Coverage   88.74%   88.74%           
=======================================
  Files          11       11           
  Lines        1066     1066           
=======================================
  Hits          946      946           
  Misses        120      120           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ashyanSpada ashyanSpada marked this pull request as ready for review January 23, 2026 12:55
@ashyanSpada ashyanSpada merged commit 5576973 into master Jan 23, 2026
10 of 15 checks passed
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.

1 participant