Repository navigation
Description
Measured
MRI's rb_any_to_s always prints the object's address; rbgo's Kernel#to_s
never does. #inspect prints it on both sides, so the two disagree with each
other within rbgo.
class Plain; end
Plain.new.to_s # ruby 4.0.5 "#<Plain:0x00007f...>" rbgo "#<Plain>"
Plain.new.inspect # ruby 4.0.5 "#<Plain:0x00007f...>" rbgo "#<Plain:0x0000...>"
Object.new.to_s # ruby 4.0.5 "#<Object:0x...>" rbgo "#<Object>"
Thread::Mutex.new.to_s # ruby 4.0.5 "#<Thread::Mutex:0x...>" rbgo "#<Thread::Mutex>"It is general — every object that inherits Kernel#to_s is affected, whatever
its Go representation — and it is present on main (325bea5), not introduced
by anything in flight.
#inspect already has the address logic (defaultObjectInspect,
builtins.go:6670), which prints #<Name:0x%016x …ivars>; RObject.ToS()
(object_model.go:673) returns "#<" + class.name + ">" and has no access to
the VM, so it cannot reach refID. The fix therefore belongs in the
Object#to_s native (builtins.go:1024), which does have the VM, rather than in
ToS().
Two hazards to measure before changing it
- Receivers that reach
Object#to_sand should NOT get an address.nil,
true/falseand anything whose class does not define its own#to_sneed
checking one by one;nil.to_sis""in MRI, so a blanket
#<Class:0xADDR>in that native would be wrong for it. - Built-in value subclasses.
class MyStr < String; endrenders as its
wrapped value in MRI, andRObject.ToS()already handles that via
o.builtin; that arm must survive.
So this wants its own census — which receivers actually land on Object#to_s —
before the one-line change, not after.
It masks a second defect
ObjectSpace::WeakMap#to_s and WeakKeyMap#to_s are over-defined in rbgo:
objectspace_weak.go aliases each to #inspect, where MRI defines neither
(#owner is Kernel for both, and #to_s == #inspect is false there).
ObjectSpace::WeakKeyMap.new.to_s
# ruby 4.0.5 #<ObjectSpace::WeakKeyMap:0x...>
# rbgo #<ObjectSpace::WeakKeyMap:0x... size=0>Removing those two aliases is correct and was prepared as part of #754, but it
had to be put back: without the address in Kernel#to_s, dropping the alias
fixes #owner and loses the 0x..., trading one divergence for another. The
call sites now carry a comment pointing here. Once Kernel#to_s prints the
address, the removal becomes a strict improvement and should land with it.
Found while measuring #754.
🤖 Generated with Claude Code
Activity
tannevaled commentedon Oct 3, 2026
The census this issue asked for, run
28 receivers,
#ownerof#to_sand its text, againstruby 4.0.5. It settlesthe two hazards and finds two more things.
Hazard 1 is real: four kinds of receiver reach
Kernel#to_shere that should not#to_s.ownerruby#to_s.ownerrbgo#to_snilNilClassKernel""bothtrueTrueClassKernel"true"bothfalseFalseClassKernel"false"both(1..2)RangeKernel"1..2"bothThe text is right in every case, because
Object#to_sisobject.NewString(self.ToS())and each Go type'sToS()already knows what toprint. So putting
#<Class:0x…>into that one native unconditionally would turnnil.to_sinto"#<NilClass:0x…>". The address form cannot go thereblanket; those four classes need their own
#to_sfirst, which also fixes#ownerfor them.Hazard 2 is not a problem
A built-in value subclass is already handled and already matches:
The receivers that genuinely want
rb_any_to_sThese are the ones where MRI reports
owner=Kerneltoo, so the address form isexactly what they should get:
BasicObject.new.to_sraisesNoMethodErroron both, correctly.Two more divergences found by the same census
Proc#to_sloses the address and the source location. It has its own#to_son both sides (owner=Proc), so this is separate from the above:Enumerator#to_sis over-defined. MRI reportsowner=Kernelfor it — soit is the plain address form — while rbgo gives Enumerator its own, aliased to
#inspect:That is the same over-definition shape as the
ObjectSpaceweak maps describedabove, and it should be removed in the same change — after the address is in
place, not before, for the same reason.
So the order of work is
NilClass,TrueClass,FalseClassandRangetheir own#to_s(texts unchanged,
#ownerfixed).rb_any_to_s's#<Class:0x%016x>intoObject#to_s, keeping theo.builtinarm.Proc#to_sits address and source location.#to_saliases —ObjectSpace::WeakMap,ObjectSpace::WeakKeyMap,Enumerator— whichbecome strict improvements only once step 2 has landed.
Step 1 has to come before step 2 or
nil.to_sbreaks; step 4 has to come afterstep 2 or three texts lose their address. Nothing here can be done in one blanket
edit, which is what the census was for.
Everything above measured on
main@325bea5pluswave55-alias-sites(neither changes any row).
fix(vm): a built-in's second name is its own original_name
tannevaled commentedon Oct 3, 2026
Step 1 is on
wave56-any-to-s; steps 2–4 were built, measured and backed outStep 1 — giving
NilClass,TrueClass,FalseClassandRangetheir own#to_s— is pushed. No text changes; only#owner,#methodandinstance_methods(false)move, and all four now matchruby 4.0.5.Steps 2–4 were implemented in full and reverted, because measuring them turned up
three things the census above could not see. The patch is kept; this is what has
to be dealt with first.
a. 20 existing tests pin the address-less text — and they are the ones that are wrong
With
Kernel#to_sprinting the address,go test ./...fails in 20 testfunctions, every one of the same shape:
In each,
#inspectis defined by the shim and unchanged, while#to_sfell toKernel. A class that defines only#inspectgetsrb_any_to_sfor#to_sinreal Ruby, with the address — so the new text is right and the expectations
are pinning rbgo's old behaviour. They have to be updated, and made
address-tolerant to do it, which is 20 test functions of churn that belongs in its
own change rather than riding along.
b.
putshas a fast path that bypasses#to_sGiving
Proc#to_sMRI's form exposed this:Two different answers for the same object in the same run. MRI prints the same
thing for both. A user-defined
#to_sis dispatched correctly(
puts/print/interpolation on a class withdef to_sall give"CUSTOM"), sothis is a built-in fast path, invisible until the native
#to_sand the GoToS()were made to disagree.Proc#to_scannot be made MRI-correct while itexists.
c. A symbol proc is not reported as a lambda
Separate, pre-existing, found in the same probe:
(The missing space before
(&:was also measured and is part of the revertedpatch; MRI has none.)
Revised order
nil/true/false/Rangeown their#to_s.putsfast path, or give the Go-levelToS()what it needs, soputs xandx.to_scannot disagree. Blocks step 4.Kernel#to_sgets#<Class:0x%016x>;Proc#to_sgets its address, sourcelocation and
(lambda); the three over-defined#to_saliases(
ObjectSpace::WeakMap,WeakKeyMap,Enumerator) come out.One more note for step 4: the "nothing better to say" test cannot be
self.ToS() == "#<" + classOf(self).name + ">". That form was tried and itsilently skipped
Thread::Mutex, whose value renders as#<Thread::Mutex>while its class answers
"Mutex"to#name— filed as #759. A structural test(the inside is a constant path) works and admits
#<Thread::Mutex>whileexcluding
#<struct a=1>and#<Enumerator: [1, 2]:each>.With that structural test, 24 of the 27 census rows match. The 3 that do not are
all separate: #758 (a block in a multi-line literal reports the wrong line, which
Proc#to_sthen prints),Method#to_srendering(*)where MRI renders(),and the
Enumeratorover-definition in step 4.fix(vm): a built-in's second name is its own original_name (#757)
fix(vm): nil, true, false and Range own their #to_s (#761)
tannevaled commentedon Oct 4, 2026
Step 2 is done, and it was bigger than "a
putsfast path"The note above said
putsbypasses#to_sand blocks step 4. Measuredproperly, the bypass is
displayStr— rbgo'srb_obj_as_string— dispatching#to_sonly for a user object and taking the Go-levelToS()for everybuilt-in value type. It shows on seven of the nine core classes on
main,through
#putsand#print:Float,Symbol,Hash,NilClass,TrueClass,Rangethe same;Arrayonly under
#print(MRI's#putsrecurses into an array element-wise and neverasks it for
#to_s). Fixed in #764.It also found a defect in #760, before that merged
The same probe, on a String:
MRI's
objtostringreturns a String untouched, subclasses included, so aredefined
String#to_schanges neither an interpolation nor aputs. #760 hadbeen emitting
dup; send :to_s; any_to_string, which answers"SUB". The threeopcodes are now one —
OpObjToString, MRI'sobjtostringplus itsanytostringfallback — and the subclass is a test row. That is a defect I putin a PR and took out before it landed, found only because the next defect in the
same area made it visible.
What remains, and why step 3 is now the gate
— fix(vm): nil, true, false and Range own their #to_s #761, merged.nil/true/false/Rangeown their#to_s— fix(vm): puts and print must dispatch #to_s #764. The blocker on step 4 isputs/printmust dispatch#to_sgone: there is no longer a second route that can disagree with the method.
next thing, and now the only thing in front of step 4.
Kernel#to_sgets#<Class:0x%016x>;Proc#to_sits address, sourcelocation and
(lambda); the three over-defined#to_saliases(
ObjectSpace::WeakMap,WeakKeyMap,Enumerator) come out.One number for step 4's design, measured while doing step 2: dispatching
#to_scosts ~114 ns/op against 2.1 ns/op for the Go-level
ToS()— 54× on thatstep, but ~22 ms per 200 000 calls, which is invisible next to the write.
Five runs of a 200 000-
putsloop gavemain0.61–0.77 s and the branch0.64–0.71 s: overlapping distributions, no program-level effect. So step 4 does
not need a cache either, unless a workload shows one.
test(vm): record the nineteen ToS() the display path no longer takes
fix(vm): puts and print must dispatch #to_s (#764)