perf: Reduce best fitting allocations#7411
Merged
MichaReiser merged 2 commits intomainfrom Sep 18, 2023
Merged
Conversation
CodSpeed Performance ReportMerging #7411 will improve performances by 7.76%Comparing Summary
Benchmarks breakdown
|
1a0f9f8 to
a43c245
Compare
Member
Author
|
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
a43c245 to
cc50546
Compare
cc50546 to
ddd5e34
Compare
ddd5e34 to
2843fc0
Compare
charliermarsh
approved these changes
Sep 18, 2023
6 tasks
Boshen
added a commit
to oxc-project/oxc
that referenced
this pull request
Jan 16, 2026
Port of astral-sh/ruff#7411 to oxc_formatter. This optimizes memory allocation for `BestFitting` elements by using a flat storage structure with marker tags instead of nested allocations. Key changes: - Add `StartBestFittingEntry`/`EndBestFittingEntry` tags to delimit variants - Change `BestFittingElement` from `&[&[FormatElement]]` to `&[FormatElement]` - Add `BestFittingVariantsIter` iterator to parse variants from flat structure - Update printer to handle the new structure Benefits: - Reduces memory allocations by avoiding per-variant heap allocations - All variant content stored in a single contiguous buffer Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Let's try this again, now that we have more best fitting elements.
Summary
This PR removes the number of allocations necessary when using
BestFitting.Best fitting performs the following allocations today:
Vecin which it stores all variantsVec<FormatElement>that stores the variant's IR elementsThis PR eliminates 2. It uses
StartBestFittingEntryandEndBestFittingEntrymarkers to identify the start/end of a variant. The outer vector is still required because it otherwise gets fairly complicated to identify the start/end of a variant in case we encounter nested best fittings.This won't have as much impact anymore when #7475 lands but we'll need best fitting to implement Black's preview string processing
Test Plan
cargo test