The Curious Case of the Bad Clone
summary
A two-line Sorbet sig triggers a 150% slowdown by causing about 1,600 method_added calls during cloning. Investigation uses CPU profiles, bundle open edits, puts and binding.pry, rails runner, and object space TraceObjectAllocations to find an unnamed singleton-class clone path. A minimal reproducer and Rails bug-template script isolates the issue, a PR with Alan Wu changes the clone/attach guard, and the fix lands in Ruby 3.0 (and is backported to 2.7).
AI-written summary · about
chapters (12)
- 0:00Introduction
- 1:43What is Sorbet
- 3:11Runtime magic with method_added
- 7:27Performance regression
- 11:28Mystery of cloned methods
- 14:02Kernel.clone and Singleton behavior
- 20:47Root cause in Rails sharding
- 27:37after_find and object cloning
- 33:15Reproducing the bug
- 41:41Singleton class cloning
- 46:24Fixing Singleton class cloning
- 50:21Verifying fix in Ruby 3
related talks
28mWho You Gonna Call: Analyzing the Run-time Call-Site Behavior of Ruby Applications
34mTool belt of a seasoned bug hunter
29mHow to Performance
36mThere and Back Again
41mSpeed up Rails, Speed up Your Code
29mThe Case of the Missing Method: A Ruby Mystery Story
52mHow Are Method Calls Formed?
30mRuby Objects: A Walkabout
