From: Michael Hudson-Doyle Date: Fri, 10 Apr 2015 22:01:54 +0000 (+1200) Subject: runtime: merge slice and sliceStruct X-Git-Tag: go1.5beta1~1103 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ab4df700b841fcd9a5d249b77c547bad9574d948;p=gostls13.git runtime: merge slice and sliceStruct By removing type slice, renaming type sliceStruct to type slice and whacking until it compiles. Has a pleasing net reduction of conversions. Fixes #10188 Change-Id: I77202b8df637185b632fd7875a1fdd8d52c7a83c Reviewed-on: https://go-review.googlesource.com/8770 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- diff --git a/src/runtime/alg.go b/src/runtime/alg.go index 659c8d7d79..f24ebd1fb2 100644 --- a/src/runtime/alg.go +++ b/src/runtime/alg.go @@ -281,7 +281,7 @@ func stringHash(s string, seed uintptr) uintptr { } func bytesHash(b []byte, seed uintptr) uintptr { - s := (*sliceStruct)(unsafe.Pointer(&b)) + s := (*slice)(unsafe.Pointer(&b)) return memhash(s.array, seed, uintptr(s.len)) } @@ -305,7 +305,7 @@ func ifaceHash(i interface { // Testing adapter for memclr func memclrBytes(b []byte) { - s := (*sliceStruct)(unsafe.Pointer(&b)) + s := (*slice)(unsafe.Pointer(&b)) memclr(s.array, uintptr(s.len)) } diff --git a/src/runtime/cpuprof.go b/src/runtime/cpuprof.go index 055b2af018..0790852d97 100644 --- a/src/runtime/cpuprof.go +++ b/src/runtime/cpuprof.go @@ -396,8 +396,8 @@ Flush: } func uintptrBytes(p []uintptr) (ret []byte) { - pp := (*sliceStruct)(unsafe.Pointer(&p)) - rp := (*sliceStruct)(unsafe.Pointer(&ret)) + pp := (*slice)(unsafe.Pointer(&p)) + rp := (*slice)(unsafe.Pointer(&ret)) rp.array = pp.array rp.len = pp.len * int(unsafe.Sizeof(p[0])) diff --git a/src/runtime/export_test.go b/src/runtime/export_test.go index 58ac34283f..905218b22b 100644 --- a/src/runtime/export_test.go +++ b/src/runtime/export_test.go @@ -76,8 +76,10 @@ func GCMask(x interface{}) (ret []byte) { s := (*slice)(unsafe.Pointer(&ret)) systemstack(func() { var len uintptr - getgcmask(e.data, e._type, &s.array, &len) - s.len = uint(len) + var a *byte + getgcmask(e.data, e._type, &a, &len) + s.array = unsafe.Pointer(a) + s.len = int(len) s.cap = s.len }) return diff --git a/src/runtime/mbarrier.go b/src/runtime/mbarrier.go index 4233f01a99..2e3e4d8041 100644 --- a/src/runtime/mbarrier.go +++ b/src/runtime/mbarrier.go @@ -355,7 +355,7 @@ func typedslicecopy(typ *_type, dst, src slice) int { if !needwb() { memmove(dstp, srcp, uintptr(n)*typ.size) - return int(n) + return n } systemstack(func() { @@ -365,7 +365,7 @@ func typedslicecopy(typ *_type, dst, src slice) int { // out of the array they point into. dstp = add(dstp, uintptr(n-1)*typ.size) srcp = add(srcp, uintptr(n-1)*typ.size) - i := uint(0) + i := 0 for { typedmemmove(typ, dstp, srcp) if i++; i >= n { @@ -377,7 +377,7 @@ func typedslicecopy(typ *_type, dst, src slice) int { } else { // Copy forward, being careful not to move dstp/srcp // out of the array they point into. - i := uint(0) + i := 0 for { typedmemmove(typ, dstp, srcp) if i++; i >= n { diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go index c78005c9af..c5de8218c2 100644 --- a/src/runtime/mheap.go +++ b/src/runtime/mheap.go @@ -148,12 +148,12 @@ func recordspan(vh unsafe.Pointer, p unsafe.Pointer) { } var new []*mspan sp := (*slice)(unsafe.Pointer(&new)) - sp.array = (*byte)(sysAlloc(uintptr(n)*ptrSize, &memstats.other_sys)) + sp.array = sysAlloc(uintptr(n)*ptrSize, &memstats.other_sys) if sp.array == nil { throw("runtime: cannot allocate memory") } - sp.len = uint(len(h_allspans)) - sp.cap = uint(n) + sp.len = len(h_allspans) + sp.cap = n if len(h_allspans) > 0 { copy(new, h_allspans) // Don't free the old array if it's referenced by sweep. @@ -256,9 +256,9 @@ func mHeap_Init(h *mheap, spans_size uintptr) { } sp := (*slice)(unsafe.Pointer(&h_spans)) - sp.array = (*byte)(unsafe.Pointer(h.spans)) - sp.len = uint(spans_size / ptrSize) - sp.cap = uint(spans_size / ptrSize) + sp.array = unsafe.Pointer(h.spans) + sp.len = int(spans_size / ptrSize) + sp.cap = int(spans_size / ptrSize) } func mHeap_MapSpans(h *mheap) { diff --git a/src/runtime/print1.go b/src/runtime/print1.go index ba5799182a..6eff38168d 100644 --- a/src/runtime/print1.go +++ b/src/runtime/print1.go @@ -13,9 +13,9 @@ type hex uint64 func bytes(s string) (ret []byte) { rp := (*slice)(unsafe.Pointer(&ret)) sp := (*_string)(noescape(unsafe.Pointer(&s))) - rp.array = sp.str - rp.len = uint(sp.len) - rp.cap = uint(sp.len) + rp.array = unsafe.Pointer(sp.str) + rp.len = sp.len + rp.cap = sp.len return } diff --git a/src/runtime/runtime2.go b/src/runtime/runtime2.go index 1f4f0daec4..cdbaebcc09 100644 --- a/src/runtime/runtime2.go +++ b/src/runtime/runtime2.go @@ -87,12 +87,6 @@ type eface struct { data unsafe.Pointer } -type slice struct { - array *byte // actual data - len uint // number of elements - cap uint // allocated number of elements -} - // A guintptr holds a goroutine pointer, but typed as a uintptr // to bypass write barriers. It is used in the Gobuf goroutine state. // diff --git a/src/runtime/select.go b/src/runtime/select.go index 98ac5a3d61..2dd541b530 100644 --- a/src/runtime/select.go +++ b/src/runtime/select.go @@ -159,7 +159,7 @@ func selectdefaultImpl(sel *hselect, callerpc uintptr, so uintptr) { } func sellock(sel *hselect) { - lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)} + lockslice := slice{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)} lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice)) var c *hchan for _, c0 := range lockorder { @@ -181,7 +181,7 @@ func selunlock(sel *hselect) { // Now if the first M touches sel, it will access freed memory. n := int(sel.ncase) r := 0 - lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), n, n} + lockslice := slice{unsafe.Pointer(sel.lockorder), n, n} lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice)) // skip the default case if n > 0 && lockorder[0] == nil { @@ -221,7 +221,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) { print("select: sel=", sel, "\n") } - scaseslice := sliceStruct{unsafe.Pointer(&sel.scase), int(sel.ncase), int(sel.ncase)} + scaseslice := slice{unsafe.Pointer(&sel.scase), int(sel.ncase), int(sel.ncase)} scases := *(*[]scase)(unsafe.Pointer(&scaseslice)) var t0 int64 @@ -241,7 +241,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) { // optimizing (and needing to test). // generate permuted order - pollslice := sliceStruct{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)} + pollslice := slice{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)} pollorder := *(*[]uint16)(unsafe.Pointer(&pollslice)) for i := 0; i < int(sel.ncase); i++ { pollorder[i] = uint16(i) @@ -255,7 +255,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) { // sort the cases by Hchan address to get the locking order. // simple heap sort, to guarantee n log n time and constant stack footprint. - lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)} + lockslice := slice{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)} lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice)) for i := 0; i < int(sel.ncase); i++ { j := i diff --git a/src/runtime/slice.go b/src/runtime/slice.go index ae46d9c1ac..cf2510aeb2 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -8,14 +8,14 @@ import ( "unsafe" ) -type sliceStruct struct { +type slice struct { array unsafe.Pointer len int cap int } // TODO: take uintptrs instead of int64s? -func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct { +func makeslice(t *slicetype, len64, cap64 int64) slice { // 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, @@ -30,10 +30,10 @@ func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct { panic(errorString("makeslice: cap out of range")) } p := newarray(t.elem, uintptr(cap)) - return sliceStruct{p, len, cap} + return slice{p, len, cap} } -func growslice(t *slicetype, old sliceStruct, n int) sliceStruct { +func growslice(t *slicetype, old slice, n int) slice { if n < 1 { panic(errorString("growslice: invalid n")) } @@ -52,7 +52,7 @@ func growslice(t *slicetype, old sliceStruct, n int) sliceStruct { if et.size == 0 { // 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. - return sliceStruct{unsafe.Pointer(&zerobase), old.len, cap} + return slice{unsafe.Pointer(&zerobase), old.len, cap} } newcap := old.cap @@ -91,10 +91,10 @@ func growslice(t *slicetype, old sliceStruct, n int) sliceStruct { } } - return sliceStruct{p, old.len, newcap} + return slice{p, old.len, newcap} } -func slicecopy(to sliceStruct, fm sliceStruct, width uintptr) int { +func slicecopy(to, fm slice, width uintptr) int { if fm.len == 0 || to.len == 0 { return 0 } diff --git a/src/runtime/string.go b/src/runtime/string.go index 0ba309cf02..a5851b7abc 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -148,7 +148,7 @@ func stringtoslicebytetmp(s string) []byte { // for i, c := range []byte(str) str := (*stringStruct)(unsafe.Pointer(&s)) - ret := slice{array: (*byte)(str.str), len: uint(str.len), cap: uint(str.len)} + ret := slice{array: unsafe.Pointer(str.str), len: str.len, cap: str.len} return *(*[]byte)(unsafe.Pointer(&ret)) } @@ -266,9 +266,7 @@ func rawstring(size int) (s string, b []byte) { (*stringStruct)(unsafe.Pointer(&s)).str = p (*stringStruct)(unsafe.Pointer(&s)).len = size - (*slice)(unsafe.Pointer(&b)).array = (*uint8)(p) - (*slice)(unsafe.Pointer(&b)).len = uint(size) - (*slice)(unsafe.Pointer(&b)).cap = uint(size) + *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size} for { ms := maxstring @@ -286,9 +284,7 @@ func rawbyteslice(size int) (b []byte) { memclr(add(p, uintptr(size)), cap-uintptr(size)) } - (*slice)(unsafe.Pointer(&b)).array = (*uint8)(p) - (*slice)(unsafe.Pointer(&b)).len = uint(size) - (*slice)(unsafe.Pointer(&b)).cap = uint(cap) + *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)} return } @@ -303,9 +299,7 @@ func rawruneslice(size int) (b []rune) { memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4) } - (*slice)(unsafe.Pointer(&b)).array = (*uint8)(p) - (*slice)(unsafe.Pointer(&b)).len = uint(size) - (*slice)(unsafe.Pointer(&b)).cap = uint(mem / 4) + *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)} return }