
Issue #19424 has been updated by jeremyevans0 (Jeremy Evans). Subject changed from Degradation in **Marshal load** only in Ruby 3.1.2 compared to 2.7.4 to Significant performance decreases in `OpenStruct#marshal_load` in Ruby 3.0 and 3.1 I was able to reproduce the issue. It seems to be limited to OpenStruct. Here's a simpler example: ```ruby require 'benchmark' require 'ostruct' n = 100_000 obj = OpenStruct.new( one: nil ) Benchmark.bm(20) do |x| klass = obj.class x.report("#{klass} dump") do n.times do Marshal.dump(obj) end end m = Marshal.dump(obj) x.report("#{klass} load") do n.times do Marshal.load(m) end end end ``` with results: ``` $ ruby27 -v t.rb && ruby30 -v t.rb && ruby31 -v t.rb ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-openbsd] user system total real OpenStruct dump 0.520000 0.000000 0.520000 ( 0.523775) OpenStruct load 0.370000 0.000000 0.370000 ( 0.376001) ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-openbsd] user system total real OpenStruct dump 0.540000 0.000000 0.540000 ( 0.537673) OpenStruct load 1.590000 0.000000 1.590000 ( 1.595759) ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-openbsd] user system total real OpenStruct dump 0.550000 0.000000 0.550000 ( 0.556089) OpenStruct load 2.240000 0.000000 2.240000 ( 2.243714) ``` Ruby 3.2 performance is about the same as Ruby 3.1. ---------------------------------------- Bug #19424: Significant performance decreases in `OpenStruct#marshal_load` in Ruby 3.0 and 3.1 https://bugs.ruby-lang.org/issues/19424#change-101781 * 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/