Skip to content

refactor(allocator): introduce Alloc trait#11198

Merged
graphite-app[bot] merged 1 commit intomainfrom
05-20-feat_allocator_introduce_alloc_trait
May 21, 2025
Merged

refactor(allocator): introduce Alloc trait#11198
graphite-app[bot] merged 1 commit intomainfrom
05-20-feat_allocator_introduce_alloc_trait

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented May 20, 2025

First step towards replacing bumpalo with our own allocator (#11145).

Introduce Alloc trait and implement it for bumpalo::Bump. A later PR will parameterize Vec with Alloc, instead of a static dependency on Bump.

We can later on replace Bump with our own Arena type. Arena will also implement Alloc, so the two will be able to be swapped with a minimum of fuss.

Why do we need a trait at all?

Why couldn't Vec just statically depend on Arena, same as it now does on Bump?

Rationale for a trait here is: My intent is to have 2 different variants of Arena to support filling allocator chunks from both ends (string data fills from the start, all other types fill from the end). This will have 3 advantages:

  1. More tightly packed data. &str / Atom is the only type in the AST which is not aligned on 8. Strings currently often tend to end up with unnecessary padding bytes around them.
  2. Packing all string data into a single immutable stretch of memory has potential to speed up all string operations (Make Atom<'a> and CompactString nice and fast backlog#46 (comment)).
  3. It should unblock the biggest current perf bottleneck in raw transfer, by allowing all strings in the AST to be converted to UTF-16 in a single shot (as if they were just 1 long string), rather than converting each string individually.

String is a wrapper around Vec, and it's preferable not to have to implement Vec twice for front-filling (strings) and back-filling (all other types). Vec<T, A: Alloc> is a simpler solution.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label May 20, 2025
Copy link
Member Author

overlookmotel commented May 20, 2025


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 May 20, 2025

CodSpeed Instrumentation Performance Report

Merging #11198 will not alter performance

Comparing 05-20-feat_allocator_introduce_alloc_trait (a2ab84b) with main (66e0b1f)

Summary

✅ 38 untouched benchmarks

@overlookmotel overlookmotel marked this pull request as ready for review May 20, 2025 22:16
@overlookmotel overlookmotel changed the title feat(allocator): introduce Alloc trait refactor(allocator): introduce Alloc trait May 20, 2025
@github-actions github-actions bot added the C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior label May 20, 2025
@overlookmotel overlookmotel force-pushed the 05-20-feat_allocator_introduce_alloc_trait branch 2 times, most recently from ae7a48f to 69ca688 Compare May 20, 2025 22:23
@Dunqing
Copy link
Member

Dunqing commented May 21, 2025

Wow, that's a good idea to improve String

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label May 21, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented May 21, 2025

Merge activity

First step towards replacing `bumpalo` with our own allocator (#11145).

Introduce `Alloc` trait and implement it for `bumpalo::Bump`. A later PR will parameterize `Vec` with `Alloc`, instead of a static dependency on `Bump`.

We can later on replace `Bump` with our own `Arena` type. `Arena` will also implement `Alloc`, so the two will be able to be swapped with a minimum of fuss.

### Why do we need a trait at all?

Why couldn't `Vec` just statically depend on `Arena`, same as it now does on `Bump`?

Rationale for a trait here is: My intent is to have 2 different variants of `Arena` to support filling allocator chunks from both ends (string data fills from the start, all other types fill from the end). This will have 3 advantages:

1. More tightly packed data. `&str` / `Atom` is the only type in the AST which is not aligned on 8. Strings currently often tend to end up with unnecessary padding bytes around them.
2. Packing all string data into a single immutable stretch of memory has potential to speed up all string operations (oxc-project/backlog#46 (comment)).
3. It should unblock the biggest current perf bottleneck in raw transfer, by allowing all strings in the AST to be converted to UTF-16 in a single shot (as if they were just 1 long string), rather than converting each string individually.

`String` is a wrapper around `Vec`, and it's preferable not to have to implement `Vec` twice for front-filling (strings) and back-filling (all other types). `Vec<T, A: Alloc>` is a simpler solution.
@graphite-app graphite-app bot force-pushed the 05-20-feat_allocator_introduce_alloc_trait branch from 69ca688 to a2ab84b Compare May 21, 2025 00:42
graphite-app bot pushed a commit that referenced this pull request May 21, 2025
Parameterize `InnerVec` (otherwise known as `Vec2`) and `RawVec` with `Alloc` trait introduced in #11198.

`InnerVec` and `RawVec` no longer have a dependency on `bumpalo::Bump`. The rationale for that is discussed in #11198.

The only substantive change this PR makes is that all `Alloc` methods panic/abort if allocation fails, rather than returning a `Result::Err`. This alters the behavior of `try_*` methods, which previously wouldn't panic. However, I don't believe we use any of those methods, and we have no plans to, so in my view we should remove them. In practice it should be very difficult to exhaust all memory, and if we did there's no way to gracefully recover from that (what would we do? only parse half the file?). So this has little/no practical impact.

That apart, this PR is pure refactor. The diff is large, but it's almost entirely adding the `A: Alloc` bound to all methods / iterators, and removing `Bump`.
@graphite-app graphite-app bot merged commit a2ab84b into main May 21, 2025
24 checks passed
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 21, 2025
@graphite-app graphite-app bot deleted the 05-20-feat_allocator_introduce_alloc_trait branch May 21, 2025 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants