Speed up variable access with SIMD scan and slot inline caches#191
Merged
Conversation
…ance-fa1178 # Conflicts: # src/ChibiRuby/MRubyState.Vm.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two layered optimizations for
Symbol-keyed variable lookup in the interpreter, worth +26% on optcarrot combined (18.9 → 23.8–24.0 fps, video checksum unchanged).1. SIMD scan for
VariableTableSymbolis a singleuint, so the key array can be searched with the vectorizedMemoryExtensions.IndexOfoverMemoryMarshal.Cast<Symbol, uint>instead of a scalar loop. optcarrot's PPU has 79 distinct ivars (CPU: 36), so the linear scan was a real cost: this alone is +10% on optcarrot.2. Slot inline caches for variable-access opcodes
Irep.VariableSlotCachestores aushortslot guess per symbol-pool index, used byGetIV/SetIV/GetGV/SetGVand theGetConstfast path; the trivial-getter fast path inSendgets the same treatment viaMethodCacheEntry.TrivialGetterSlot.Cached slots are verified guesses: every use re-checks
keys[slot] == symbol(TryGetAt/TrySetAt), so stale slots (afterremove_instance_variable, table growth, or a call site alternating between different layouts) can never read or write the wrong variable — they just fall back to the scan and re-learn the slot.Miss paths are deliberately
[MethodImpl(NoInlining)]: an earlier version with the miss path inlined into the dispatch-switch case bodies lost the entire SIMD win on optcarrot due to code-layout effects, despite winning on a microbenchmark.Numbers
Interleaved runs, alternating order, compared within round; optcarrot checksum 59662 identical in every configuration.
Tests
tests/ChibiRuby.Tests/ruby/test/variables.rb: slot shift afterremove_instance_variable, one call site alternating between different ivar layouts, reads through a warmed site on an empty table, global table growth mid-loop.Also adds a
--quick-script <file.rb> [iters] [warmups]wall-clock runner andruby/bm_ivar.rbto ChibiRuby.Benchmark.🤖 Generated with Claude Code