Skip to content

Commit

Permalink
Finalize Kevin's handrolled parser.
Browse files Browse the repository at this point in the history
And get rid of the Ragel parser.

This is 7% faster on activitypub, 15% after on twitter and 11% faster
on citm_catalog.

There might be some more optimization opportunities, I did a quick
optimization pass to fix a regression in string parsing, but other
than that I haven't dug much in performance.
  • Loading branch information
byroot committed Jan 16, 2025
1 parent 5694cff commit 03a7d4f
Show file tree
Hide file tree
Showing 5 changed files with 1,372 additions and 1,614 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ext/json/ext/parser/parser.c linguist-generated=true
java/src/json/ext/Parser.java linguist-generated=true
48 changes: 0 additions & 48 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end rescue nil
EXT_ROOT_DIR = 'ext/json/ext'
EXT_PARSER_DIR = "#{EXT_ROOT_DIR}/parser"
EXT_PARSER_DL = "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}"
RAGEL_PATH = "#{EXT_PARSER_DIR}/parser.rl"
EXT_PARSER_SRC = "#{EXT_PARSER_DIR}/parser.c"
EXT_GENERATOR_DIR = "#{EXT_ROOT_DIR}/generator"
EXT_GENERATOR_DL = "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}"
Expand Down Expand Up @@ -234,53 +233,6 @@ else
system 'ctags', *Dir['**/*.{rb,c,h,java}']
end

file EXT_PARSER_SRC => RAGEL_PATH do
cd EXT_PARSER_DIR do
if RAGEL_CODEGEN == 'ragel'
sh "ragel parser.rl -G2 -o parser.c"
else
sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2"
end
src = File.read("parser.c").gsub(/[ \t]+$/, '')
src.gsub!(/^static const int (JSON_.*=.*);$/, 'enum {\1};')
src.gsub!(/^(static const char) (_JSON(?:_\w+)?_nfa_\w+)(?=\[\] =)/, '\1 MAYBE_UNUSED(\2)')
src.gsub!(/0 <= ([\( ]+\*[\( ]*p\)+) && \1 <= 31/, "0 <= (signed char)(*(p)) && (*(p)) <= 31")
src[0, 0] = "/* This file is automatically generated from parser.rl by using ragel */"
File.open("parser.c", "w") {|f| f.print src}
end
end

desc "Generate diagrams of ragel parser (ps)"
task :ragel_dot_ps do
root = 'diagrams'
specs = []
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
for s in specs
if RAGEL_DOTGEN == 'ragel'
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tps -o#{root}/#{s}.ps"
else
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tps -o#{root}/#{s}.ps"
end
end
end

desc "Generate diagrams of ragel parser (png)"
task :ragel_dot_png do
root = 'diagrams'
specs = []
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
for s in specs
if RAGEL_DOTGEN == 'ragel'
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tpng -o#{root}/#{s}.png"
else
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tpng -o#{root}/#{s}.png"
end
end
end

desc "Generate diagrams of ragel parser"
task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ]

desc "Create the gem packages"
task :package do
sh "gem build json.gemspec"
Expand Down
Loading

0 comments on commit 03a7d4f

Please sign in to comment.