tinygo/

directory
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT, BSD-3-Clause

README

TinyGo runtime fork for the Durable core

budget/build-wasm.sh builds the TinyGo core with the TinyGo release pinned in the tinygo rows of automation/dev/toolchains.lock (0.42.0) and nothing else. It refuses any other version; budget/tinygo-install.sh installs the pinned release and checks its digest. It builds against a TINYGOROOT overlay: real directories whose files link to the toolchain's own, with every .go file under src/ here copied over the toolchain's file at the same path. The tinygo binary is not rebuilt.

Each file here is the 0.42.0 file with the changes listed below. Diff it against the toolchain's file to review it. To move to a new TinyGo, port each change to that version's file and change the version and digests in automation/dev/toolchains.lock.

src/go.mod makes src/ a module of its own, so the root module's go build ./... and go vet ./... skip the fork. Without it they loaded src/runtime as a package and failed: "build constraints exclude all Go files". build-wasm.sh copies only the .go files.

file change why
src/runtime/arch_tinygowasm.go growHeap grows the heap by a fixed step (growHeapStepKiB, set by build-wasm.sh from CORE_HEAP_STEP; 4 MiB; a heap smaller than the step grows by its own size, so a small module doubles up to the step), halves a step that passes the maximum memory until it fits, never takes a step below 1/64 of the heap (setHeapEnd's memcpy of the GC metadata needs the old and new metadata disjoint), and doubles the step for the calls one allocation makes (the allocator does not join the free range a step adds to the one before it, so an allocation larger than the step would never fit) Stock TinyGo doubles the heap (48 to 96 MiB), so the heap reaches only the initial size times a power of two, the last step reserves as much as the program has used, and a doubling past the cap fails even when memory below the cap remains. A step of at most 4 MiB without the doubling rule fails an allocation larger than 4 MiB at any heap size with "out of memory" while most of the cap is free.
src/runtime/gc_blocks.go (with heapMaxKiB/heapKeepFloor in arch_tinygowasm.go) popFreeRange takes keep; alloc passes it until the request has run a collection. With it, a request under keepLargeBlocks (32 KiB) whose shortest fitting free range has 32 KiB or more gets nil, so alloc collects first, while keepLargeRanges holds: the heap is at least two thirds of the maximum memory (heapKeepFloor, from heapMaxKiB, which build-wasm.sh sets from CORE_MAX_MEMORY) and heap/16 bytes or more were allocated since the last collection (allocAtGCNot, set by runGC). Both globals are kept inverted, so neither is a heap-range word for the root scan. The request after a collection takes any range. The allocator is best fit and never moves objects. Late in a long run near the ceiling, small objects freed among live ones leave the free memory in many short ranges and small requests eat into the long ones, so a large slice fails with a sixth of the heap free: U-harness-scheduler-retention's 20,000-deep chain at 96 MiB failed its finish transaction asking 1.5 MB with 16 MB free in 166,000 ranges, the longest 240 KB. With the rule it completes (69 collections to step 180,000 against 40; the leaf phase about 30% slower). Below the floor nothing changes, so the count rows (1 MiB of linear memory) do not move.
src/runtime/gc_globals.go On wasm findGlobals scans the globals from the start of .data (the word after the marker PiGDDATA in pigDataStart, read volatile), not from __global_base; it scans from __global_base when the word is unset or out of range. build-wasm.sh links with --Map and patchdata writes the word from the map, failing the build when the map has no .data, when a memory section other than .rodata starts below .data, or when the module holds the marker other than once. wasm-ld puts .rodata between __global_base and .data. Its string bytes and constant tables hold about 330 words in the heap's address range (3-letter ASCII tails plus NUL point into 6.1-7.6 MiB), and the conservative scan kept every heap object under them alive: on W1-3,500 the dead first-turn 3.5 MB model request and step output and several 0.4-0.8 MB read pages.
src/runtime/gc_globals.go (skip ranges) pigDataStart also carries up to 12 [start, end) ranges of .data or .bss (unused pairs ^0); findGlobals leaves them out of the root scan and never passes an empty range. patchdata fills them from the linker map with the .data and .bss symbols matching ^(strconv\$alloc(\.[0-9]+)?|arc4random_buf\.rng_state|runtime\.(xorshift32State|gcMallocs|gcTotalAlloc|growHeapMallocs)|main\.(stats|allocs))$, merges adjacent ones, and fails when there are more than 12 ranges or when the ranges are already set. TinyGo's package initializer places strconv's constant tables (the isPrint16/isGraphic uint16 code-point pairs) in .data. Read as words they look like heap addresses (0x037700a1 is 58 MB), and about 24 of them pinned whatever object they landed in, for the module's life: a 20,000-task creation transaction among them (U-harness-scheduler-retention's 20,000-deep chain). wasi-libc's arc4random_buf.rng_state (552 B of .bss) holds the ChaCha output the runtime's hardwareRand leaves there: random words that pinned a dead 1.5 MB store.Tx.writes array and the 5.7 MB of statement parameters under it; the runtime's fastrand state xorshift32State, seeded from it, is a random word too. The allocator's counters (gcMallocs, gcTotalAlloc, the fork's growHeapMallocs) and the core binding's copies of them in its mem_stats and alloc_stats answers (main.stats, main.allocs) sweep through the heap's addresses as the instance allocates (300,000 allocations is 0x493e0, inside a heap that starts at 0x26210), so each collection they pin another object: gcMallocs held 1,008 and then 176 B of dead objects at the last two readings of the count gate's 6-turn replay, and wasm-tinygo's warm.liveGrowthPerTurn read 20 to 64 B/turn, moving with every change of the allocation count, while the same core read 21 natively and on Go. With the counters left out it reads one figure at every fixture size, and the heap at 50 and 3,500 turns is the same.
src/runtime/hashmap.go Bucket arrays get a GC layout: the tophash bytes and padding are not pointers, and the key and value words are pointers only when the key or value type is at least a pointer wide. Layouts longer than an inline layout are cached in hashmapBucketLayouts. The file gets a tinygo.wasm build constraint, which the upstream file does not have. hashmapMake allocated buckets with a nil layout, so the conservative scan read every word as a pointer. Tophash bytes and sub-word values form heap-range words (a map[ID]bool value word of 0x01010101 is 16 MiB); on a 20,000-deep chain this kept a whole session.Tx and old bucket arrays alive, and the 96 MiB core ran out of memory (live 155 MB, against 90 MB with the layout). The constraint keeps go build ./... from compiling the fork as a Go package.

TinyGo is BSD-3-licensed: see LICENSE.

Directories

Path Synopsis
Command patchdata writes the start address of .data, and the ranges of the globals that hold no heap pointers, into a TinyGo core built with the runtime fork in budget/tinygo.
Command patchdata writes the start address of .data, and the ranges of the globals that hold no heap pointers, into a TinyGo core built with the runtime fork in budget/tinygo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL