Repository navigation
Fewer allocations in hot library paths: int yield cache, C# Comparable#clamp, allocation-free [a, b].min/max, block-filled Array#fill - #94
Conversation
Integer#times, #upto, #step, Range#each over Fixnums, each_index and each_with_index yield a C# int counter, which boxed afresh on every iteration. BlockParam.Yield gets int overloads that take the box from ScriptingRuntimeHelpers.Int32ToObject, so a counter in -100..999 allocates nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW
It was Ruby in ruby4.rb, so every x.clamp(lo, hi) paid for a method scope, a splat array and a case on its size. The C# version follows MRI's cmp_clamp: the same errors, the Range and duck-typed range forms, arity -1, and <=> called through a dynamic site so a redefined <=> is still honoured. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW
The commonest way Ruby spells the smaller or larger of two values allocated an object[4] and a RubyArray per evaluation. As long as Array#min (#max) is still the library's own public method and no refinement is about, the answer is now worked out from the two elements with the same <=> calls in the same order (MRI's opt_newarray_send does the same). A redefinition, alias, visibility change or refinement sends it back to the call as written; the binding is re-checked against RubyModule.GlobalMethodVersion. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Fill went through IList's indexer one element at a time: an interface call, a frozen check and a bounds check per element. A RubyArray now fills its backing store with RubyArray.Fill, and Utils.Fill (whose TODO said "can be optimized for big repeatCount") is Array.Fill. Filling a 64,000-element array went from 440 to 235 microseconds. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW
AddMultiple stored the value one element at a time; it now uses the same block fill as Array#fill. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW
Profiling khasinski/doom's
bench/benchmark.rbwith dotnet-trace showed the renderer allocating ~11 MB per frame. In this environment that allocation costs far more than the dispatch around it. These commits cut the allocation, and the per-element work, in the generic (non-JIT) runtime paths:BlockParam.Yield(int …)overloads.times,upto,step,Range#eachover Fixnums,each_indexandeach_with_indexboxed their counter on every iteration. The new overloads take the box fromScriptingRuntimeHelpers.Int32ToObject, the cache for −100..999. Before this,Range#eachwas 7% of all allocated bytes.Comparable#clampin C#. It was Ruby inruby4.rb, so eachx.clamp(lo, hi)paid for a method scope, a splat array and acase. The new version follows MRI'scmp_clamp: the same errors, the Range and duck-typed range forms, arity −1, and<=>through a dynamic site.[a, b].min/[a, b].maxwithout the array, as MRI'sopt_newarray_senddoes.ArrayExtremeSiteanswers from the two elements with the same<=>calls asIListOps.GetExtreme, as long asArray#min(#max) is still the library's own public method and no refinement is active. A redefinition, alias, visibility change or refinement falls back to the call as written. The check is re-done wheneverRubyModule.GlobalMethodVersionchanges.Array#fillandArray.new(n, value)use a block fill.Fillwent throughIList's indexer one element at a time, andAddMultiplestored one element at a time. Both now go throughRubyArray.FillandUtils.Fill, which is nowArray.Fill(the old loop had a TODO saying "can be optimized for big repeatCount"). Filling a 64,000-element array went from 440 to 235 µs.Measurements (Release, net8.0, 4-core Linux VM, alternating runs against master
44891067)bench/benchmark.rb, median frame timeThe framebuffer checksum is identical in every run.
Tried and not kept
Array#dupthat skips theinitialize_dup/initialize_copydispatch. It made no measurable difference: the time is in allocating the copy.GCgen0size, server GC, non-concurrent GC,libclrgc). Pauses got shorter, but frame times did not improve.Testing
spec/languageplusspec/core/{array,comparable,integer,range,enumerable,float,kernel,string}. The failure list is identical to master's:Array#fill/Array.newwith too-large sizes, twoString#byteslice/#[]cases, and the existing Integer bignum-boundary cases.clampandfill: matrices of argument forms, errors, arity and identity. The output is identical to CRuby's.[a, b].min/max: a matrix of types, errors, evaluation order, a block, a count, a custom<=>, redefinition, alias and refinement. The output is identical to master's.Initializers.Generated.cswas regenerated withregen-initializers.sh. I added the twoReflectionCache.Generated.csentries by hand, in alphabetical order.🤖 Generated with Claude Code
https://claude.ai/code/session_01KPwZf6zTBynkVVtAVjXKpW