Skip to content

ZJIT: Idea: Optimize lazy initialization pattern #903

@tekknolagi

Description

@tekknolagi

@tenderworks had the idea that maybe lazy initialization of an ivar should be a single VM instruction. For example, right now,

class C
  attr_accessor :a
  def a
    @a ||= 5
  end
end

compiles into

== disasm: #<ISeq:a@tmp/lazyinit.rb:3 (3,2)-(5,5)>
0000 getinstancevariable                    :@a, <is:0>               (   4)[LiCa]
0003 dup
0004 branchif                               13
0006 pop
0007 putobject                              5
0009 dup
0010 setinstancevariable                    :@a, <is:1>
0013 leave

a) that's a lot of code and b) would it be possible to get better IC hit behavior? Right now this pattern has IC misses the first two calls (first empty, second is filled but wrong)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions