ruby / rbs

Type Signature for Ruby
Other
1.94k stars 211 forks source link

Dedup empty arrays in parsing #1950

Closed pocke closed 4 weeks ago

pocke commented 1 month ago

This patch dedups empty arrays allocated during parsing.

Currently, RBS allocates many arrays during parsing. It consumes a lot of memory.

This patch reduces memory usage by deduping empty arrays.

BTW, I'm also working on deduping empty Hashes. It caused a performance problem. Therefore this PR focuses on Array and I'll make another PR for Hash.

Profiling

I've checked the memory usage by benchmark/memory_new_rails_env.rb script.

before:

Total allocated: 158965078 bytes (1615620 objects)
Total retained:  56607852 bytes (566708 objects)

(snip)

allocated memory by class
-----------------------------------
  81452592  Hash
  20455584  Array
  20114192  RBS::Location
  13382510  String

after:

Total allocated: 153021766 bytes (1466984 objects)
Total retained:  52321620 bytes (459558 objects)

(snip)

allocated memory by class
-----------------------------------
  81456912  Hash
  20114856  RBS::Location
  14508496  Array
  13380142  String

It reduced ~8% retained memory.

I've confirmed steep check works well with this patch.