Skip to content

Lazily convert Prism/RBS locations to CodeRange objects#436

Open
sinsoku wants to merge 1 commit intoruby:masterfrom
sinsoku:lazy-convert-code-range
Open

Lazily convert Prism/RBS locations to CodeRange objects#436
sinsoku wants to merge 1 commit intoruby:masterfrom
sinsoku:lazy-convert-code-range

Conversation

@sinsoku
Copy link
Copy Markdown
Contributor

@sinsoku sinsoku commented Apr 13, 2026

AST node initializers were eagerly calling code_range_from_node to
convert Prism::Location/RBS::Location into CodeRange, triggering
expensive UTF-16LE encoding via String#encode (9.0% of CPU samples
in tool/dog_bench.rb).

Store the raw location in @mid_code_range_loc / @cname_code_range_loc
and convert lazily on first access via ||=. In batch mode most values
are never accessed; in LSP mode the cost is deferred from file loading
to first interactive use (hover, go-to-definition, etc.).

Also remove mid_code_range from attrs hashes to avoid comparing
unconverted locations in Node#diff.

Measured with tool/dog_bench.rb (stackprof):

  • String#encode: 89 samples (9.0%) -> 19 samples (1.9%)

AST node initializers were eagerly calling `code_range_from_node` to
convert `Prism::Location`/`RBS::Location` into `CodeRange`, triggering
expensive UTF-16LE encoding via `String#encode` (9.0% of CPU samples
in `tool/dog_bench.rb`).

Store the raw location in `@mid_code_range_loc` / `@cname_code_range_loc`
and convert lazily on first access via `||=`. In batch mode most values
are never accessed; in LSP mode the cost is deferred from file loading
to first interactive use (hover, go-to-definition, etc.).

Also remove `mid_code_range` from `attrs` hashes to avoid comparing
unconverted locations in `Node#diff`.

Measured with `tool/dog_bench.rb` (stackprof):
- `String#encode`: 89 samples (9.0%) -> 19 samples (1.9%)
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.

1 participant