]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: special case allocation of arrays of size 1
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 17 Jul 2017 03:46:52 +0000 (17:46 -1000)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 14 Aug 2017 23:32:03 +0000 (23:32 +0000)
This avoids division and multiplication.
Instrumentation suggests that this is a very common case.

Change-Id: I2d5d5012d4f4df4c4af1f9f85ca9c323c9889c0e
Reviewed-on: https://go-review.googlesource.com/54657
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/malloc.go

index 0ebd2c0ab2306afa4006c00dfac820b203e3fce9..274ab537fcf732d00f453a02d04793b174390c55 100644 (file)
@@ -847,6 +847,9 @@ func reflect_unsafe_New(typ *_type) unsafe.Pointer {
 
 // newarray allocates an array of n elements of type typ.
 func newarray(typ *_type, n int) unsafe.Pointer {
+       if n == 1 {
+               return mallocgc(typ.size, typ, true)
+       }
        if n < 0 || uintptr(n) > maxSliceCap(typ.size) {
                panic(plainError("runtime: allocation size out of range"))
        }