You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran some benchmarks, parsing discourse@597ef1119536693ef0bc7fe799ff485047f9f6f7 against Prism, Ripper and whitequark/parser. I've removed the following files from the discourse codebase to get rid of all encoding errors (thrown by whitequark/parser):
and got the following results for ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]:
user system total real
Ripper 0.133004 0.210824 0.743934 ( 0.278801)
Parser::Ruby32 0.203646 0.187459 85.015693 ( 10.967147)
Prism 0.163261 0.203811 6.541322 ( 0.968234)
whitequark/parser being the slowest makes sense. However, I was wondering why prism is currently slower than ripper. Is it a consequence of having much better error tolerance, or is it something that can be improved after higher priority issues are fixed?
Could you try without using parallel? Because that doesn't make much sense on CRuby since CRuby has the GVL, and it likely increases the amount of noise in the results + makes it a bit harder to repro.
I tried ruby -rripper -e 'p Ripper.parse("42")' on all ruby versions (since 1.9 when Ripper was added) and they all return nil. I guess that's not how that API is supposed to be used, and the Ripper documentation is bad.
One more thing is it's likely better to use Benchmark.bmbm, at least on Linux the first iteration is much slower otherwise, probably due to files getting in the cache or so.
Great, those results show that Prism is ~1.5x faster than Ripper (and has a much better interface) and ~20x faster than the parser gem, on discourse/**/*.rb!
Unfortunately this is very much not a 1:1 comparison.
The parser gem is written all in Ruby, and suffers some major performance penalties from allocating so many tiny objects. It's scanner is also generated, which leads to all kinds of problems.
The ripper interface is provided so that users can build their own ASTs. By default it calls into methods like on_call and then uses the return value to build up the tree on subsequent "event" method calls. It returns nil by default when nothing is defined. sexp and sexp_raw are two methods that implement the Ripper interface, but they suffer from the issue of going back and forth between C and Ruby on every node and token.
Prism parses everything in C, and then makes one pass to convert everything into Ruby.
Probably the most fair comparison to date would be RubyVM::AbstractSyntaxTree.parse_file(filepath) versus Prism.const_get(:Debug).profile_file(filepath). But even that is not really a fair comparison because we do things that they don't and they do things that we don't.
The only real truly fair comparison is going to be RubyVM::InstructionSequence.compile(source) vs RubyVM::InstructionSequence.compile_prism(source). That will go through the whole chain of source -> ast -> iseqs. But we're not quite there yet. Even this comparison won't be as useful as you might think because it depends on your use case.
Overall, this kind of benchmarking is useful for us to hit performance goals, but Matz has already said he's happy with prism's performance.
It depends on the context though (I think you are being too humble here :)):
If the context is a tool using Prism/Ripper/parser then I think the benchmark above is fully appropriate, because this is the APIs that tools written in Ruby are most likely to use. It's well known that e.g. RuboCop spends a lot of time in the parser gem, using Prism (or even a converter from Prism AST to parser AST) would speed it up significantly.
If it's about measuring the parser speed only and no output, that's much trickier, the closest is what @kddnewton wrote above (RubyVM::AbstractSyntaxTree.parse_file(filepath) versus Prism.const_get(:Debug).profile_file(filepath)), parse_file might still do some Ruby-level allocations though, unsure. But I would say that benchmark is not really relevant: who parses but does nothing with it? Then just not parsing is faster. It is useful for profiling only parsing and optimizing Prism though.
If it's about CRuby code loading speed then indeed what matters then is how fast to parse and compile to bytecode. It would be great to have any speedup there as it could affect code loading speed/application startup.
If the context is a tool using Prism/Ripper/parser then I think the benchmark above is fully appropriate, because this is the APIs that tools written in Ruby are most likely to use. It's well known that e.g. RuboCop spends a lot of time in the parser gem, using Prism (or even a converter from Prism AST to parser AST) would speed it up significantly.
Thats exactly what I'm interested in most atm. I've been exploring how much faster RuboCop could run if rubocop-ast used prism under the hood.
Activity
eregon commentedon Oct 16, 2023
From what I can see
Ripper.parse(file)does nothing:Could you try without using
parallel? Because that doesn't make much sense on CRuby since CRuby has the GVL, and it likely increases the amount of noise in the results + makes it a bit harder to repro.eregon commentedon Oct 16, 2023
I tried
ruby -rripper -e 'p Ripper.parse("42")'on all ruby versions (since 1.9 when Ripper was added) and they all returnnil. I guess that's not how that API is supposed to be used, and the Ripper documentation is bad.froydnj commentedon Oct 16, 2023
Also consider moving the
Dir.globcall outside the benchmarks, so you're just testing parsing performance?eregon commentedon Oct 16, 2023
One more thing is it's likely better to use
Benchmark.bmbm, at least on Linux the first iteration is much slower otherwise, probably due to files getting in the cache or so.faraazahmad commentedon Oct 16, 2023
Ah yes thanks for the suggestions! I made some changes:
and Prism is definitely faster than Ripper:
However, it was strange seeing
Ripper#parsereturnnil. Seems like its not a Prism issue so I guess it can be closed.eregon commentedon Oct 16, 2023
Great, those results show that Prism is ~1.5x faster than Ripper (and has a much better interface) and ~20x faster than the parser gem, on
discourse/**/*.rb!kddnewton commentedon Oct 16, 2023
Unfortunately this is very much not a 1:1 comparison.
on_calland then uses the return value to build up the tree on subsequent "event" method calls. It returnsnilby default when nothing is defined.sexpandsexp_raware two methods that implement the Ripper interface, but they suffer from the issue of going back and forth between C and Ruby on every node and token.Probably the most fair comparison to date would be
RubyVM::AbstractSyntaxTree.parse_file(filepath)versusPrism.const_get(:Debug).profile_file(filepath). But even that is not really a fair comparison because we do things that they don't and they do things that we don't.The only real truly fair comparison is going to be
RubyVM::InstructionSequence.compile(source)vsRubyVM::InstructionSequence.compile_prism(source). That will go through the whole chain of source -> ast -> iseqs. But we're not quite there yet. Even this comparison won't be as useful as you might think because it depends on your use case.Overall, this kind of benchmarking is useful for us to hit performance goals, but Matz has already said he's happy with prism's performance.
eregon commentedon Oct 16, 2023
It depends on the context though (I think you are being too humble here :)):
RubyVM::AbstractSyntaxTree.parse_file(filepath)versusPrism.const_get(:Debug).profile_file(filepath)),parse_filemight still do some Ruby-level allocations though, unsure. But I would say that benchmark is not really relevant: who parses but does nothing with it? Then just not parsing is faster. It is useful for profiling only parsing and optimizing Prism though.faraazahmad commentedon Oct 16, 2023
Thats exactly what I'm interested in most atm. I've been exploring how much faster RuboCop could run if
rubocop-astusedprismunder the hood.kddnewton commentedon Oct 16, 2023
In that case you should check out https://github.com/kddnewton/parser-prism.