From d5ae7965b75ff98e8425f39043171ad4df0a0106 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 29 Feb 2024 17:50:27 +0100 Subject: [PATCH] [ruby/prism] Lazily create Location objects in Prism::Serialize::Loader#load_location * Following the changes in #2428. * PRISM_FFI_BACKEND=true ruby -v -Ilib -rprism -rbenchmark -e '10.times { p Benchmark.realtime { Dir.glob("lib/**/*.rb") { |f| Prism.parse_file(f) } } }' ruby 3.3.0: 0.255 => 0.210 ruby 3.3.0 YJIT: 0.150 => 0.120 https://github.com/ruby/prism/commit/fabf809bbf --- prism/templates/lib/prism/serialize.rb.erb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/prism/templates/lib/prism/serialize.rb.erb b/prism/templates/lib/prism/serialize.rb.erb index 5e195d9bbc..4370363773 100644 --- a/prism/templates/lib/prism/serialize.rb.erb +++ b/prism/templates/lib/prism/serialize.rb.erb @@ -89,18 +89,18 @@ module Prism def load_comments Array.new(load_varuint) do case load_varuint - when 0 then InlineComment.new(load_location) - when 1 then EmbDocComment.new(load_location) + when 0 then InlineComment.new(load_location_object) + when 1 then EmbDocComment.new(load_location_object) end end end def load_metadata comments = load_comments - magic_comments = Array.new(load_varuint) { MagicComment.new(load_location, load_location) } - data_loc = load_optional_location - errors = Array.new(load_varuint) { ParseError.new(load_embedded_string, load_location, load_error_level) } - warnings = Array.new(load_varuint) { ParseWarning.new(load_embedded_string, load_location, load_warning_level) } + magic_comments = Array.new(load_varuint) { MagicComment.new(load_location_object, load_location_object) } + data_loc = load_optional_location_object + errors = Array.new(load_varuint) { ParseError.new(load_embedded_string, load_location_object, load_error_level) } + warnings = Array.new(load_varuint) { ParseWarning.new(load_embedded_string, load_location_object, load_warning_level) } [comments, magic_comments, data_loc, errors, warnings] end @@ -214,6 +214,10 @@ module Prism end def load_location + (load_varuint << 32) | load_varuint + end + + def load_location_object Location.new(source, load_varuint, load_varuint) end @@ -221,6 +225,10 @@ module Prism load_location if io.getbyte != 0 end + def load_optional_location_object + load_location_object if io.getbyte != 0 + end + def load_constant(index) constant = constant_pool[index]