From 1f1ad418b6ef286adc5b8959c274c691eb791705 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 1 Nov 2026 11:08:20 +0100 Subject: [PATCH 1/2] vm: take the uninitialized-global gate off the hot path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #856 put `vm.globals["$VERBOSE"]` in front of **every** Ruby-level global read, in the interpreter and in both AOT tiers: ```go if !vm.warningEnabled() || strings.HasPrefix(name, "#") { return vm.gvar(name) } ``` The comment I shipped with it said "one map lookup and false in any ordinary run". That was true and beside the point. `getgvar` is a hot instruction, the lookup was unconditional, and a cost on a hot path is not excused by being small — particularly one that buys nothing on the reads that have a value. ## The reorder Every test now sits behind `uninitialized`, the flag `gvarResolve` already returns: ```go v, uninitialized := vm.gvarResolve(name) if !uninitialized { return v } ``` The resolution happens exactly once, as it did before #856 existed, and a read that found a value returns through the same path it always did. `$stdout`, `$LOAD_PATH`, `$~`, `$1` and every assigned user global now pay **nothing** for the warning — not the map lookup, not the `strings.HasPrefix`, not the table lookups. Nothing is deferred that could matter: `uninitialized` can only be true for a name that resolved to no special, no match-data slot and no entry in `vm.globals`, which is both rare and already an anomaly in the program being run. ## ⛔ Why this is a reorder and not a benchmark The machine is at 1-minute load 8.9 and 5-minute load 16 — around twenty-five peer sessions build across this fleet — and above roughly 8 it cannot time anything honestly. So the choice was between a number I could not defend and a structure that makes the number unnecessary. Removing a cost beats measuring it. ## ⛔ And the reorder found dead code, which the coverage gate would have caught Deferring the tests made one of them unreachable: ```go if mriPredefinedGvars[name] || englishAlias[name] != "" { ``` Probed, by walking both tables through `gvarResolve`: **0 of englishAlias's 9 keys** resolve uninitialised, while **9 of the predefined table's 42** do — and those nine are exactly `$,` `$-F` `$-i` `$.` `$;` `$?` `$FILENAME` `$\` `$_`, the names this VM leaves on the ordinary user-global path. So the English clause is gone, and the invariant that makes it unnecessary is written down instead: `englishAlias` lists only names whose target this VM models, so `specialGvar` or the match-data resolver answers every one of them and none can arrive there. An alias added for an unmodelled target would warn — the comment says so, and says what to do about it. ## Measured `cmd/oracle` on the globals probe, this branch against the reference: `stderr 810 vs 810 bytes`, three warnings each, same names and same message text. The only differences are the two the probes' README records as expected — 2.6.10's backtick quote style and its one-line lag in a warning's position. Behaviour is unchanged, which is the point: the warning tests and the predefined-table walk from #856 pass untouched. Co-authored-by: Claude Opus 5 --- internal/vm/globals.go | 49 ++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/internal/vm/globals.go b/internal/vm/globals.go index fa71b7c4..e309c9de 100644 --- a/internal/vm/globals.go +++ b/internal/vm/globals.go @@ -307,9 +307,28 @@ func (vm *VM) warnDeprecatedGvar(msg string) { // none of them warns. Route one of them through gvarGet and every `puts` in a // `ruby -w` run grows a warning about $_. // -// The gate is first because it is one map lookup and false in any ordinary run, -// so the name resolution is walked once and the message is composed never. +// ⛔ EVERY test here is behind `uninitialized`, and that ordering is the whole +// cost of the feature. The resolution happens exactly once, as `gvar` did before +// this existed, and a read that found a value returns through the same path it +// always did — so an ordinary `$stdout` or `$LOAD_PATH` read pays nothing for the +// warning, not even the one map lookup the $VERBOSE gate costs. +// +// The first draft asked the gate FIRST, which put `vm.globals["$VERBOSE"]` in +// front of every Ruby-level global read in the interpreter and in both AOT +// tiers. The comment it carried — "one map lookup and false in any ordinary +// run" — was true and beside the point: `getgvar` is a hot instruction, the +// lookup is unconditional, and nothing about it could be measured honestly on a +// machine the fleet keeps at load 16. Making the cost structurally zero is worth +// more than a benchmark of a cost that need not exist. +// +// Nothing is lost by deferring: `uninitialized` can only be true for a name that +// resolved to no special, no match-data slot and no entry in vm.globals, which +// is both rare and already an anomaly in the program being run. func (vm *VM) gvarGet(name string) object.Value { + v, uninitialized := vm.gvarResolve(name) + if !uninitialized { + return v + } // ⛔ bytecode.ErrinfoCell ("#$!") is not a Ruby global, it is the cell the // begin/rescue compiler reads the pending exception through, and it is spelled // with a '#' precisely so no program can name it. vm.go's getgvar arm @@ -317,17 +336,25 @@ func (vm *VM) gvarGet(name string) object.Value { // this line every compiled `rescue` would report an uninitialised global under // `ruby -w`. No Ruby global can start with '#', so the whole family is covered // rather than the one spelling that exists today. - if !vm.warningEnabled() || strings.HasPrefix(name, "#") { - return vm.gvar(name) + if strings.HasPrefix(name, "#") { + return v } - if mriPredefinedGvars[name] || englishAlias[name] != "" { - // A name the interpreter defines has a getter, so it is never - // uninitialised however little of it this VM models. The English library's - // readable aliases resolve to one of those names, so they inherit that. - return vm.gvar(name) + // A name the interpreter defines has a getter, so it is never uninitialised + // however little of it this VM models. Measured: nine of the forty-two names + // in the table reach this point — `$,` `$-F` `$-i` `$.` `$;` `$?` `$FILENAME` + // `$\` `$_` — and they are exactly the nine this VM leaves on the ordinary + // user-global path. + // + // The English library's aliases need no clause of their own, and that is an + // invariant rather than a coincidence: englishAlias lists only names whose + // target this VM models, so specialGvar or the match-data resolver answers + // every one of them and none can arrive here. Probed: 0 of its 9 keys resolve + // uninitialised. ⛔ Adding an alias whose target is NOT modelled would make it + // warn — put it in the table above, or model the target. + if mriPredefinedGvars[name] { + return v } - v, uninitialized := vm.gvarResolve(name) - if uninitialized { + if vm.warningEnabled() { // QUOTE_ID quotes the id rb_gvar_get was CALLED with, so the message // names the spelling the program read, not a canonical slot. vm.rbWarningf("global variable '%s' not initialized", name) From 107495341b556a94916cf07c259af07539091b47 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 1 Nov 2026 12:59:37 +0100 Subject: [PATCH 2/2] vm: cover the errinfo-cell guard, which only generated code reaches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The macOS lane went red. Its log was unreadable — `gh api .../jobs//logs` answered 403 twice while `gh api rate_limit` reported **4707 of 5000 core calls remaining**, so that endpoint has its own burst limit and the hourly budget was never the problem. Diagnosed from the diff instead, and confirmed locally with the same instrument the lane uses. Taking the gate off the hot path moved the `'#'` guard from ```go if !vm.warningEnabled() || strings.HasPrefix(name, "#") { // before ``` to a statement of its own behind the `uninitialized` flag. Before, every global read with `$VERBOSE` unset executed it; after, it is reachable only when a name is both never-assigned and starts with `'#'` — which is `bytecode.ErrinfoCell` ("#$!") and nothing else. ⛔ And no Ruby program can reach it through the interpreter: vm.go's getgvar arm intercepts that name **before** calling `gvarGet`. The only production caller is AOT-compiled code, where the two tiers emit `vm.gvarGet` without the interception. So running a `rescue` does not exercise the guard, and the per-function coverage ratchet correctly noticed that nothing did. A whitebox test calls `gvarGet(bytecode.ErrinfoCell)` directly, which is the honest way to cover a path whose only caller is generated code. It sets `$VERBOSE` to true first — rb_warning's gate — so a missing guard would produce a line rather than being masked by the gate, and it plants the positive beside it: the same call with an ordinary never-assigned name **must** warn, or the silence would prove only that the gate is shut. `gvarGet` measures 100.0% under `go tool cover -func` from those three tests alone. `main` is merged in, since #861 has landed. Co-authored-by: Claude Opus 5 --- internal/vm/gvar_get_errinfo_internal_test.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 internal/vm/gvar_get_errinfo_internal_test.go diff --git a/internal/vm/gvar_get_errinfo_internal_test.go b/internal/vm/gvar_get_errinfo_internal_test.go new file mode 100644 index 00000000..bdf7691c --- /dev/null +++ b/internal/vm/gvar_get_errinfo_internal_test.go @@ -0,0 +1,55 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import ( + "strings" + "testing" + + "github.com/go-embedded-ruby/ruby/internal/bytecode" + "github.com/go-embedded-ruby/ruby/internal/object" +) + +// ⛔ A WHITEBOX test, because the branch it covers is unreachable from Ruby +// through the interpreter. +// +// gvarGet skips the uninitialized-global warning for a name starting with '#', +// which is `bytecode.ErrinfoCell` ("#$!") — the cell the begin/rescue compiler +// reads the pending exception through, spelled so no program can name it. +// vm.go's getgvar arm intercepts that name BEFORE calling gvarGet, so the only +// caller that reaches the guard is AOT-compiled code, where the two tiers emit +// `vm.gvarGet` without the interception. +// +// Running a `rescue` from Ruby therefore does not exercise it, and a reordering +// that moved the guard behind the `uninitialized` flag took it from "covered by +// every global read" to "covered by nothing" — which the per-function coverage +// ratchet is there to notice. Calling gvarGet directly is the honest way to +// cover a path whose only production caller is generated code. +func TestGvarGetDoesNotWarnForTheErrinfoCell(t *testing.T) { + var out strings.Builder + vm := New(&out) + // rb_warning's gate: only a TRUE $VERBOSE prints. Set it, so a missing guard + // would actually produce a line rather than being masked by the gate. + vm.setGVar("$VERBOSE", object.True) + + got := vm.gvarGet(bytecode.ErrinfoCell) + if !object.IsNil(got) { + t.Errorf("gvarGet(%q) = %v, want nil", bytecode.ErrinfoCell, got) + } + if out.String() != "" { + t.Errorf("the errinfo cell produced a warning: %q", out.String()) + } + + // The control: the same call with an ordinary never-assigned name MUST warn, + // or the silence above would prove only that the gate is shut. + out.Reset() + if v := vm.gvarGet("$a_name_no_table_holds"); !object.IsNil(v) { + t.Errorf("an uninitialised global read as %v, want nil", v) + } + if !strings.Contains(out.String(), "not initialized") { + t.Fatalf("the planted positive produced no warning: %q — the errinfo "+ + "silence proves nothing", out.String()) + } +}