Skip to content

Commit

Permalink
runtime: only allocate heap memory when needed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aykevl committed Aug 24, 2024
1 parent 1ef1aa7 commit 3a94c90
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/runtime/gc_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

const gcDebug = false
const needsStaticHeap = true

// Some globals + constants for the entire GC.

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/gc_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"unsafe"
)

const needsStaticHeap = false

// initHeap is called when the heap is first initialized at program start.
func initHeap()

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/gc_leaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"unsafe"
)

const needsStaticHeap = true

// Ever-incrementing pointer: no memory is freed.
var heapptr = heapStart

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/gc_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"unsafe"
)

const needsStaticHeap = false

var gcTotalAlloc uint64 // for runtime.MemStats
var gcMallocs uint64
var gcFrees uint64
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/runtime_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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
Expand Down Expand Up @@ -267,7 +270,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.
Expand Down

0 comments on commit 3a94c90

Please sign in to comment.