Skip to content
  • Jean Boussier's avatar
    f32d5071
    Elide string allocation when using `String#gsub` in MAP mode · f32d5071
    Jean Boussier authored
    If the provided Hash doesn't have a default proc, we know for
    sure that we'll never call into user provided code, hence the
    string we allocate to access the Hash can't possibly escape.
    
    So we don't actually have to allocate it, we can use a fake_str,
    AKA a stack allocated string.
    
    ```
    compare-ruby: ruby 3.5.0dev (2025-02-10T13:47:44Z master 3fb455ad) +PRISM [arm64-darwin23]
    built-ruby: ruby 3.5.0dev (2025-02-10T17:09:52Z opt-gsub-alloc ea5c28958f) +PRISM [arm64-darwin23]
    warming up....
    
    |                 |compare-ruby|built-ruby|
    |:----------------|-----------:|---------:|
    |escape           |      3.374k|    3.722k|
    |                 |           -|     1.10x|
    |escape_bin       |      5.469k|    6.587k|
    |                 |           -|     1.20x|
    |escape_utf8      |      3.465k|    3.734k|
    |                 |           -|     1.08x|
    |escape_utf8_bin  |      5.752k|    7.283k|
    |                 |           -|     1.27x|
    ```
    f32d5071
    Elide string allocation when using `String#gsub` in MAP mode
    Jean Boussier authored
    If the provided Hash doesn't have a default proc, we know for
    sure that we'll never call into user provided code, hence the
    string we allocate to access the Hash can't possibly escape.
    
    So we don't actually have to allocate it, we can use a fake_str,
    AKA a stack allocated string.
    
    ```
    compare-ruby: ruby 3.5.0dev (2025-02-10T13:47:44Z master 3fb455ad) +PRISM [arm64-darwin23]
    built-ruby: ruby 3.5.0dev (2025-02-10T17:09:52Z opt-gsub-alloc ea5c28958f) +PRISM [arm64-darwin23]
    warming up....
    
    |                 |compare-ruby|built-ruby|
    |:----------------|-----------:|---------:|
    |escape           |      3.374k|    3.722k|
    |                 |           -|     1.10x|
    |escape_bin       |      5.469k|    6.587k|
    |                 |           -|     1.20x|
    |escape_utf8      |      3.465k|    3.734k|
    |                 |           -|     1.08x|
    |escape_utf8_bin  |      5.752k|    7.283k|
    |                 |           -|     1.27x|
    ```
Loading