Jean Boussier 2025-12-03 13:50:35 +0000 (Wed, 03 Dec 2025)
New Revision: fcf3939780
https://github.com/ruby/ruby/commit/fcf3939780
Log:
Speedup TypedData_Get_Struct
While profiling `Monitor#synchronize` and `Mutex#synchronize`
I noticed a fairly significant amount of time spent in
`rb_check_typeddata`.
By implementing a fast path that assumes the object is valid
and that can be inlined, it does make a significant difference:
Before:
```
Mutex 13.548M (± 3.6%) i/s (73.81 ns/i) - 68.566M in 5.067444
Monitor 10.497M (± 6.5%) i/s (95.27 ns/i) - 52.529M in 5.032698s
```
After:
```
Mutex 20.887M (± 0.3%) i/s (47.88 ns/i) - 106.021M in 5.075989s
Monitor 16.245M (±13.3%) i/s (61.56 ns/i) - 80.705M in 5.099680s
```
```ruby
require 'bundler/inline'
gemfile do
gem "benchmark-ips"
end
mutex = Mutex.new
require "monitor"
monitor = Monitor.new
Benchmark.ips do |x|
x.report("Mutex") { mutex.synchronize { } }
x.report("Monitor") { monitor.synchronize { } }
end
```
Modified files:
include/ruby/internal/core/rtypeddata.h