Skip to content
  • Jean Boussier's avatar
    4885a00a
    ActiveModel::Attribute: elide dup for immutable types · 4885a00a
    Jean Boussier authored
    `Attribute` is mostly immutable, but the deserialized value is
    memoized, and that value can potentially be mutable, and mutated.
    
    When the value is immutable however, we can share instances.
    
    Benchmark: https://gist.github.com/casperisfine/ae56bec1e7eecbff3a696b367e2bafa2
    
    Before:
    
    ```
    ruby 3.4.0dev (2024-12-12T09:30:43Z master 197a3efc75) +YJIT +PRISM [arm64-darwin23]
    Warming up --------------------------------------
            ActiveRecord     4.664k i/100ms
    Calculating -------------------------------------
            ActiveRecord     46.983k (± 1.0%) i/s   (21.28 μs/i) -    237.864k in   5.063292s
    ```
    
    After:
    
    ```
    ruby 3.4.0dev (2024-12-12T09:30:43Z master 197a3efc75) +YJIT +PRISM [arm64-darwin23]
    Warming up --------------------------------------
            ActiveRecord     5.404k i/100ms
    Calculating -------------------------------------
            ActiveRecord     53.502k (± 1.4%) i/s   (18.69 μs/i) -    270.200k in   5.051328s
    ```
    4885a00a
    ActiveModel::Attribute: elide dup for immutable types
    Jean Boussier authored
    `Attribute` is mostly immutable, but the deserialized value is
    memoized, and that value can potentially be mutable, and mutated.
    
    When the value is immutable however, we can share instances.
    
    Benchmark: https://gist.github.com/casperisfine/ae56bec1e7eecbff3a696b367e2bafa2
    
    Before:
    
    ```
    ruby 3.4.0dev (2024-12-12T09:30:43Z master 197a3efc75) +YJIT +PRISM [arm64-darwin23]
    Warming up --------------------------------------
            ActiveRecord     4.664k i/100ms
    Calculating -------------------------------------
            ActiveRecord     46.983k (± 1.0%) i/s   (21.28 μs/i) -    237.864k in   5.063292s
    ```
    
    After:
    
    ```
    ruby 3.4.0dev (2024-12-12T09:30:43Z master 197a3efc75) +YJIT +PRISM [arm64-darwin23]
    Warming up --------------------------------------
            ActiveRecord     5.404k i/100ms
    Calculating -------------------------------------
            ActiveRecord     53.502k (± 1.4%) i/s   (18.69 μs/i) -    270.200k in   5.051328s
    ```
Loading