From a4cbe3326ec37313bfd92c01c3dbf313cae4e2b8 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 5 Aug 2024 16:12:48 +0200 Subject: [PATCH] runtime: only allocate heap memory when needed For example, with -gc=none and -gc=leaking, no heap needs to be allocated when initializing the runtime. And some GCs (like -gc=custom) are responsible for allocating the heap themselves. --- src/runtime/gc_blocks.go | 1 + src/runtime/gc_custom.go | 2 ++ src/runtime/gc_leaking.go | 2 ++ src/runtime/gc_none.go | 2 ++ src/runtime/runtime_unix.go | 7 +++++-- 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/runtime/gc_blocks.go b/src/runtime/gc_blocks.go index d58bfd92a..6d3286364 100644 --- a/src/runtime/gc_blocks.go +++ b/src/runtime/gc_blocks.go @@ -37,6 +37,7 @@ import ( ) const gcDebug = false +const needsStaticHeap = true // Some globals + constants for the entire GC. diff --git a/src/runtime/gc_custom.go b/src/runtime/gc_custom.go index a34b7dce6..0125f1688 100644 --- a/src/runtime/gc_custom.go +++ b/src/runtime/gc_custom.go @@ -36,6 +36,8 @@ import ( "unsafe" ) +const needsStaticHeap = false + // initHeap is called when the heap is first initialized at program start. func initHeap() diff --git a/src/runtime/gc_leaking.go b/src/runtime/gc_leaking.go index 9e84dc2a5..bdc7efe81 100644 --- a/src/runtime/gc_leaking.go +++ b/src/runtime/gc_leaking.go @@ -11,6 +11,8 @@ import ( "unsafe" ) +const needsStaticHeap = true + // Ever-incrementing pointer: no memory is freed. var heapptr uintptr diff --git a/src/runtime/gc_none.go b/src/runtime/gc_none.go index 43d3c4904..173c1f643 100644 --- a/src/runtime/gc_none.go +++ b/src/runtime/gc_none.go @@ -10,6 +10,8 @@ import ( "unsafe" ) +const needsStaticHeap = false + var gcTotalAlloc uint64 // for runtime.MemStats var gcMallocs uint64 var gcFrees uint64 diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 08e3e7426..6add9157d 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -79,7 +79,10 @@ var stackTop uintptr // //export main func main(argc int32, argv *unsafe.Pointer) int { - preinit() + if needsStaticHeap { + // Allocate area for the heap if the GC needs it. + allocateHeap() + } // Store argc and argv for later use. main_argc = argc @@ -298,7 +301,7 @@ var heapMaxSize uintptr var heapStart, heapEnd uintptr -func preinit() { +func allocateHeap() { // Allocate a large chunk of virtual memory. Because it is virtual, it won't // really be allocated in RAM. Memory will only be allocated when it is // first touched.