Skip to content
  • Jean Boussier's avatar
    87f9c3c6
    String#gsub! Elide MatchData allocation when we know it can't escape · 87f9c3c6
    Jean Boussier authored
    
    
    In gsub is used with a string replacement or a map that doesn't
    have a default proc, we know for sure no code can cause the MatchData
    to escape the `gsub` call.
    
    In such case, we still have to allocate a new MatchData because we
    don't know what is the lifetime of the backref, but for any subsequent
    match we can re-use the MatchData we allocated ourselves, reducing
    allocations significantly.
    
    This partially fixes [Misc #20652], except when a block is used,
    and partially reduce the performance impact of
    abc0304c / [Bug #17507]
    
    ```
    compare-ruby: ruby 3.5.0dev (2025-02-24T09:44:57Z master 5cf14639) +PRISM [arm64-darwin24]
    built-ruby: ruby 3.5.0dev (2025-02-24T10:58:27Z gsub-elude-match da966636e9) +PRISM [arm64-darwin24]
    warming up....
    
    |                 |compare-ruby|built-ruby|
    |:----------------|-----------:|---------:|
    |escape           |      3.577k|    3.697k|
    |                 |           -|     1.03x|
    |escape_bin       |      5.869k|    6.743k|
    |                 |           -|     1.15x|
    |escape_utf8      |      3.448k|    3.738k|
    |                 |           -|     1.08x|
    |escape_utf8_bin  |      6.361k|    7.267k|
    |                 |           -|     1.14x|
    ```
    
    Co-Authored-By: default avatarÉtienne Barrié <etienne.barrie@gmail.com>
    87f9c3c6
    String#gsub! Elide MatchData allocation when we know it can't escape
    Jean Boussier authored
    
    
    In gsub is used with a string replacement or a map that doesn't
    have a default proc, we know for sure no code can cause the MatchData
    to escape the `gsub` call.
    
    In such case, we still have to allocate a new MatchData because we
    don't know what is the lifetime of the backref, but for any subsequent
    match we can re-use the MatchData we allocated ourselves, reducing
    allocations significantly.
    
    This partially fixes [Misc #20652], except when a block is used,
    and partially reduce the performance impact of
    abc0304c / [Bug #17507]
    
    ```
    compare-ruby: ruby 3.5.0dev (2025-02-24T09:44:57Z master 5cf14639) +PRISM [arm64-darwin24]
    built-ruby: ruby 3.5.0dev (2025-02-24T10:58:27Z gsub-elude-match da966636e9) +PRISM [arm64-darwin24]
    warming up....
    
    |                 |compare-ruby|built-ruby|
    |:----------------|-----------:|---------:|
    |escape           |      3.577k|    3.697k|
    |                 |           -|     1.03x|
    |escape_bin       |      5.869k|    6.743k|
    |                 |           -|     1.15x|
    |escape_utf8      |      3.448k|    3.738k|
    |                 |           -|     1.08x|
    |escape_utf8_bin  |      6.361k|    7.267k|
    |                 |           -|     1.14x|
    ```
    
    Co-Authored-By: default avatarÉtienne Barrié <etienne.barrie@gmail.com>
Loading