
Issue #19424 has been updated by jeremyevans0 (Jeremy Evans). byroot (Jean Boussier) wrote in #note-5:
That Ruby 3.0+ difference is likely due to the `if defined? Ractor` branching in the gem. On 3.0 it has to do even more work to make the defined methods Ractor safe.
Presumably that is not something we would want to revert. I checked and performance with the 2.7 version of ostruct remains the same in Ruby 3.0 and 3.1: ``` ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-openbsd] ["OpenStruct::VERSION", "0.2.0"] user system total real OpenStruct dump 0.540000 0.000000 0.540000 ( 0.525649) OpenStruct load 0.400000 0.000000 0.400000 ( 0.374714) OpenStruct new 0.120000 0.000000 0.120000 ( 0.122805) ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-openbsd] ["OpenStruct::VERSION", "0.2.0"] user system total real OpenStruct dump 0.520000 0.000000 0.520000 ( 0.533818) OpenStruct load 0.500000 0.000000 0.500000 ( 0.489260) OpenStruct new 0.130000 0.000000 0.130000 ( 0.132554) ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-openbsd] ["OpenStruct::VERSION", "0.2.0"] user system total real OpenStruct dump 0.560000 0.000000 0.560000 ( 0.560805) OpenStruct load 0.510000 0.000000 0.510000 ( 0.515713) OpenStruct new 0.150000 0.000000 0.150000 ( 0.134401) ``` The only issue is the version of ostruct that shipped with Ruby 2.7 (0.2.0) is not available as a gem, so users that want to use that version to improve load/new performance cannot do so. However, the ostruct 0.1.0 release that shipped with Ruby 2.6 is available as a gem and offers similar load/new performance. ---------------------------------------- Bug #19424: Significant performance decreases in `OpenStruct#marshal_load` in Ruby 3.0 and 3.1 https://bugs.ruby-lang.org/issues/19424#change-101792 * Author: sumitdey035 (Sumit Dey) * Status: Open * Priority: Normal * ruby -v: 3.1.2 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I can see degradation in **Marshal load** only in Ruby 3.1.2 compared to 2.7.4 Processing time increased by 200%(2.4 sec to 4.3 sec) Memory allocation increased by 600%(6500001 to 39000004) ``` require 'benchmark' require 'ostruct' N_OJ = 500_000 ex_HASH = { 'one' => 1, 'array' => [ true, false ] } ex_JSON = '{ "one": 1, "array": [ true, false ] }' ex_STRUCT = OpenStruct.new( one: 1, hash: ex_HASH, array: [ true, false ] ) ex_MARSHAL = "\x04\bU:\x0FOpenStruct{\b:\bonei\x06:\thash{\aI\"\bone\x06:\x06ETi\x06I\"\narray\x06;\bT[\aTF:\narray[\aTF" "-----------------Ruby #{system("rbenv version")}----------------" Benchmark.bm(20) do |x| x.report('native marshal dump') do N_OJ.times do y = Marshal.dump(ex_STRUCT) end end x.report('native marshal load') do N_OJ.times do y = Marshal.load(ex_MARSHAL) end end start_memory = GC.stat[:total_allocated_objects] N_OJ.times do y = Marshal.dump(ex_STRUCT) end end_memory = GC.stat[:total_allocated_objects] print "Marshal dump memory allocation- #{end_memory - start_memory}\n" start_memory = GC.stat[:total_allocated_objects] N_OJ.times do y = Marshal.load(ex_MARSHAL) end end_memory = GC.stat[:total_allocated_objects] print "Marshal load memory allocation- #{end_memory - start_memory}\n" end``` **Benchmark and Memory consumption result**  ---Files-------------------------------- Screenshot 2023-02-07 at 1.04.49 PM.png (184 KB) -- https://bugs.ruby-lang.org/