README
¶
Optional memoization
github.com/goplus/ixgo/memo enables memoization for the default stack and bytecode dispatch backends.
EnableInstructionExecution alone skips memo; when combined with
EnableBytecodeDispatch, dispatch takes precedence and supports memo.
The package is optional for applications embedding ixgo. The ixgo command imports
it explicitly, so CLI runs enable memoization by default.
Enable memoization
Add a blank import to the program that creates or runs the interpreter:
import _ "github.com/goplus/ixgo/memo"
The package registers the memo plugin during init. Without this import, ixgo keeps
memoization disabled. The ixgo command includes this blank import, so command-line
runs enable memoization automatically. Applications embedding ixgo choose whether to
include the import.
Supported paths
The plugin analyzes each function with SSA (Static Single Assignment) information before installing a cache. It supports these paths:
- Pure scalar recursion with integer,
float32, andfloat64values. Bytecode dispatch requires at least two self-call sites to avoid cache overhead on linear recursion; the default stack eligibility is unchanged. - Integer accumulator recursion with one isolated pointer output.
- Recursive receiver call graphs with cached receiver fields, including helper methods that track field transforms. Methods outside these graphs use the selected backend without an invocation-local receiver memo cache.
- Integer additive, affine, and supported bitwise receiver transforms.
- Floating-point receiver state caching using exact IEEE-754 bit patterns.
- Mutual recursion when the complete call graph satisfies the safety checks.
The analysis is conservative. Unsupported or observable behavior uses the selected backend without memo. This includes multiple mutable outputs, unsafe pointer aliasing, external side effects, hooks, tracing, debug callbacks, custom call interception, and unsupported instructions.
Floating-point behavior
Float keys and cached state use exact bit patterns. The implementation preserves:
+0and-0;- NaN payloads;
- positive and negative infinity;
float32precision and step-by-step rounding.
The plugin does not regroup floating-point operations. The uglyfib floating-point
files are examples only and are not memoized because preserving their operation order
does not provide a safe reusable state for this optimizer.
Package layout
The SSA eligibility checks and receiver analysis live in this package. Shared runtime
bit-pattern helpers used by the interpreter live in github.com/goplus/ixgo/internal/memo.
Tests
From the repository root:
go test ./memo
GOWORK=off go test -race ./memo
go test -race -tags instruction . ./memo
The stack integration tests are in interp_memo_test.go. Dispatch integration
is covered by TestBytecodeMemoPlugin, TestBytecodeMemoEntryLeaf,
TestBytecodeMemoLinearRecursion, and TestDispatchMemoExecution; use the
instruction build tag to exercise dispatch. The
SSA eligibility tests are in this package because the analysis is package-private.
fibish performance comparison
The following measurements compare the default stack interpreter with memoization enabled
against the main baseline. Both sides use the same testdata/fibish source,
GOWORK=off, GOMAXPROCS=1, macOS arm64, and include process startup and source
loading. Times are shown to 0.001 seconds. Each value is the median of five
complete-process runs. The table lists fixtures with a recorded baseline, correct
output, and a useful memoization result.
| Fixture | main |
Memoized | Speedup |
|---|---|---|---|
fibish.go |
3.997 s | 0.053 s | 75.4x |
fibish_closure.go |
6.371 s | 0.053 s | 120.2x |
fibish_struct.go |
4.105 s | 0.058 s | 70.8x |
fibish_struct2.go |
14.104 s | 0.055 s | 256.4x |
uglyfib.go |
114.526 s | 0.056 s | 2,045.1x |
uglyfib_struct.go |
124.912 s | 0.056 s | 2,230.6x |
fibish_float32.go |
8.831 s | 0.053 s | 166.6x |
fibish_float64.go |
9.420 s | 0.056 s | 168.2x |
fibish_struct_float32.go |
7.081 s | 0.055 s | 128.7x |
fibish_struct_float64.go |
8.243 s | 0.056 s | 147.2x |
fibish_struct2_float32.go |
18.845 s | 0.053 s | 358.5x |
fibish_struct2_float64.go |
22.719 s | 0.052 s | 433.9x |
These files are examples only. They are not memoized:
uglyfib_float32.gouglyfib_float64.gouglyfib_struct_float32.gouglyfib_struct_float64.go