From 5175a3eed74bde28b1a3ddfe390e026200f58a0e Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 9 Aug 2023 12:37:18 -0500 Subject: [PATCH] Use a generic getter with __callee__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This modifies the getter definition to use a single method that uses `__callee__` to access the hash. def __get_callee__! @table[__callee__] end On versions of Ruby 2.0+ and JRuby 9.4.2+ __callee__ will return the aliased name, which in this case is the key for the table. This eliminates one of the Proc-based methods per field and also performs better on CRuby: Before: Warming up -------------------------------------- get 44.000 i/100ms set 46.000 i/100ms Calculating ------------------------------------- get 504.731 (± 8.5%) i/s - 2.508k in 5.024439s set 506.779 (± 1.4%) i/s - 2.576k in 5.084061s After: Warming up -------------------------------------- get 80.000 i/100ms set 65.000 i/100ms Calculating ------------------------------------- get 808.319 (± 0.7%) i/s - 4.080k in 5.047805s set 662.678 (± 0.9%) i/s - 3.315k in 5.002857s It also uses less memory; creating 100k OpenStruct in the same way as the benchmark uses 205.2MB before and 164MB after. --- lib/ostruct.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/ostruct.rb b/lib/ostruct.rb index a08561d..33ba1ab 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -221,16 +221,15 @@ def marshal_dump # :nodoc: # def new_ostruct_member!(name) # :nodoc: unless @table.key?(name) || is_method_protected!(name) + # use generic method with __callee__ for getter + singleton_class.alias_method(name, :__get_callee__!) + if defined?(::Ractor) - getter_proc = nil.instance_eval{ Proc.new { @table[name] } } setter_proc = nil.instance_eval{ Proc.new {|x| @table[name] = x} } - ::Ractor.make_shareable(getter_proc) ::Ractor.make_shareable(setter_proc) else - getter_proc = Proc.new { @table[name] } setter_proc = Proc.new {|x| @table[name] = x} end - define_singleton_method!(name, &getter_proc) define_singleton_method!("#{name}=", &setter_proc) end end @@ -254,6 +253,10 @@ def new_ostruct_member!(name) # :nodoc: end end + def __get_callee__! + @table[__callee__] + end + def freeze @table.freeze super