|
| 1 | +# typed: ignore |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Benchmark the performance overhead of calling: |
| 5 | +# - A concrete implementation of an abstract method |
| 6 | +# - An inherited concrete implementation of an abstract method |
| 7 | +# - The error case of calling an unimplemented abstract method |
| 8 | + |
| 9 | +############################################# Results ############################################# |
| 10 | +# |
| 11 | +# ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [arm64-darwin23] |
| 12 | +# |
| 13 | +# ## Interpreter |
| 14 | +# |
| 15 | +# | Call to... | Regular impl | Inherited impl | Missing impl | |
| 16 | +# |-------------------|--------------------:|------------------------:|--------------------------:| |
| 17 | +# | sorbet-runtime | (same-ish) 23.02 ns | (2.70x slower) 57.30 ns | (1.13x slower) 472.86 ns | |
| 18 | +# | manual delegation | (same-ish) 22.18 ns | (2.07x slower) 44.90 ns | *415.36 ns* | |
| 19 | +# | type_toolkit | (same-ish) 22.56 ns | *22.03 ns* | (2.11x slower) 890.38 ns | |
| 20 | +# |
| 21 | +# ## YJIT# |
| 22 | +# | Call to... | Regular impl | Inherited impl | Missing impl | |
| 23 | +# |-------------------|--------------------:|-------------------------:|--------------------------:| |
| 24 | +# | sorbet-runtime | (same-ish) 1.63 ns | (21.41x slower) 34.91 ns | (1.10x slower) 447.59 ns | |
| 25 | +# | manual delegation | (same-ish) 1.63 ns | (7.15x slower) 11.66 ns | *405.84 ns* | |
| 26 | +# | type_toolkit | (same-ish) 1.67 ns | *1.63 ns* | (1.91x slower) 774.91 ns | |
| 27 | +# |
| 28 | +#################################################################################################### |
| 29 | + |
| 30 | +require "bundler" |
| 31 | +Bundler.require(:default, :benchmark) |
| 32 | + |
| 33 | +require "type_toolkit" |
| 34 | + |
| 35 | +module TypeKitDemo |
| 36 | + # Provides the concrete implementation of `m` |
| 37 | + class Parent |
| 38 | + def m1 = "Parent#m1" |
| 39 | + end |
| 40 | + |
| 41 | + module I |
| 42 | + interface! |
| 43 | + |
| 44 | + abstract def m1; end |
| 45 | + abstract def m2; end |
| 46 | + abstract def not_implemented; end |
| 47 | + end |
| 48 | + |
| 49 | + # Inherits the concrete implementation of `m` from DemoParentClass. |
| 50 | + class Child < Parent |
| 51 | + include I |
| 52 | + |
| 53 | + def m2 = "Child#m2" |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +module SorbetRuntimeDemo |
| 58 | + # Provides the concrete implementation of `m` |
| 59 | + class Parent |
| 60 | + def m1 = "Parent#m1" |
| 61 | + end |
| 62 | + |
| 63 | + module I |
| 64 | + extend T::Sig |
| 65 | + extend T::Helpers |
| 66 | + |
| 67 | + interface! |
| 68 | + |
| 69 | + sig { abstract.returns(String) } |
| 70 | + def m1; end |
| 71 | + |
| 72 | + sig { abstract.returns(String) } |
| 73 | + def m2; end |
| 74 | + |
| 75 | + sig { abstract.returns(String) } |
| 76 | + def not_implemented; end |
| 77 | + end |
| 78 | + |
| 79 | + # Inherits the concrete implementation of `m` from DemoParentClass. |
| 80 | + class Child < Parent |
| 81 | + include I |
| 82 | + |
| 83 | + def m2 = "Child#m2" |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +module ManualDelegationDemo |
| 88 | + class Parent |
| 89 | + def m1 = "Parent#m1" |
| 90 | + end |
| 91 | + |
| 92 | + module I |
| 93 | + def m1 = defined?(super) ? super : raise |
| 94 | + def m2 = defined?(super) ? super : raise |
| 95 | + def not_implemented = defined?(super) ? super : raise |
| 96 | + end |
| 97 | + |
| 98 | + # Inherits the concrete implementation of `m` from DemoParentClass. |
| 99 | + class Child < Parent |
| 100 | + include I |
| 101 | + |
| 102 | + def m2 = "Child#m2" |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +type_toolkit_object = TypeKitDemo::Child.new |
| 107 | +manual_delegation_object = ManualDelegationDemo::Child.new |
| 108 | +sorbet_runtime_object = SorbetRuntimeDemo::Child.new |
| 109 | + |
| 110 | +[:interpreter, :yjit].each do |mode| |
| 111 | + if mode == :yjit |
| 112 | + puts <<~MSG |
| 113 | +
|
| 114 | +
|
| 115 | + ================================================================================ |
| 116 | + Enabling YJIT... |
| 117 | + ================================================================================ |
| 118 | +
|
| 119 | +
|
| 120 | + MSG |
| 121 | + RubyVM::YJIT.enable |
| 122 | + end |
| 123 | + |
| 124 | + warmup = 5 |
| 125 | + time = 10 |
| 126 | + |
| 127 | + width = ["type_toolkit", "sorbet-runtime", "manual delegation"].max_by(&:length).length |
| 128 | + |
| 129 | + puts "Benchmark the performance of calling the concrete implementation directly..." |
| 130 | + Benchmark.ips do |x| |
| 131 | + x.config(warmup:, time:) |
| 132 | + |
| 133 | + x.report("type_toolkit".rjust(width)) do |times| |
| 134 | + i = 0 |
| 135 | + while (i += 1) < times |
| 136 | + type_toolkit_object.m2 |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + x.report("sorbet-runtime".rjust(width)) do |times| |
| 141 | + i = 0 |
| 142 | + while (i += 1) < times |
| 143 | + sorbet_runtime_object.m2 |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + x.report("manual delegation".rjust(width)) do |times| |
| 148 | + i = 0 |
| 149 | + while (i += 1) < times |
| 150 | + manual_delegation_object.m2 |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + x.compare! |
| 155 | + end |
| 156 | + |
| 157 | + puts "\n\nBenchmark the performance of calling the inherited concrete implementation..." |
| 158 | + Benchmark.ips do |x| |
| 159 | + x.config(warmup:, time:) |
| 160 | + |
| 161 | + x.report("type_toolkit".rjust(width)) do |times| |
| 162 | + i = 0 |
| 163 | + while (i += 1) < times |
| 164 | + type_toolkit_object.m1 |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + x.report("sorbet-runtime".rjust(width)) do |times| |
| 169 | + i = 0 |
| 170 | + while (i += 1) < times |
| 171 | + sorbet_runtime_object.m1 |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + x.report("manual delegation".rjust(width)) do |times| |
| 176 | + i = 0 |
| 177 | + while (i += 1) < times |
| 178 | + manual_delegation_object.m1 |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + x.compare! |
| 183 | + end |
| 184 | + |
| 185 | + puts "\n\nTest the performance of calling an unimplemented abstract method..." |
| 186 | + Benchmark.ips do |x| |
| 187 | + x.config(warmup:, time:) |
| 188 | + |
| 189 | + x.report("type_toolkit".rjust(width)) do |times| |
| 190 | + i = 0 |
| 191 | + while (i += 1) < times |
| 192 | + begin |
| 193 | + type_toolkit_object.not_implemented |
| 194 | + rescue AbstractMethodNotImplementedError # rubocop:disable Lint/SuppressedException |
| 195 | + end |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | + x.report("sorbet-runtime".rjust(width)) do |times| |
| 200 | + i = 0 |
| 201 | + while (i += 1) < times |
| 202 | + begin |
| 203 | + sorbet_runtime_object.not_implemented |
| 204 | + rescue NotImplementedError # rubocop:disable Lint/SuppressedException |
| 205 | + end |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | + x.report("manual delegation".rjust(width)) do |times| |
| 210 | + i = 0 |
| 211 | + while (i += 1) < times |
| 212 | + begin |
| 213 | + manual_delegation_object.not_implemented |
| 214 | + rescue StandardError # rubocop:disable Lint/SuppressedException |
| 215 | + end |
| 216 | + end |
| 217 | + end |
| 218 | + |
| 219 | + x.compare! |
| 220 | + end |
| 221 | +end |
0 commit comments