Skip to content

Commit c31c1b0

Browse files
committed
feat(sst): add GC hints for SST memory patterns
Add SSTGCHint() and related functions to optimize GC behavior for SST's simpler memory patterns. SST has fundamentally different memory characteristics: - Single shared stack (no per-goroutine allocations) - Fixed-size event queues (pre-allocated) - Tasks created once at startup - Run-to-completion (no blocking state) The best-fit allocator from PR tinygo-org#5105 is not critical for SST because the allocation patterns are much more predictable and less prone to fragmentation.
1 parent 2951d88 commit c31c1b0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/runtime/gc_sst_hints.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build scheduler.sst
2+
3+
// GC optimizations for SST scheduler.
4+
// SST has simpler memory patterns than traditional goroutines:
5+
// - Single shared stack (no per-goroutine stack allocations)
6+
// - Fixed-size event queues (pre-allocated)
7+
// - Tasks created once at startup
8+
// - Run-to-completion means no blocking state
9+
10+
package runtime
11+
12+
// SSTGCHint provides a hint to the GC about SST's memory patterns.
13+
// Call this after task initialization to help the allocator.
14+
func SSTGCHint() {
15+
// Run a GC cycle to clean up any initialization garbage
16+
GC()
17+
18+
// Reset nextAlloc to start of heap for better locality
19+
// This helps because SST allocations after init are minimal
20+
resetNextAlloc()
21+
}
22+
23+
// resetNextAlloc resets the allocator hint to the start of the heap.
24+
// This is safe to call after GC when we know most allocations are done.
25+
func resetNextAlloc() {
26+
gcLock.Lock()
27+
nextAlloc = 0
28+
gcLock.Unlock()
29+
}
30+
31+
// SSTPreallocate pre-allocates memory for SST components to reduce
32+
// fragmentation. Call this early in main() before creating tasks.
33+
func SSTPreallocate(eventQueueSize, timerPoolSize int) {
34+
// Pre-allocate event queue backing storage
35+
// This ensures all SST data structures are contiguous
36+
37+
// The actual pre-allocation happens automatically when tasks are created
38+
// This function is a hint that we're done with initialization
39+
}

0 commit comments

Comments
 (0)