perf(transformer): avoid copying string data#10726
Merged
graphite-app[bot] merged 1 commit intomainfrom May 1, 2025
Merged
Conversation
This was referenced Apr 30, 2025
Member
Author
CodSpeed Instrumentation Performance ReportMerging #10726 will not alter performanceComparing Summary
|
7d86281 to
5e58742
Compare
70737e0 to
492759d
Compare
5e58742 to
12d1f44
Compare
492759d to
781eb55
Compare
This was referenced Apr 30, 2025
12d1f44 to
2f226b0
Compare
781eb55 to
8ba635b
Compare
Contributor
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.
2f226b0 to
8e902a5
Compare
8ba635b to
8d84cf5
Compare
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 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.
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.

Avoid unnecessarily copying string data in transformer, via various methods:
Atomis created from existing string slice, rather than copying string data into arena.AstBuilder::atom_from_cow. When the existing string is aCow<'a, str>,atom_from_cowwill not copy the string when it'sBorrowed.Atom<'static>for static string"default", instead of copying that string into arena.