rubyXL icon indicating copy to clipboard operation
rubyXL copied to clipboard

Memoize the key of instance_variable_set

Open ooooooo-q opened this issue 4 years ago • 1 comments

I used memory_profiler to check where rubyXL was using a lot of memory, and fixed it.

"@#{v[:accessor]}" generates two String objects, which can be expensive depending on the access frequency, so we memoized it.

Result

  • write xlsx
    • https://github.com/ooooooo-q/rubyxl_memory_usage/blob/main/operate.rb#L8
  • read xlsx
    • https://github.com/ooooooo-q/rubyxl_memory_usage/blob/main/operate.rb#L42

rubyXL 3.4.20 (with Ruby 3.1.0)

  • write file
    • Total allocated: 16.41 MB (335760 objects)
    • Total retained: 6.58 kB (128 objects)
  • read file
    • Total allocated: 24.26 MB (340069 objects)
    • Total retained: 6.23 kB (57 objects)

memoize "@#{v[:accessor]}"

  • write file
    • Total allocated: 14.06 MB (276927 objects)
    • Total retained: 23.97 kB (558 objects)
  • read file
    • Total allocated: 21.82 MB (279137 objects)
    • Total retained: 23.62 kB (487 objects)

ooooooo-q avatar Jan 30 '22 00:01 ooooooo-q

I'm just curious, would an instance variable not work for memoizing? Why a class variable?

paddor avatar Jan 29 '25 13:01 paddor