Skip to content

Lazily initialize BasicVertex and MethodEntity to reduce GC pressure#435

Merged
mame merged 2 commits intoruby:masterfrom
sinsoku:lazy-initialize-vertex-and-method-entity
Apr 28, 2026
Merged

Lazily initialize BasicVertex and MethodEntity to reduce GC pressure#435
mame merged 2 commits intoruby:masterfrom
sinsoku:lazy-initialize-vertex-and-method-entity

Conversation

@sinsoku
Copy link
Copy Markdown
Collaborator

@sinsoku sinsoku commented Apr 13, 2026

BasicVertex#initialize and MethodEntity#initialize were eagerly
allocating collections that are often unused, contributing to GC
pressure (38.1% of CPU samples in tool/dog_bench.rb).

  • BasicVertex: remove eager @types_to_be_added = {} and use ||=
    on first write in on_type_added
  • MethodEntity: replace 4 eager Sets and 1 Hash with lazy accessor
    methods

Measured with tool/dog_bench.rb (stackprof):

  • GC overhead: 38.1% -> 23.7%
  • Total samples: 1132 -> 1001

sinsoku added 2 commits April 13, 2026 21:42
BasicVertex#initialize was allocating an empty Hash for
@types_to_be_added on every instantiation (4.3% of CPU samples).
Most vertices never use this field. Initialize it lazily with ||=
on first write in on_type_added.

Measured with tool/dog_bench.rb (stackprof):
- GC overhead: 38.1% -> 28.6%
- BasicVertex#initialize: 49 samples -> 0
MethodEntity#initialize was allocating 4 Sets and 1 Hash eagerly.
Use lazy accessor methods to defer allocation until first use.
Most MethodEntity instances are created during module resolution
but many fields are never populated.

Measured with tool/dog_bench.rb (stackprof, cumulative with prior commits):
- GC overhead: 28.6% -> 23.7%
- Total samples: 1038 -> 1001
Copy link
Copy Markdown
Member

@mame mame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give it a try!

@mame mame merged commit e00842d into ruby:master Apr 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants