mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 07:23:39 +00:00
compiler, runtime: optimize zero-sized allocations
Instead of referring to an unused global, use a constant value. This is safe even when using `-gc=none` (since no actual memory gets allocated) which wasn't the case before. It should also reduce binary size by a few bytes for most programs.
This commit is contained in:
committed by
Ron Evans
parent
221ea6a54c
commit
134de98ba5
@@ -8,3 +8,22 @@ import "unsafe"
|
||||
// It is used instead of normal alloc in //go:noheap functions, and must either
|
||||
// be optimized away or throw a linker error.
|
||||
func alloc_noheap(size uintptr, layout unsafe.Pointer) unsafe.Pointer
|
||||
|
||||
// Special alloc function that returns a sentinel value that can never be on the
|
||||
// heap or match any other valid pointer. An alloc(0, xxx) call can be safely
|
||||
// converted to an alloc_zero(0, xxx) call as an optimization.
|
||||
//
|
||||
// It is always a good idea to inline this function, since the result is a
|
||||
// constant. Marking it as go:inline to be sure even though the compiler should
|
||||
// already be doing this.
|
||||
//
|
||||
//go:inline
|
||||
func alloc_zero(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
// Returning a constant here is safe, since the Go spec does not require
|
||||
// multiple zero-sized allocations to be unequal when compared for equality:
|
||||
//
|
||||
// > Pointers to distinct zero-size variables may or may not be equal.
|
||||
//
|
||||
// Source: https://go.dev/ref/spec#Comparison_operators
|
||||
return unsafe.Pointer(zeroSizeAllocPtr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user