Skip to content

Benchmarks vs Ripper, whitequark/parser #1692

Closed

Description

@faraazahmad

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):

spec/requests/application_controller_spec.rb
spec/lib/text_cleaner_spec.rb
spec/integration/invalid_request_spec.rb
spec/helpers/application_helper_spec.rb
spec/lib/middleware/request_tracker_spec.rb
spec/lib/freedom_patches/safe_buffer_spec.rb

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?

code to reproduce:

require 'prism'
require 'parser/ruby32'
require 'ripper'
require 'parallel'
require 'benchmark'

GLOB = "#{Dir.home}/oss/discourse/**/*.rb"
puts RUBY_DESCRIPTION

def prism
  Parallel.each(Dir.glob(GLOB)) do |file|
    Prism.parse_file(file)
  end
end

def parser
  Parallel.each(Dir.glob(GLOB)) do |file|
    Parser::Ruby32.parse_file(file)
  end
end

def ripper
  Parallel.each(Dir.glob(GLOB)) do |file|
    Ripper.parse(file)
  end
end

Benchmark.bm do |x|
  x.report('Ripper') { ripper }
  x.report('Parser::Ruby32') { parser }
  x.report('Prism') { prism }
end

Gemfile:

source "https://rubygems.org"

gem "parallel", "~> 1.23"
gem "prism", "~> 0.14.0"
gem "parser", "~> 3.2"

Activity

  1. eregon commented on Oct 16, 2023

    @eregon
    Member

    From what I can see Ripper.parse(file) does nothing:

    irb(main):008:0> RUBY_VERSION
    => "3.2.2"
    irb(main):009:0> Ripper.parse("test.rb")
    => nil
    irb(main):010:0> Ripper.parse(File.read("test.rb"))
    => nil
    irb(main):011:0> Ripper.sexp(File.read("test.rb"))
    => 
    [:program,  ...
    

    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.

  2. eregon commented on Oct 16, 2023

    @eregon
    Member

    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.

  3. froydnj commented on Oct 16, 2023

    @froydnj
    Contributor

    Also consider moving the Dir.glob call outside the benchmarks, so you're just testing parsing performance?

  4. eregon commented on Oct 16, 2023

    @eregon
    Member

    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.

  5. faraazahmad commented on Oct 16, 2023

    @faraazahmad
    ContributorAuthor

    Ah yes thanks for the suggestions! I made some changes:

    require 'prism'
    require 'parser/ruby32'
    require 'ripper'
    require 'benchmark'
    
    puts RUBY_DESCRIPTION
    FILES = Dir.glob("#{Dir.home}/oss/discourse/**/*.rb")
    
    def prism
      FILES.each { |file| Prism.parse_file(file) }
    end
    
    def parser
      FILES.each { |file| Parser::Ruby32.parse_file(file) }
    end
    
    def ripper
      FILES.each { |file| Ripper.sexp(File.read(file)) }
    end
    
    Benchmark.bmbm do |x|
      x.report('Ripper') { ripper }
      x.report('Parser::Ruby32') { parser }
      x.report('Prism') { prism }
    end

    and Prism is definitely faster than Ripper:

    ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]
    Rehearsal --------------------------------------------------
    Ripper           2.780442   0.157583   2.938025 (  2.941024)
    Parser::Ruby32  35.392140   0.270148  35.662288 ( 35.688984)
    Prism            1.785082   0.133348   1.918430 (  1.919916)
    ---------------------------------------- total: 40.518743sec
    
                         user     system      total        real
    Ripper           2.517396   0.137712   2.655108 (  2.656233)
    Parser::Ruby32  35.451241   0.289852  35.741093 ( 35.816163)
    Prism            1.710153   0.130002   1.840155 (  1.841665)
    

    However, it was strange seeing Ripper#parse return nil. Seems like its not a Prism issue so I guess it can be closed.

  6. eregon commented on Oct 16, 2023

    @eregon
    Member

    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!

  7. kddnewton commented on Oct 16, 2023

    @kddnewton
    Collaborator

    Unfortunately this is very much not a 1:1 comparison.

    1. 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.
    2. 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.
    3. 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.

  8. eregon commented on Oct 16, 2023

    @eregon
    Member

    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.
  9. faraazahmad commented on Oct 16, 2023

    @faraazahmad
    ContributorAuthor

    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.

  10. kddnewton commented on Oct 16, 2023

    @kddnewton
    Collaborator

    In that case you should check out https://github.com/kddnewton/parser-prism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions