Skip to content

perf(formatter): reduce AstNode size by 8 bytes using thread-local allocator#18340

Closed
Dunqing wants to merge 1 commit intomainfrom
01-21-perf_formatter_reduce_astnode_size_by_8_bytes_using_thread-local_allocator
Closed

perf(formatter): reduce AstNode size by 8 bytes using thread-local allocator#18340
Dunqing wants to merge 1 commit intomainfrom
01-21-perf_formatter_reduce_astnode_size_by_8_bytes_using_thread-local_allocator

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Jan 21, 2026

Memory Benchmark Results

Summary

This benchmark measures memory usage during formatting before and after removing the allocator field from AstNode (using thread-local storage instead).

Metric Value
AstNode size 40 bytes → 32 bytes (-20%)
Total formatting overhead BEFORE 33,231,380 bytes (31.7 MB)
Total formatting overhead AFTER 31,221,021 bytes (29.8 MB)
Memory saved 2,010,359 bytes (1.9 MB)
Reduction 6.0%

Per-File Results

File Source Size BEFORE AFTER Saved %
RadixUIAdoptionSection.jsx 2.5 KB 159.0 KB 151.1 KB 7.9 KB 5.0%
errors.ts 8.2 KB 336.6 KB 322.6 KB 14.0 KB 4.2%
Search.tsx 11.7 KB 789.8 KB 746.3 KB 43.5 KB 5.5%
core.js 12.3 KB 776.2 KB 731.1 KB 45.1 KB 5.8%
next.ts 17.3 KB 1.1 MB 1.0 MB 100.0 KB 9.1%
index.tsx 30.5 KB 1.8 MB 1.7 MB 100.0 KB 5.6%
handle-comments.js 27.2 KB 1.3 MB 1.3 MB 0.0 KB 0.0%
types.ts 76.1 KB 7.5 MB 7.1 MB 400.0 KB 5.3%
App.tsx 345.5 KB 21.3 MB 20.1 MB 1.2 MB 5.6%
TOTAL 531.3 KB 35.0 MB 33.1 MB 1.9 MB 5.4%

Benchmark Files

Files sourced from real-world projects:

  • RadixUIAdoptionSection.jsx - Small JSX (61 lines)
  • errors.ts - Vue.js compiler-core (191 lines)
  • Search.tsx - Outline app (389 lines)
  • core.js - Prettier main/core (426 lines)
  • next.ts - Next.js server (619 lines)
  • index.tsx - Next.js client (995 lines)
  • handle-comments.js - Prettier comments handler (1092 lines)
  • types.ts - Hono types (2370 lines)
  • App.tsx - Excalidraw App component (11180 lines)

How to Reproduce

# Create measurement tool
cat > tasks/benchmark/examples/mem_measure.rs << 'EOF'
use oxc_allocator::Allocator;
use oxc_formatter::{FormatOptions, Formatter, SortImportsOptions, get_parse_options};
use oxc_parser::Parser;
use oxc_tasks_common::TestFiles;

fn main() {
    for file in TestFiles::formatter().files() {
        let allocator = Allocator::default();
        let program = Parser::new(&allocator, &file.source_text, file.source_type)
            .with_options(get_parse_options())
            .parse()
            .program;
        let after_parse = allocator.used_bytes();
        let format_options = FormatOptions {
            experimental_sort_imports: Some(SortImportsOptions::default()),
            ..Default::default()
        };
        let _ = Formatter::new(&allocator, format_options).build(&program);
        let after_format = allocator.used_bytes();
        println!("{}: {} -> {}", file.file_name, after_parse, after_format);
    }
}
EOF

# Run benchmark
cargo run -p oxc_benchmark --example mem_measure --release

@github-actions github-actions bot added A-ast-tools Area - AST tools A-formatter Area - Formatter C-performance Category - Solution not expected to change functional behavior, only performance labels Jan 21, 2026
Copy link
Member Author

Dunqing commented Jan 21, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 21, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing 01-21-perf_formatter_reduce_astnode_size_by_8_bytes_using_thread-local_allocator (c5a2ae8) with main (88e0896)1

Summary

✅ 38 untouched benchmarks
⏩ 7 skipped benchmarks2

Footnotes

  1. No successful run was found on main (04a2871) during the generation of this report, so 88e0896 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

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

@Dunqing
Copy link
Member Author

Dunqing commented Jan 26, 2026

Not worth it as it has to store the allocator in the thread_local, but the benefits are negligible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast-tools Area - AST tools A-formatter Area - Formatter C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant