Skip to content

v0.7.10.6

Choose a tag to compare

@github-actions github-actions released this 29 Apr 21:29
· 8 commits to main since this release
v0.7.10.6
41ff344

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.OptimizeLoadConst rewrites every LOAD_CONST whose
    pool entry is an int in [0, 255] into LOAD_SMALL_INT with
    the value embedded in the oparg.
    MIRRORS: Python/flowgraph.c:2168 basicblock_optimize_load_const
    • :1408 maybe_instr_make_load_smallint.
  • flowgraph.RemoveUnusedConsts condenses the const pool to
    indices that surviving LOAD_CONST instructions 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 v when consts[idx] is an
    int in [0, 255]. The const-pool entry is left in place; the
    follow-up RemoveUnusedConsts pass condenses unreferenced slots
    except slot 0.
  • compiler/flowgraph/remove_unused_consts.go (90 LOC) —
    RemoveUnusedConsts(seq, consts) []any builds an index_map,
    marks every used index, always reserves slot 0, condenses the
    pool, and remaps every surviving LOAD_CONST oparg 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_CONST opcodes 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 new emitFuncBodyConstLoad (always
    LOAD_CONST + addConst). The old
    emitFuncBodyConstLoadFirstInt was deleted.
  • compiler/codegen/scope.go::popChildUnit — runs
    flowgraph.OptimizeLoadConst and flowgraph.RemoveUnusedConsts
    before optimize.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 on fold_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 through popChildUnit. 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.