Skip to content

Commit

Permalink
String#* takes one argument, and in that case, better not splat the arg
Browse files Browse the repository at this point in the history
because splatting the argument allocates an extra Array object.

benchmark
```ruby
s = 'a'.html_safe
Benchmark.ips do |x|
  x.report('') { s * 1 }
end
```

result
```
before
Warming up --------------------------------------
                       216.816k i/100ms
Calculating -------------------------------------
                          2.341M (± 2.0%) i/s -     11.708M in   5.002555s

after
Warming up --------------------------------------
                       315.118k i/100ms
Calculating -------------------------------------
                          3.704M (± 1.5%) i/s -     18.592M in   5.020261s
```
  • Loading branch information
amatsuda committed Jan 5, 2023
1 parent 62ca4b5 commit 5653d7d
Showing 1 changed file with 1 addition and 1 deletion.
Expand Up @@ -100,7 +100,7 @@ def +(other)
dup.concat(other)
end

def *(*)
def *(_)
new_string = super
new_safe_buffer = new_string.is_a?(SafeBuffer) ? new_string : SafeBuffer.new(new_string)
new_safe_buffer.instance_variable_set(:@html_safe, @html_safe)
Expand Down

0 comments on commit 5653d7d

Please sign in to comment.