Skip to content

[prism] Avoid allocating Strings by threading 'src lifetime throughout parser state and node types#745

Merged
reese merged 4 commits intotrunkfrom
reese-ident-lifetimes
Jan 1, 2026
Merged

[prism] Avoid allocating Strings by threading 'src lifetime throughout parser state and node types#745
reese merged 4 commits intotrunkfrom
reese-ident-lifetimes

Conversation

@reese
Copy link
Collaborator

@reese reese commented Jan 1, 2026

The biggest remaining source of unnecessary String allocations is simple constants like identifiers that come from source code. Since historically we haven't operated by references (since we had to copy all the Ripper objects) back to source nodes, most of the main structs (ParserState etc.) don't have clear lifetimes, forcing most of our render tokens to operate entirely on owned Strings.

This PR is a bit invasive but ultimately relatively straightforward: it adds a 'src lifetime, adds it throughout all of the structs and functions that operate on Prism nodes or tokens, and then updates places to use {loc,const}_to_str wherever possible. This means we no longer need to clone identifiers or convert them to strings, including in some of the associated machinery (e.g. variable bindings).

It's probably easiest to review this commit-by-commit. The first commit mostly adds lifetimes to structs, the second to formatting functions, and the last converts *_to_string to *_to_str wherever possible. It's possible this same thing could be applied to other callers (Strings, heredoc closers, etc.) but for the sake of minimizing the change I've punted on those for later.

Copy link
Collaborator

@froydnj froydnj left a comment

Choose a reason for hiding this comment

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

Oh man, I thought this was going to have to wait until the ripper backend was gone. So nice.

@reese reese force-pushed the reese-ident-lifetimes branch from e3a9a0b to d07e1c4 Compare January 1, 2026 14:22
@reese reese merged commit a037b58 into trunk Jan 1, 2026
8 checks passed
@reese reese deleted the reese-ident-lifetimes branch January 1, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants