Skip to content

Introduce bumpalo for arena-based allocation#124

Open
cijiugechu wants to merge 8 commits intoridiculousfish:masterfrom
cijiugechu:introduce-bumpalo
Open

Introduce bumpalo for arena-based allocation#124
cijiugechu wants to merge 8 commits intoridiculousfish:masterfrom
cijiugechu:introduce-bumpalo

Conversation

@cijiugechu
Copy link

Summary

This pull request introduces bumpalo as an arena allocator for the regex compilation process. By allocating intermediate data structures within a single memory arena, this change reduces the number of calls to the global allocator and improves compilation performance.

Motivation

The current regex compiler performs numerous small, individual allocations for nodes, character sets, and other temporary objects. Each allocation incurs overhead from the system's global allocator, which can become a performance bottleneck, especially when compiling complex regular expressions or a high volume of expressions in a tight loop.

Benchmark Results

To validate the performance improvement, I ran benchmarks using criterion comparing the master branch with the changes in this PR. The benchmarks measure the time taken to compile various regular expressions.

Environment:

  • CPU: AMD 5800H
  • OS: Windows 11
  • Rustc: 1.88.0

Results:

PR:

match                   time:   [1.1117 µs 1.1150 µs 1.1190 µs]
                        change: [+0.5259% +1.5259% +2.4285%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

complex match           time:   [3.2961 µs 3.3050 µs 3.3185 µs]
                        change: [−0.2778% +0.0724% +0.4733%] (p = 0.71 > 0.05)
                        No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe

     Running benches\replacement.rs (target\release\deps\replacement-9ccaa7d52867a80a.exe)
Gnuplot not found, using plotters backend
replacement             time:   [1.2250 µs 1.2307 µs 1.2391 µs]
                        change: [−1.7877% −0.9720% −0.0563%] (p = 0.04 < 0.05)
                        Change within noise threshold.
Found 13 outliers among 100 measurements (13.00%)
  7 (7.00%) high mild
  6 (6.00%) high severe

complex replacement     time:   [3.7252 µs 3.7388 µs 3.7558 µs]
                        change: [−0.5110% −0.0665% +0.3653%] (p = 0.77 > 0.05)
                        No change in performance detected.
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) high mild
  4 (4.00%) high severe

main:

match                   time:   [1.2032 µs 1.2095 µs 1.2168 µs]
                        change: [+7.0828% +7.9067% +8.6668%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 11 outliers among 100 measurements (11.00%)
  10 (10.00%) high mild
  1 (1.00%) high severe

complex match           time:   [4.3964 µs 4.4092 µs 4.4241 µs]
                        change: [+33.144% +34.088% +35.177%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 8 outliers among 100 measurements (8.00%)
  6 (6.00%) high mild
  2 (2.00%) high severe

     Running benches\replacement.rs (target\release\deps\replacement-3570e8082fae5506.exe)
Gnuplot not found, using plotters backend
replacement             time:   [1.3258 µs 1.3388 µs 1.3526 µs]
                        change: [+6.4229% +7.7029% +9.0819%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 8 outliers among 100 measurements (8.00%)
  6 (6.00%) high mild
  2 (2.00%) high severe

complex replacement     time:   [4.7559 µs 4.7651 µs 4.7760 µs]
                        change: [+28.158% +28.980% +29.863%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

@ridiculousfish ridiculousfish self-requested a review July 12, 2025 16:36
Copy link
Owner

@ridiculousfish ridiculousfish left a comment

Choose a reason for hiding this comment

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

Thank you for this work - though it failed to compile even when running directly on commit a17ffa4910ce27a57c56022405837cc4c40476f2 (I've since tweaked the GitHub settings so you can run our pipelines and tests in CI).

Also I think introducing bumpalo is premature. bumpalo is by necessity an invasive change and programming against arenas is unfamiliar to a lot of Rust programmers.

As of yet zero work has been put into optimizing the parser and optimizer. I think there's a lot of low-hanging fruit there. My suggested approach is to introduce the benchmarking suite you propose, use normal profiling techniques to optimize the parse and optimization phases, and then only reach for bumpalo and similar only if and when we've exhausted what ordinary optimizations cna give us.

@cijiugechu
Copy link
Author

Thank you for this work - though it failed to compile even when running directly on commit a17ffa4910ce27a57c56022405837cc4c40476f2 (I've since tweaked the GitHub settings so you can run our pipelines and tests in CI).

Also I think introducing bumpalo is premature. bumpalo is by necessity an invasive change and programming against arenas is unfamiliar to a lot of Rust programmers.

As of yet zero work has been put into optimizing the parser and optimizer. I think there's a lot of low-hanging fruit there. My suggested approach is to introduce the benchmarking suite you propose, use normal profiling techniques to optimize the parse and optimization phases, and then only reach for bumpalo and similar only if and when we've exhausted what ordinary optimizations cna give us.

Thank you for the feedback and for taking the time to review my PR. I also appreciate you tweaking the GitHub settings so I can run the CI pipeline; that will be very helpful.

My initial intention was to contain the use of bumpalo strictly within the compilation phase as an internal implementation detail, copying all final data structures back to the heap to avoid affecting any public APIs.

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.

2 participants