Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b08dacf

Browse files
jeremyevansEric WongXrXrmameko1
authored
Optimize dynamic string interpolation for symbol/true/false/nil/0-9
This provides a significant speedup for symbol, true, false, nil, and 0-9, class/module, and a small speedup in most other cases. Speedups (using included benchmarks): :symbol :: 60% 0-9 :: 50% Class/Module :: 50% nil/true/false :: 20% integer :: 10% [] :: 10% "" :: 3% One reason this approach is faster is it reduces the number of VM instructions for each interpolated value. Initial idea, approach, and benchmarks from Eric Wong. I applied the same approach against the master branch, updating it to handle the significant internal changes since this was first proposed 4 years ago (such as CALL_INFO/CALL_CACHE -> CALL_DATA). I also expanded it to optimize true/false/nil/0-9/class/module, and added handling of missing methods, refined methods, and RUBY_DEBUG. This renames the tostring insn to anytostring, and adds an objtostring insn that implements the optimization. This requires making a few functions non-static, and adding some non-static functions. This disables 4 YJIT tests. Those tests should be reenabled after YJIT optimizes the new objtostring insn. Implements [Feature #13715] Co-authored-by: Eric Wong <[email protected]> Co-authored-by: Alan Wu <[email protected]> Co-authored-by: Yusuke Endoh <[email protected]> Co-authored-by: Koichi Sasada <[email protected]>
1 parent 4adb012 commit b08dacf

20 files changed

+240
-37
lines changed

benchmark/vm_dstr_ary.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i = 0
2+
x = y = []
3+
while i<6_000_000 # benchmark loop 2
4+
i += 1
5+
str = "foo#{x}bar#{y}baz"
6+
end

benchmark/vm_dstr_bool.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
i = 0
2+
x = true
3+
y = false
4+
while i<6_000_000 # benchmark loop 2
5+
i += 1
6+
str = "foo#{x}bar#{y}baz"
7+
end

benchmark/vm_dstr_class_module.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
i = 0
2+
class A; end unless defined?(A)
3+
module B; end unless defined?(B)
4+
x = A
5+
y = B
6+
while i<6_000_000 # benchmark loop 2
7+
i += 1
8+
str = "foo#{x}bar#{y}baz"
9+
end
10+

benchmark/vm_dstr_digit.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
i = 0
2+
x = 0
3+
y = 9
4+
while i<6_000_000 # benchmark loop 2
5+
i += 1
6+
str = "foo#{x}bar#{y}baz"
7+
end

benchmark/vm_dstr_int.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
i = 0
2+
while i<6_000_000 # benchmark loop 2
3+
i += 1
4+
str = "foo#{i}bar#{i}baz"
5+
end

benchmark/vm_dstr_nil.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i = 0
2+
x = y = nil
3+
while i<6_000_000 # benchmark loop 2
4+
i += 1
5+
str = "foo#{x}bar#{y}baz"
6+
end

benchmark/vm_dstr_obj.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i = 0
2+
x = y = Object.new
3+
while i<6_000_000 # benchmark loop 2
4+
i += 1
5+
str = "foo#{x}bar#{y}baz"
6+
end

benchmark/vm_dstr_obj_def.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
i = 0
2+
o = Object.new
3+
def o.to_s; -""; end
4+
x = y = o
5+
while i<6_000_000 # benchmark loop 2
6+
i += 1
7+
str = "foo#{x}bar#{y}baz"
8+
end

benchmark/vm_dstr_str.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i = 0
2+
x = y = ""
3+
while i<6_000_000 # benchmark loop 2
4+
i += 1
5+
str = "foo#{x}bar#{y}baz"
6+
end

benchmark/vm_dstr_sym.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i = 0
2+
x = y = :z
3+
while i<6_000_000 # benchmark loop 2
4+
i += 1
5+
str = "foo#{x}bar#{y}baz"
6+
end

0 commit comments

Comments
 (0)