Skip to content

perf(transformer): avoid copying string data#10726

Merged
graphite-app[bot] merged 1 commit intomainfrom
04-30-perf_transformer_avoid_copying_string_data
May 1, 2025
Merged

perf(transformer): avoid copying string data#10726
graphite-app[bot] merged 1 commit intomainfrom
04-30-perf_transformer_avoid_copying_string_data

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Apr 30, 2025

Avoid unnecessarily copying string data in transformer, via various methods:

  1. Alter lifetimes so that Atom is created from existing string slice, rather than copying string data into arena.
  2. Utilize AstBuilder::atom_from_cow. When the existing string is a Cow<'a, str>, atom_from_cow will not copy the string when it's Borrowed.
  3. Use Atom<'static> for static string "default", instead of copying that string into arena.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance labels Apr 30, 2025
Copy link
Member Author

overlookmotel commented Apr 30, 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.

@overlookmotel overlookmotel marked this pull request as ready for review April 30, 2025 14:04
@overlookmotel overlookmotel requested a review from Dunqing as a code owner April 30, 2025 14:04
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 30, 2025

CodSpeed Instrumentation Performance Report

Merging #10726 will not alter performance

Comparing 04-30-perf_transformer_avoid_copying_string_data (8d84cf5) with 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ (8e902a5)

Summary

✅ 36 untouched benchmarks

@overlookmotel overlookmotel changed the base branch from 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ to graphite-base/10726 April 30, 2025 14:23
@overlookmotel overlookmotel force-pushed the 04-30-perf_transformer_avoid_copying_string_data branch from 70737e0 to 492759d Compare April 30, 2025 14:25
@overlookmotel overlookmotel changed the base branch from graphite-base/10726 to 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ April 30, 2025 14:25
@overlookmotel overlookmotel changed the base branch from 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ to graphite-base/10726 April 30, 2025 21:41
@overlookmotel overlookmotel force-pushed the 04-30-perf_transformer_avoid_copying_string_data branch from 492759d to 781eb55 Compare April 30, 2025 21:57
@overlookmotel overlookmotel changed the base branch from graphite-base/10726 to 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ April 30, 2025 21:57
@graphite-app graphite-app bot force-pushed the 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ branch from 12d1f44 to 2f226b0 Compare May 1, 2025 02:10
@graphite-app graphite-app bot force-pushed the 04-30-perf_transformer_avoid_copying_string_data branch from 781eb55 to 8ba635b Compare May 1, 2025 02:10
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label May 1, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented May 1, 2025

Merge activity

Avoid unnecessarily copying string data in transformer, via various methods:

1. Alter lifetimes so that `Atom` is created from existing string slice, rather than copying string data into arena.
2. Utilize `AstBuilder::atom_from_cow`. When the existing string is a `Cow<'a, str>`, `atom_from_cow` will not copy the string when it's `Borrowed`.
3. Use `Atom<'static>` for static string `"default"`, instead of copying that string into arena.
@graphite-app graphite-app bot force-pushed the 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ branch from 2f226b0 to 8e902a5 Compare May 1, 2025 06:41
@graphite-app graphite-app bot force-pushed the 04-30-perf_transformer_avoid_copying_string_data branch from 8ba635b to 8d84cf5 Compare May 1, 2025 06:41
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 1, 2025
Base automatically changed from 04-30-perf_transformer_regexp_avoid_copying_string_data_and_temp_string_ to main May 1, 2025 06:54
@graphite-app graphite-app bot merged commit 8d84cf5 into main May 1, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 04-30-perf_transformer_avoid_copying_string_data branch May 1, 2025 06:56
graphite-app bot pushed a commit that referenced this pull request May 1, 2025
…ifetime (#10735)

Closes #10734.

`AstBuilder` methods previously took `IntoIn<'a, Atom<'a>>`. This is a footgun because it makes it really easy to end up copying string data from an existing `Atom`, when that `Atom` could be reused for free. Just passing an `&Atom` instead of `Atom` is enough to accidentally trigger copying the whole string, incurring higher memory usage.

Instead, take `Into<Atom<'a>>`. This still allows the convenience of passing `&'static str` or `&'a str`, but these don't result in the string being copied, only referenced.

Additionally, only take a `&'a str` in `AstBuilder::program`, instead of `IntoIn<'a, &'a str>`, for the same reasons.

All the changes in this PR outside of `AstBuilder` itself are just to manually allocate `Atom`s in the few places where the string does not already exist in the arena. All the places where we were unnecessarily copying string data are already addressed in PRs that precede this one e.g. #10726.

The downside of this change is that it requires more verbose code in places, but I think that's outweighed by the benefit. Now when calling an `AstBuilder` method, you have to ask yourself "do I need to copy this string?" and you have to insert an `ast.atom(...)` call if you conclude that you need to. Previously you didn't have to consider that question, and poor performance/memory usage was the default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler 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