Commit b89b2a1
committed
Enable reusable Prism parse result
Follow-up to Shopify/ruby-lsp#1849.
This feature utilizes ruby/prism#3478 to implement Shopify/ruby-lsp#1849.
By using ruby/prism#3478, this feature enables performance improvements by reusing Prism's parsed results
instead of parsing the source code. Below is a sample code snippet, which is expected to improve performance by
1.3x in this case.
```ruby
#!/usr/local/bin/ruby
require 'benchmark/ips'
require 'prism'
require 'rubocop-ast'
@source = File.read(__FILE__)
@parse_lex_result = Prism.parse_lex(@source)
def build_processed_source(parse_lex_result)
RuboCop::AST::ProcessedSource.new(
@source,
3.4,
__FILE__,
parser_engine: 'parser_prism',
prism_result: parse_lex_result
)
end
Benchmark.ips do |x|
x.report('source') { build_processed_source(nil) }
x.report('Prism::ParseLexResult') { build_processed_source(@parse_lex_result) }
x.compare!
end
```
```console
$ bundle exec ruby reusable_ast.rb
ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-darwin23]
Warming up --------------------------------------
source 116.000 i/100ms
Prism::ParseLexResult
151.000 i/100ms
Calculating -------------------------------------
source 1.144k (± 8.4%) i/s - 5.684k in 5.018270s
Prism::ParseLexResult
1.460k (± 5.2%) i/s - 7.399k in 5.082102s
Comparison:
Prism::ParseLexResult: 1460.3 i/s
source: 1144.4 i/s - 1.28x slower
```
Achieving 1.3x speedup with such this simple modification is a significant improvement for Ruby LSP and its users.
## Compatibility
By using `parser_class.instance_method(:initialize).parameters.assoc(:key)`,
the implementation checks whether `Prism#initialize` supports the `:parser` keyword.
If it does not, the system falls back to the conventional parsing method.
This ensures backward compatibility with previous versions of Prism.
## Development Notes
Since this feature is specifically designed for Prism, the keyword name `prism_result` is meant for Prism.
While it may be possible to achieve similar functionality with the Parser gem, there are currently no concrete use cases.
Therefore, for clarity, we have chosen `prism_result` as the keyword.
Additionally, Prism has two types of results: `Prism::ParseResult` and `Prism::ParseLexResult`,
but the `prism_result` keyword argument is meant to receive `Prism::ParseLexResult`.
Since `prism_parse_lex_result` would be too long, the name `prism_result` was chosen to align with the superclass `Prism::Result`.1 parent 47e0eea commit b89b2a1
File tree
3 files changed
+99
-7
lines changed- changelog
- lib/rubocop/ast
- spec/rubocop/ast
3 files changed
+99
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
7 | 28 | | |
8 | 29 | | |
9 | 30 | | |
| |||
25 | 46 | | |
26 | 47 | | |
27 | 48 | | |
28 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
29 | 52 | | |
30 | 53 | | |
31 | 54 | | |
| |||
44 | 67 | | |
45 | 68 | | |
46 | 69 | | |
47 | | - | |
| 70 | + | |
48 | 71 | | |
49 | 72 | | |
50 | 73 | | |
| |||
202 | 225 | | |
203 | 226 | | |
204 | 227 | | |
205 | | - | |
| 228 | + | |
206 | 229 | | |
207 | 230 | | |
208 | 231 | | |
| |||
216 | 239 | | |
217 | 240 | | |
218 | 241 | | |
219 | | - | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
220 | 245 | | |
221 | 246 | | |
222 | 247 | | |
| |||
326 | 351 | | |
327 | 352 | | |
328 | 353 | | |
329 | | - | |
| 354 | + | |
| 355 | + | |
330 | 356 | | |
331 | 357 | | |
332 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
333 | 376 | | |
334 | 377 | | |
335 | 378 | | |
| |||
341 | 384 | | |
342 | 385 | | |
343 | 386 | | |
| 387 | + | |
344 | 388 | | |
345 | 389 | | |
346 | 390 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
| |||
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
15 | 22 | | |
16 | 23 | | |
17 | 24 | | |
| |||
59 | 66 | | |
60 | 67 | | |
61 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
62 | 79 | | |
63 | 80 | | |
64 | 81 | | |
| |||
94 | 111 | | |
95 | 112 | | |
96 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
97 | 124 | | |
98 | 125 | | |
99 | 126 | | |
100 | 127 | | |
101 | 128 | | |
102 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
103 | 140 | | |
104 | 141 | | |
105 | 142 | | |
106 | 143 | | |
107 | 144 | | |
108 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
109 | 156 | | |
110 | 157 | | |
111 | 158 | | |
| |||
0 commit comments