• G
    Avoid slowing down AR object initialization · 28d52c59
    Godfrey Chan 提交于
    2d73f5ae forces AR to enter the `define_attribute_methods` method whenever it
    instantiate a record from the `init_with` entry point. This is a potential
    performance hotspot, because `init_with` is called from all `find*` family
    methods, and `define_attribute_methods` is slow because it tries to acquire
    a lock on the mutex everytime it is entered.
    
    By using [DCL](http://en.wikipedia.org/wiki/Double-checked_locking), we can
    avoid grabbing the lock most of the time when the attribute methods are already
    defined (the common case). This is made possible by the fact that reading an
    instance variable is an atomic operation in Ruby.
    
    Credit goes to Aaron Patterson for pointing me to DCL and filling me in on the
    atomicity guarantees in Ruby.
    
    [*Godfrey Chan*, *Aaron Patterson*]
    28d52c59
attribute_methods.rb 16.3 KB