Skip to content

refactor(allocator,formatter): consolidate borrow-passthrough into reusable alloc_cow_str#23528

Closed
hyf0 wants to merge 1 commit into
oxc-project:mainfrom
hyf0:perf/alloc-cow-str-helper
Closed

refactor(allocator,formatter): consolidate borrow-passthrough into reusable alloc_cow_str#23528
hyf0 wants to merge 1 commit into
oxc-project:mainfrom
hyf0:perf/alloc-cow-str-helper

Conversation

@hyf0

@hyf0 hyf0 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

What

Introduces Allocator::alloc_cow_str(&cow) — returns a Cow's &str without copying when it is Cow::Borrowed, copying into the arena only for Cow::Owned. Crystallizes the borrow-passthrough pattern (string #23465, number #23512, bigint #23534) into one documented, reusable helper instead of an inline match repeated at each site.

It takes the Cow by shared reference so it also works where the Cow lives behind a &self borrow (string literals) and can't be moved out — letting every in-tree site use it.

Pure consolidation: behavior- and allocation-neutral (the bigint alloc win landed separately in #23534; allocation snapshots are unchanged here).

Migrated sites (all of them)

  • oxc_formatter string literals (CleanedStringLiteralText::fmt)
  • oxc_formatter numeric literals (CleanedNumberLiteralText::fmt)
  • oxc_formatter bigint literals
  • oxc_formatter_json: removed the crate-local arena_cow_str wrapper; its call sites now use the shared helper directly (f.allocator().alloc_cow_str(&cow)) — matching how both formatters already call f.allocator().alloc_str(...) directly, with no per-crate wrapper.

Addresses review (@leaysgur)

  • Added # Panics doc.
  • #[inline(always)] (+ #[expect(clippy::inline_always)]) to match the sibling alloc_str.
  • Migrated the other in-tree site that had a local copy (oxc_formatter_json) — and every remaining inline occurrence of the pattern.

Prepared with AI assistance.

@hyf0 hyf0 marked this pull request as ready for review June 17, 2026 02:14
@codspeed-hq

codspeed-hq Bot commented Jun 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing hyf0:perf/alloc-cow-str-helper (471de46) with main (80f1697)

Open in CodSpeed

Footnotes

  1. 9 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.

@leaysgur leaysgur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For oxc_allocator change, I'll leave review to others... (seems fine though), but a couple of things noticed:

  • it's missing a # Panics doc
  • uses #[inline] instead of #[inline(always)] like the sibling?

For formatter part, if we're extracting this into oxc_allocator as a reusable helper, the same pattern elsewhere should be migrated too?
e.g. oxc_formatter_json already has a local arena_cow_str doing exactly this.

@hyf0 hyf0 force-pushed the perf/alloc-cow-str-helper branch from cea172c to ed846f3 Compare June 17, 2026 04:51
@hyf0 hyf0 changed the title perf(allocator,formatter): add alloc_cow_str for the borrow-passthrough pattern refactor(allocator,formatter): consolidate borrow-passthrough into reusable alloc_cow_str Jun 17, 2026
@hyf0

hyf0 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

For oxc_allocator change, I'll leave review to others..., but a couple of things noticed:

  • it's missing a # Panics doc
  • uses #[inline] instead of #[inline(always)] like the sibling?

For formatter part, if we're extracting this into oxc_allocator as a reusable helper, the same pattern elsewhere should be migrated too? e.g. oxc_formatter_json already has a local arena_cow_str doing exactly this.

I split the pure improvement PR into #23534. I left the arena refactor change in this PR so we could focus on the question.

@hyf0 hyf0 force-pushed the perf/alloc-cow-str-helper branch from ed846f3 to b511b9b Compare June 17, 2026 05:19
@hyf0

hyf0 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @leaysgur! Addressed everything, and split the PR along your two points:

  • Perf vs. refactor: the only real allocation win (bigint borrow-passthrough) is now its own PR — perf(formatter): avoid arena copy for already-lowercase bigint literals #23534. This PR is the pure, allocation-neutral consolidation.
  • # Panics doc: added (scoped to the Cow::Owned arm; Cow::Borrowed never allocates).
  • #[inline(always)]: now matches the sibling alloc_str (with #[expect(clippy::inline_always)]).
  • Migrate the pattern elsewhere: done, and all the way. alloc_cow_str takes the Cow by shared reference, so every in-tree site can use it — including the string-literal site whose Cow lives behind &self. I also removed oxc_formatter_json's local arena_cow_str wrapper; its call sites now use f.allocator().alloc_cow_str(&cow) directly, matching how both formatters already call f.allocator().alloc_str(...) with no per-crate wrapper.

Left the oxc_allocator API for others to review, as you suggested.

@leaysgur leaysgur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM for formatter changes. 👌🏻

…usable alloc_cow_str

Adds Allocator::alloc_cow_str(&cow): returns a Cow's &str without copying when Cow::Borrowed,
copying into the arena only for Cow::Owned. Crystallizes the borrow-passthrough pattern
(string oxc-project#23465, number oxc-project#23512, bigint oxc-project#23534) into one documented, reusable helper instead of
the inline match repeated at each site.

Takes the Cow by shared reference so it also works where the Cow lives behind a &self borrow
(string literals) and can't be moved out.

Migrates every in-tree site to it:
- oxc_formatter string / numeric / bigint literals
- oxc_formatter_json: removes the crate-local arena_cow_str wrapper; call sites now use the
  shared helper directly (f.allocator().alloc_cow_str(&cow)), matching how the formatters
  already call f.allocator().alloc_str(...) directly with no per-crate wrapper.

Behaviour- and allocation-neutral (pure consolidation); allocs snapshots unchanged.
@hyf0 hyf0 force-pushed the perf/alloc-cow-str-helper branch from b511b9b to 471de46 Compare June 17, 2026 05:41
@camc314 camc314 added A-linter Area - Linter A-formatter Area - Formatter A-allocator Area - Allocator and removed A-linter Area - Linter labels Jun 17, 2026
@graphite-app graphite-app Bot closed this in 44e4c6c Jun 22, 2026
camc314 pushed a commit that referenced this pull request Jul 3, 2026
Closes #23528.
(I will confirm this (should be lived in `oxc_allocator` side?) later, then refactor all callsites)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-allocator Area - Allocator A-formatter Area - Formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants