diff options
| author | Russ Cox <rsc@golang.org> | 2011-11-15 12:05:25 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2011-11-15 12:05:25 -0500 |
| commit | 94c2536e3f286f06dc7d8acfdbccac115a15437a (patch) | |
| tree | dca5a355e0c6b607b1405e3fd6cdb9fe5343310f /src/pkg/runtime/slice.c | |
| parent | 276473cd72d47c5566f1dafeee6c45ff688cac74 (diff) | |
| download | go-94c2536e3f286f06dc7d8acfdbccac115a15437a.tar.xz | |
runtime: avoid allocation for make([]T, 0)
R=gri, iant, iant
CC=golang-dev
https://golang.org/cl/5375093
Diffstat (limited to 'src/pkg/runtime/slice.c')
| -rw-r--r-- | src/pkg/runtime/slice.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/pkg/runtime/slice.c b/src/pkg/runtime/slice.c index 20edf24d94..2fe4c6da47 100644 --- a/src/pkg/runtime/slice.c +++ b/src/pkg/runtime/slice.c @@ -32,6 +32,11 @@ runtime·makeslice(SliceType *t, int64 len, int64 cap, Slice ret) } } +// Dummy word to use as base pointer for make([]T, 0). +// Since you cannot take the address of such a slice, +// you can't tell that they all have the same base pointer. +static uintptr zerobase; + static void makeslice1(SliceType *t, int32 len, int32 cap, Slice *ret) { @@ -42,7 +47,9 @@ makeslice1(SliceType *t, int32 len, int32 cap, Slice *ret) ret->len = len; ret->cap = cap; - if((t->elem->kind&KindNoPointers)) + if(cap == 0) + ret->array = (byte*)&zerobase; + else if((t->elem->kind&KindNoPointers)) ret->array = runtime·mallocgc(size, FlagNoPointers, 1, 1); else ret->array = runtime·mal(size); |
