[ruby-core:126564] [Ruby Bug#22290] Adding instance variables to anonymous object may bloat all future classes
Issue #22290 has been reported by byroot (Jean Boussier). ---------------------------------------- Bug #22290: Adding instance variables to anonymous object may bloat all future classes https://bugs.ruby-lang.org/issues/22290 * Author: byroot (Jean Boussier) * Status: Open * Backport: 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED ---------------------------------------- Reproduction script: ```ruby o = Object.new 10.times do |i| o.instance_variable_set("@a_#{i}", 1) end class Test def initialize(a, b) @a = a @b = b end end require 'objspace' p ObjectSpace.memsize_of(Test.new(1, 2)) ``` Expected: ``` 40 # or 32 on 4.1.dev ``` Actual: ``` 160 ``` ### Explanation `Class` records the max number of instance variables any of its instances held (`max_iv_count`). When inheriting from a class, `max_iv_count` is copied in the child class. If you add instance variables to an instance of `Object`, any future direct children of `Object` will start with a larger than needed `max_iv_count` and will cause Ruby to potentially allocate much larger objects than needed. All versions since 3.2 are affected, the issue was fixed on master a couple months. I believe we should backport the fix (I'm working on it), because otherwise some un-proper code running early in the process life-cycle can cause significant memory waste. -- https://bugs.ruby-lang.org/
Issue #22290 has been updated by byroot (Jean Boussier). Status changed from Open to Closed Backport pull requests: - 4.0: https://github.com/ruby/ruby/pull/18629 - 3.4: https://github.com/ruby/ruby/pull/18630 ---------------------------------------- Bug #22290: Adding instance variables to anonymous object may bloat all future classes https://bugs.ruby-lang.org/issues/22290#change-118774 * Author: byroot (Jean Boussier) * Status: Closed * Backport: 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED ---------------------------------------- Reproduction script: ```ruby o = Object.new 10.times do |i| o.instance_variable_set("@a_#{i}", 1) end class Test def initialize(a, b) @a = a @b = b end end require 'objspace' p ObjectSpace.memsize_of(Test.new(1, 2)) ``` Expected: ``` 40 # or 32 on 4.1.dev ``` Actual: ``` 160 ``` ### Explanation `Class` records the max number of instance variables any of its instances held (`max_iv_count`). When inheriting from a class, `max_iv_count` is copied in the child class. If you add instance variables to an instance of `Object`, any future direct children of `Object` will start with a larger than needed `max_iv_count` and will cause Ruby to potentially allocate much larger objects than needed. All versions since 3.2 are affected, the issue was fixed on master a couple months. I believe we should backport the fix (I'm working on it), because otherwise some un-proper code running early in the process life-cycle can cause significant memory waste. -- https://bugs.ruby-lang.org/
Issue #22290 has been updated by nagachika (Tomoyuki Chikanaga). Backport changed from 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: WONTFIX, 3.4: DONE, 4.0: DONE Merged backport PRs. ---------------------------------------- Bug #22290: Adding instance variables to anonymous object may bloat all future classes https://bugs.ruby-lang.org/issues/22290#change-118789 * Author: byroot (Jean Boussier) * Status: Closed * Backport: 3.3: WONTFIX, 3.4: DONE, 4.0: DONE ---------------------------------------- Reproduction script: ```ruby o = Object.new 10.times do |i| o.instance_variable_set("@a_#{i}", 1) end class Test def initialize(a, b) @a = a @b = b end end require 'objspace' p ObjectSpace.memsize_of(Test.new(1, 2)) ``` Expected: ``` 40 # or 32 on 4.1.dev ``` Actual: ``` 160 ``` ### Explanation `Class` records the max number of instance variables any of its instances held (`max_iv_count`). When inheriting from a class, `max_iv_count` is copied in the child class. If you add instance variables to an instance of `Object`, any future direct children of `Object` will start with a larger than needed `max_iv_count` and will cause Ruby to potentially allocate much larger objects than needed. All versions since 3.2 are affected, the issue was fixed on master a couple months. I believe we should backport the fix (I'm working on it), because otherwise some un-proper code running early in the process life-cycle can cause significant memory waste. -- https://bugs.ruby-lang.org/
participants (2)
-
byroot (Jean Boussier) -
nagachika (Tomoyuki Chikanaga)