Repository navigation
v0.7.10.6
v0.7.10.6 — optimize_load_const + remove_unused_consts 1:1 port
Date: 2026-04-30
Spec: notes/Spec/1500/1564_gocopy_v07106_optimize_load_const.md
Roadmap: notes/Spec/1500/1558_gocopy_v0710x_release_roadmap.md
v0.7.10.6 lifts the visitor's "first-int quirk" out of
compiler/codegen/visit_func_stmt.go and into two CPython-faithful
flowgraph passes ported 1:1 from
Python/flowgraph.c:
flowgraph.OptimizeLoadConstrewrites everyLOAD_CONSTwhose
pool entry is an int in[0, 255]intoLOAD_SMALL_INTwith
the value embedded in the oparg.
MIRRORS:Python/flowgraph.c:2168 basicblock_optimize_load_const:1408 maybe_instr_make_load_smallint.
flowgraph.RemoveUnusedConstscondenses the const pool to
indices that survivingLOAD_CONSTinstructions still
reference, always preserving slot 0 (CPython treats it as the
implicit docstring slot).
MIRRORS:Python/flowgraph.c:3174 remove_unused_consts.
The visitor now mirrors codegen_addop_load_const: every constant
load is addConst(v) + emit LOAD_CONST idx, and the optimizer
earns its keep post-emit. Net byte output is unchanged.
Reduced scope (vs spec 1558's original "v0.7.10.6")
Spec 1558 originally bundled seven flowgraph functions plus the
compiler/optimize/ → compiler/flowgraph/ package move into
this release. The full bundle is multi-day work and would block
fixture-coverage releases. v0.7.10.6 ships the two passes
sufficient to delete the first-int hack; the rest follow:
fold_const_unaryop+fold_const_binop: v0.7.10.7 / .8.- Named-expression / walrus: later, when its fixtures arrive.
compiler/optimize/→compiler/flowgraph/package move: pre-
funeral cleanup at v0.7.10.14.
Added
compiler/flowgraph/optimize_load_const.go(52 LOC) —
OptimizeLoadConst(seq, consts)walks every block, rewriting
LOAD_CONST idx→LOAD_SMALL_INT vwhenconsts[idx]is an
int in[0, 255]. The const-pool entry is left in place; the
follow-upRemoveUnusedConstspass condenses unreferenced slots
except slot 0.compiler/flowgraph/remove_unused_consts.go(90 LOC) —
RemoveUnusedConsts(seq, consts) []anybuilds anindex_map,
marks every used index, always reserves slot 0, condenses the
pool, and remaps every survivingLOAD_CONSToparg to its new
index. Returns the original slice when no entries dropped.compiler/flowgraph/optimize_load_const_test.go(9 cases) —
covers int 0 / int 255 / int 256 / negative int / None / string
pool entries; non-LOAD_CONSTopcodes left untouched; nil seq;
oparg outside pool.compiler/flowgraph/remove_unused_consts_test.go(8 cases) —
covers all-used / slot-0-always-kept / drop-middle / drop-
trailing / remap-all-surviving / empty pool / nil seq /
cross-block.
Changed
compiler/codegen/visit_func_stmt.go—visitFuncConstantExpr
now calls the newemitFuncBodyConstLoad(always
LOAD_CONST + addConst). The old
emitFuncBodyConstLoadFirstIntwas deleted.compiler/codegen/scope.go::popChildUnit— runs
flowgraph.OptimizeLoadConstandflowgraph.RemoveUnusedConsts
beforeoptimize.Run, so downstream passes
(InsertSuperinstructions,OptimizeLoadFast,resolveJumps)
see the canonical opcode shape.CHANGELOG.md— v0.7.10.6 entry added.
Tests
- Oracle (
bash tests/run.sh): 260/260 fixtures byte-identical. - Visitor parity (
go test ./compiler/ -run TestVisitorParity):
byte-identical=219 not-implemented=41 mismatched=0 errored=0
(unchanged from v0.7.10.5). - New flowgraph unit tests: 17 added, all green.
Out of scope (deferred)
010_expr_const_negative.py(def f(): return -3) — still
blocked onfold_const_unaryop. Lands when that pass ships.- Compare-chain shape (
a < b < c) — needs both visitor and
classifier work. Release TBD. - Module-level (top-level) const-pool optimization — only
function bodies and closures go throughpopChildUnit. The
module-level path keeps its current shape until the func_body.go
funeral (v0.7.10.14) makes the visitor the only top-level path.