Class: Hone::Patterns::DynamicIvar

Inherits:
Base
  • Object
show all
Defined in:
lib/hone/patterns/dynamic_ivar.rb

Overview

Pattern: instance_variable_set("@#name", value)

Dynamic instance variable creation causes object shape transitions. YJIT optimizes based on stable object shapes - when shapes change, it must deoptimize. Use a Hash for dynamic data instead.

Impact: Significant with YJIT, none without

Instance Attribute Summary

Attributes inherited from Base

#findings

Instance Method Summary collapse

Methods inherited from Base

#add_finding, inherited, #initialize, scan_file

Constructor Details

This class inherits a constructor from Hone::Patterns::Base

Instance Method Details

#visit_call_node(node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hone/patterns/dynamic_ivar.rb', line 16

def visit_call_node(node)
  super

  return unless node.name == :instance_variable_set

  first_arg = node.arguments&.arguments&.first
  return unless first_arg
  return unless dynamic_ivar_name?(first_arg)

  add_finding(
    node,
    message: "Dynamic instance_variable_set causes object shape transitions, hurting YJIT. Use a Hash for dynamic data instead.",
    speedup: "Enables better YJIT optimization"
  )
end