Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ end
gem "rbs", platform: platforms
gem "parser", platform: platforms
gem "ruby_parser", platform: platforms
gem "benchmark-ips"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
specs:
abbrev (0.1.2)
ast (2.4.2)
benchmark-ips (2.13.0)
ffi (1.16.3)
mini_portile2 (2.8.5)
nokogiri (1.16.2)
Expand Down Expand Up @@ -37,6 +38,7 @@ PLATFORMS
ruby

DEPENDENCIES
benchmark-ips
ffi
parser
prism!
Expand Down
30 changes: 25 additions & 5 deletions bin/prism
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Prism
when "parser" then parser(argv)
when "ripper" then ripper(argv)
when "rubyparser" then rubyparser(argv)
when "benchmark" then benchmark(argv)
else
puts <<~TXT
Usage:
Expand All @@ -31,6 +32,7 @@ module Prism
bin/prism parser [source]
bin/prism ripper [source]
bin/prism rubyparser [source]
bin/prism benchmark [source_file]
TXT
end
end
Expand Down Expand Up @@ -146,9 +148,9 @@ module Prism

puts "CRuby:"
p Debug.cruby_locals(source)

puts "Prism:"
p Debug.prism_locals(source)
p Debug.prism_locals(source)
end

# bin/prism parse [source]
Expand Down Expand Up @@ -246,6 +248,24 @@ module Prism
pp prism
end

# bin/prism benchmark [source_file]
def benchmark(argv)
require "benchmark/ips"
require "parser/current"
require "ruby_parser"

filepath = argv.fetch(0) { File.expand_path("../lib/prism/translation/parser/compiler.rb", __dir__) }

Benchmark.ips do |x|
x.report("Parser::CurrentRuby") { Parser::CurrentRuby.parse_file(filepath) }
x.report("Parser::Prism") { Prism.parse_file(filepath) }
Copy link
Member

Choose a reason for hiding this comment

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

This should be labeled just Prism as there is no Parser::Prism (it confused me for the results in the PR description).
I was about to make a PR but @kddnewton already fixed it in 40059d3 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah dang, that's copy-pasta. Thanks for the note, Benoit

x.report("Prism::Translation::Parser") { Prism::Translation::Parser.parse_file(filepath) }
x.report("RubyParser") { RubyParser.new.parse(File.read(filepath), filepath) }
x.report("Prism::Translation::RubyParser") { Prism::Translation::RubyParser.parse_file(filepath) }
x.compare!
end
end

############################################################################
# Helpers
############################################################################
Expand Down Expand Up @@ -291,18 +311,18 @@ module Prism

{ alpha: /[[:alpha:]]/, alnum: /[[:alnum:]]/, isupper: /[[:upper:]]/ }.map do |kind, regex|
codepoints = range.select { |codepoint| codepoint.chr(encoding).match?(regex) }

previous = nil
groups =
codepoints.slice_before do |codepoint|
(!previous.nil? && (codepoint - previous) != 1).tap { previous = codepoint }
end

matched =
groups.flat_map do |group|
["0x#{group.first.to_s(16).upcase}", "0x#{group.last.to_s(16).upcase}"]
end

puts "\n#define UNICODE_#{kind.upcase}_CODEPOINTS_LENGTH #{matched.length}"
puts "unicode_codepoint_t unicode_#{kind}_codepoints[UNICODE_#{kind.upcase}_CODEPOINTS_LENGTH] = {"
matched.each_slice(2) { |slice| puts " #{slice.join(", ")}," }
Expand Down