Skip to content

Commit 88a545d

Browse files
ianlancetaylorgopherbot
authored andcommitted
runtime/coverage: use unsafe.Slice, not reflect.SliceHeader
Change-Id: I59c4757df83c12b4c8b85cdd523552c5e5e7bf95 Reviewed-on: https://go-review.googlesource.com/c/go/+/508977 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent eaa8419 commit 88a545d

File tree

2 files changed

+3
-22
lines changed

2 files changed

+3
-22
lines changed

src/runtime/coverage/apis.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"internal/coverage"
1010
"io"
11-
"reflect"
1211
"sync/atomic"
1312
"unsafe"
1413
)
@@ -158,13 +157,8 @@ func ClearCounters() error {
158157
// inconsistency when reading the counter array from the thread
159158
// running ClearCounters.
160159

161-
var sd []atomic.Uint32
162-
163-
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&sd))
164160
for _, c := range cl {
165-
bufHdr.Data = uintptr(unsafe.Pointer(c.Counters))
166-
bufHdr.Len = int(c.Len)
167-
bufHdr.Cap = int(c.Len)
161+
sd := unsafe.Slice((*atomic.Uint32)(unsafe.Pointer(c.Counters)), int(c.Len))
168162
for i := 0; i < len(sd); i++ {
169163
// Skip ahead until the next non-zero value.
170164
sdi := sd[i].Load()

src/runtime/coverage/emit.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"io"
1515
"os"
1616
"path/filepath"
17-
"reflect"
1817
"runtime"
1918
"strconv"
2019
"sync/atomic"
@@ -447,26 +446,16 @@ func (s *emitState) needMetaDataFile() bool {
447446
func writeMetaData(w io.Writer, metalist []rtcov.CovMetaBlob, cmode coverage.CounterMode, gran coverage.CounterGranularity, finalHash [16]byte) error {
448447
mfw := encodemeta.NewCoverageMetaFileWriter("<io.Writer>", w)
449448

450-
// Note: "sd" is re-initialized on each iteration of the loop
451-
// below, and would normally be declared inside the loop, but
452-
// placed here escape analysis since we capture it in bufHdr.
453-
var sd []byte
454-
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&sd))
455-
456449
var blobs [][]byte
457450
for _, e := range metalist {
458-
bufHdr.Data = uintptr(unsafe.Pointer(e.P))
459-
bufHdr.Len = int(e.Len)
460-
bufHdr.Cap = int(e.Len)
451+
sd := unsafe.Slice(e.P, int(e.Len))
461452
blobs = append(blobs, sd)
462453
}
463454
return mfw.Write(finalHash, blobs, cmode, gran)
464455
}
465456

466457
func (s *emitState) VisitFuncs(f encodecounter.CounterVisitorFn) error {
467-
var sd []atomic.Uint32
468458
var tcounters []uint32
469-
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&sd))
470459

471460
rdCounters := func(actrs []atomic.Uint32, ctrs []uint32) []uint32 {
472461
ctrs = ctrs[:0]
@@ -478,9 +467,7 @@ func (s *emitState) VisitFuncs(f encodecounter.CounterVisitorFn) error {
478467

479468
dpkg := uint32(0)
480469
for _, c := range s.counterlist {
481-
bufHdr.Data = uintptr(unsafe.Pointer(c.Counters))
482-
bufHdr.Len = int(c.Len)
483-
bufHdr.Cap = int(c.Len)
470+
sd := unsafe.Slice((*atomic.Uint32)(unsafe.Pointer(c.Counters)), int(c.Len))
484471
for i := 0; i < len(sd); i++ {
485472
// Skip ahead until the next non-zero value.
486473
sdi := sd[i].Load()

0 commit comments

Comments
 (0)