2014-07-31 13:43:40 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-04-10 16:01:54 -06:00
|
|
|
type slice struct {
|
2014-07-31 13:43:40 -06:00
|
|
|
array unsafe.Pointer
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: take uintptrs instead of int64s?
|
2015-04-10 16:01:54 -06:00
|
|
|
func makeslice(t *slicetype, len64, cap64 int64) slice {
|
2014-07-31 13:43:40 -06:00
|
|
|
// NOTE: The len > MaxMem/elemsize check here is not strictly necessary,
|
|
|
|
// but it produces a 'len out of range' error instead of a 'cap out of range' error
|
|
|
|
// when someone does make([]T, bignumber). 'cap out of range' is true too,
|
|
|
|
// but since the cap is only being supplied implicitly, saying len is clearer.
|
|
|
|
// See issue 4085.
|
|
|
|
len := int(len64)
|
2014-11-11 15:05:02 -07:00
|
|
|
if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > _MaxMem/uintptr(t.elem.size) {
|
2014-07-31 13:43:40 -06:00
|
|
|
panic(errorString("makeslice: len out of range"))
|
|
|
|
}
|
|
|
|
cap := int(cap64)
|
2014-11-11 15:05:02 -07:00
|
|
|
if cap < len || int64(cap) != cap64 || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) {
|
2014-07-31 13:43:40 -06:00
|
|
|
panic(errorString("makeslice: cap out of range"))
|
|
|
|
}
|
|
|
|
p := newarray(t.elem, uintptr(cap))
|
2015-04-10 16:01:54 -06:00
|
|
|
return slice{p, len, cap}
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:01:54 -06:00
|
|
|
func growslice(t *slicetype, old slice, n int) slice {
|
2014-07-31 13:43:40 -06:00
|
|
|
if n < 1 {
|
|
|
|
panic(errorString("growslice: invalid n"))
|
|
|
|
}
|
|
|
|
|
2015-02-26 23:13:05 -07:00
|
|
|
cap := old.cap + n
|
|
|
|
if cap < old.cap || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) {
|
2014-07-31 13:43:40 -06:00
|
|
|
panic(errorString("growslice: cap out of range"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-04 13:53:45 -06:00
|
|
|
racereadrangepc(old.array, uintptr(old.len*int(t.elem.size)), callerpc, funcPC(growslice))
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
et := t.elem
|
|
|
|
if et.size == 0 {
|
2015-03-11 10:07:50 -06:00
|
|
|
// append should not create a slice with nil pointer but non-zero len.
|
|
|
|
// We assume that append doesn't need to preserve old.array in this case.
|
2015-04-10 16:01:54 -06:00
|
|
|
return slice{unsafe.Pointer(&zerobase), old.len, cap}
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
newcap := old.cap
|
|
|
|
if newcap+newcap < cap {
|
|
|
|
newcap = cap
|
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
if old.len < 1024 {
|
|
|
|
newcap += newcap
|
|
|
|
} else {
|
|
|
|
newcap += newcap / 4
|
|
|
|
}
|
|
|
|
if newcap >= cap {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:05:02 -07:00
|
|
|
if uintptr(newcap) >= _MaxMem/uintptr(et.size) {
|
2014-07-31 13:43:40 -06:00
|
|
|
panic(errorString("growslice: cap out of range"))
|
|
|
|
}
|
|
|
|
lenmem := uintptr(old.len) * uintptr(et.size)
|
2014-12-29 00:16:32 -07:00
|
|
|
capmem := roundupsize(uintptr(newcap) * uintptr(et.size))
|
2014-07-31 13:43:40 -06:00
|
|
|
newcap = int(capmem / uintptr(et.size))
|
|
|
|
var p unsafe.Pointer
|
|
|
|
if et.kind&kindNoPointers != 0 {
|
|
|
|
p = rawmem(capmem)
|
2014-12-22 20:42:05 -07:00
|
|
|
memmove(p, old.array, lenmem)
|
2014-07-31 13:43:40 -06:00
|
|
|
memclr(add(p, lenmem), capmem-lenmem)
|
|
|
|
} else {
|
2015-06-11 07:49:38 -06:00
|
|
|
// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
|
2014-07-31 13:43:40 -06:00
|
|
|
p = newarray(et, uintptr(newcap))
|
runtime: use memmove during slice append
The effect of this CL:
name old mean new mean delta
BinaryTree17 5.97s × (0.96,1.04) 5.95s × (0.98,1.02) ~ (p=0.697)
Fannkuch11 4.39s × (1.00,1.01) 4.41s × (1.00,1.01) +0.52% (p=0.015)
FmtFprintfEmpty 90.8ns × (0.97,1.05) 89.4ns × (0.94,1.13) ~ (p=0.571)
FmtFprintfString 305ns × (0.99,1.01) 292ns × (0.98,1.05) -4.35% (p=0.000)
FmtFprintfInt 278ns × (0.96,1.03) 279ns × (0.98,1.04) ~ (p=0.741)
FmtFprintfIntInt 489ns × (0.99,1.02) 482ns × (0.98,1.03) -1.43% (p=0.024)
FmtFprintfPrefixedInt 402ns × (0.98,1.02) 395ns × (0.98,1.03) -1.67% (p=0.014)
FmtFprintfFloat 578ns × (1.00,1.00) 569ns × (0.99,1.01) -1.48% (p=0.000)
FmtManyArgs 1.88µs × (0.99,1.01) 1.88µs × (1.00,1.01) ~ (p=0.055)
GobDecode 15.3ms × (0.99,1.01) 15.2ms × (1.00,1.01) -0.61% (p=0.007)
GobEncode 11.8ms × (0.98,1.05) 11.6ms × (0.99,1.01) ~ (p=0.075)
Gzip 647ms × (0.99,1.01) 647ms × (1.00,1.00) ~ (p=0.790)
Gunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~ (p=0.370)
HTTPClientServer 91.2µs × (0.99,1.01) 91.7µs × (0.99,1.02) ~ (p=0.233)
JSONEncode 31.5ms × (0.98,1.01) 31.8ms × (0.99,1.02) +1.09% (p=0.015)
JSONDecode 110ms × (0.99,1.01) 110ms × (0.99,1.02) ~ (p=0.577)
Mandelbrot200 6.00ms × (1.00,1.00) 6.02ms × (1.00,1.00) +0.24% (p=0.001)
GoParse 6.68ms × (0.98,1.02) 6.61ms × (0.99,1.01) -1.10% (p=0.027)
RegexpMatchEasy0_32 162ns × (1.00,1.00) 161ns × (1.00,1.01) -0.66% (p=0.001)
RegexpMatchEasy0_1K 539ns × (1.00,1.00) 539ns × (0.99,1.01) ~ (p=0.509)
RegexpMatchEasy1_32 140ns × (0.99,1.02) 139ns × (0.99,1.02) ~ (p=0.163)
RegexpMatchEasy1_1K 886ns × (1.00,1.00) 887ns × (1.00,1.00) ~ (p=0.408)
RegexpMatchMedium_32 252ns × (1.00,1.00) 255ns × (0.99,1.01) +1.01% (p=0.000)
RegexpMatchMedium_1K 72.6µs × (1.00,1.00) 72.6µs × (1.00,1.00) ~ (p=0.176)
RegexpMatchHard_32 3.84µs × (1.00,1.00) 3.84µs × (1.00,1.00) ~ (p=0.403)
RegexpMatchHard_1K 117µs × (1.00,1.00) 117µs × (1.00,1.00) ~ (p=0.351)
Revcomp 926ms × (0.99,1.01) 925ms × (0.99,1.01) ~ (p=0.541)
Template 126ms × (0.99,1.02) 130ms × (0.99,1.01) +3.42% (p=0.000)
TimeParse 632ns × (0.99,1.01) 626ns × (1.00,1.00) -0.88% (p=0.000)
TimeFormat 658ns × (0.99,1.01) 662ns × (0.99,1.02) ~ (p=0.111)
The effect of this CL combined with CL 9886:
name old mean new mean delta
BinaryTree17 5.90s × (0.98,1.03) 5.95s × (0.98,1.02) ~ (p=0.175)
Fannkuch11 4.34s × (1.00,1.00) 4.41s × (1.00,1.01) +1.69% (p=0.000)
FmtFprintfEmpty 87.3ns × (0.97,1.17) 89.4ns × (0.94,1.13) ~ (p=0.499)
FmtFprintfString 288ns × (0.98,1.04) 292ns × (0.98,1.05) ~ (p=0.292)
FmtFprintfInt 290ns × (0.98,1.05) 279ns × (0.98,1.04) -3.76% (p=0.001)
FmtFprintfIntInt 493ns × (0.98,1.04) 482ns × (0.98,1.03) -2.27% (p=0.017)
FmtFprintfPrefixedInt 399ns × (0.98,1.02) 395ns × (0.98,1.03) ~ (p=0.159)
FmtFprintfFloat 569ns × (1.00,1.00) 569ns × (0.99,1.01) ~ (p=0.847)
FmtManyArgs 1.90µs × (0.99,1.03) 1.88µs × (1.00,1.01) -1.14% (p=0.009)
GobDecode 15.2ms × (1.00,1.01) 15.2ms × (1.00,1.01) ~ (p=0.170)
GobEncode 11.8ms × (0.99,1.02) 11.6ms × (0.99,1.01) -1.47% (p=0.003)
Gzip 649ms × (0.99,1.00) 647ms × (1.00,1.00) ~ (p=0.200)
Gunzip 144ms × (0.99,1.01) 142ms × (1.00,1.00) -1.04% (p=0.000)
HTTPClientServer 91.1µs × (0.98,1.03) 91.7µs × (0.99,1.02) ~ (p=0.345)
JSONEncode 31.5ms × (0.99,1.01) 31.8ms × (0.99,1.02) +0.98% (p=0.021)
JSONDecode 110ms × (1.00,1.01) 110ms × (0.99,1.02) ~ (p=0.259)
Mandelbrot200 6.02ms × (1.00,1.01) 6.02ms × (1.00,1.00) ~ (p=0.500)
GoParse 6.68ms × (1.00,1.01) 6.61ms × (0.99,1.01) -1.17% (p=0.001)
RegexpMatchEasy0_32 161ns × (1.00,1.00) 161ns × (1.00,1.01) -0.39% (p=0.033)
RegexpMatchEasy0_1K 539ns × (1.00,1.00) 539ns × (0.99,1.01) ~ (p=0.445)
RegexpMatchEasy1_32 138ns × (1.00,1.01) 139ns × (0.99,1.02) ~ (p=0.281)
RegexpMatchEasy1_1K 887ns × (1.00,1.01) 887ns × (1.00,1.00) ~ (p=0.610)
RegexpMatchMedium_32 251ns × (1.00,1.02) 255ns × (0.99,1.01) +1.42% (p=0.000)
RegexpMatchMedium_1K 72.7µs × (1.00,1.00) 72.6µs × (1.00,1.00) ~ (p=0.097)
RegexpMatchHard_32 3.85µs × (1.00,1.00) 3.84µs × (1.00,1.00) -0.31% (p=0.000)
RegexpMatchHard_1K 117µs × (1.00,1.00) 117µs × (1.00,1.00) ~ (p=0.704)
Revcomp 923ms × (0.98,1.02) 925ms × (0.99,1.01) ~ (p=0.574)
Template 126ms × (0.98,1.03) 130ms × (0.99,1.01) +3.28% (p=0.000)
TimeParse 631ns × (0.99,1.02) 626ns × (1.00,1.00) ~ (p=0.053)
TimeFormat 660ns × (0.99,1.01) 662ns × (0.99,1.02) ~ (p=0.398)
Change-Id: I59c03d329fe7bc178a31477c6f1f01062b881041
Reviewed-on: https://go-review.googlesource.com/9993
Reviewed-by: Austin Clements <austin@google.com>
2015-05-13 08:48:05 -06:00
|
|
|
if !writeBarrierEnabled {
|
|
|
|
memmove(p, old.array, lenmem)
|
|
|
|
} else {
|
|
|
|
for i := uintptr(0); i < lenmem; i += et.size {
|
|
|
|
typedmemmove(et, add(p, i), add(old.array, i))
|
|
|
|
}
|
2014-12-22 20:42:05 -07:00
|
|
|
}
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:01:54 -06:00
|
|
|
return slice{p, old.len, newcap}
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:01:54 -06:00
|
|
|
func slicecopy(to, fm slice, width uintptr) int {
|
2014-12-30 13:31:17 -07:00
|
|
|
if fm.len == 0 || to.len == 0 {
|
2014-07-31 13:43:40 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
n := fm.len
|
|
|
|
if to.len < n {
|
|
|
|
n = to.len
|
|
|
|
}
|
|
|
|
|
2014-12-30 13:31:17 -07:00
|
|
|
if width == 0 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2014-07-31 13:43:40 -06:00
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&to))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(slicecopy)
|
2014-09-04 13:53:45 -06:00
|
|
|
racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
|
|
|
|
racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
size := uintptr(n) * width
|
|
|
|
if size == 1 { // common case worth about 2x to do here
|
|
|
|
// TODO: is this still worth it with new memmove impl?
|
|
|
|
*(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
|
|
|
|
} else {
|
|
|
|
memmove(to.array, fm.array, size)
|
|
|
|
}
|
|
|
|
return int(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func slicestringcopy(to []byte, fm string) int {
|
|
|
|
if len(fm) == 0 || len(to) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(fm)
|
|
|
|
if len(to) < n {
|
|
|
|
n = len(to)
|
|
|
|
}
|
|
|
|
|
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&to))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(slicestringcopy)
|
2014-09-04 13:53:45 -06:00
|
|
|
racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
|
2014-07-31 13:43:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
memmove(unsafe.Pointer(&to[0]), unsafe.Pointer((*stringStruct)(unsafe.Pointer(&fm)).str), uintptr(n))
|
|
|
|
return n
|
|
|
|
}
|