Skip to content

Commit 53f7de2

Browse files
authored
Update golangci-lint and refactor code after update to 1.21 standards (#3313)
* Update golangci-lint and target go version 1.21 * Refactor deprecated code paths * remove random seeding * migrate from reflect.SliceHeader over to unsafe.
1 parent 616e81a commit 53f7de2

File tree

11 files changed

+38
-86
lines changed

11 files changed

+38
-86
lines changed

.golangci.yml

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# options for analysis running
55
run:
6-
go: "1.19"
6+
go: "1.21"
77
# default concurrency is a available CPU number
88
concurrency: 16
99

@@ -21,26 +21,11 @@ run:
2121

2222
modules-download-mode: readonly
2323

24-
# which dirs to skip: they won't be analyzed;
25-
# can use regexp here: generated.*, regexp is applied on full path;
26-
# default value is empty list, but next dirs are always skipped independently
27-
# from this option's value:
28-
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
29-
skip-dirs:
30-
- win_eventlog$
31-
- pkg/og
32-
# which files to skip: they will be analyzed, but issues from them
33-
# won't be reported. Default value is empty list, but there is
34-
# no need to include all autogenerated files, we confidently recognize
35-
# autogenerated files. If it's not please let us know.
36-
skip-files:
37-
- .*.pb.go
38-
- .*.y.go
39-
- .*.rl.go
4024
# output configuration options
4125
output:
4226
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
43-
format: colored-line-number
27+
formats:
28+
- format: colored-line-number
4429

4530
# print lines of code with issue, default is true
4631
print-issued-lines: true
@@ -50,7 +35,7 @@ output:
5035

5136
linters-settings:
5237
goimports:
53-
local-prefixes: github.com/grafana/pyroscope/pkg,github.com/grafana/pyroscope/api,github.com/grafana/pyroscope/tools
38+
local-prefixes: github.com/grafana/pyroscope/,github.com/grafana/pyroscope/api,github.com/grafana/pyroscope/tools,github.com/grafana/pyroscope/ebpf
5439

5540
depguard:
5641
rules:
@@ -87,3 +72,21 @@ issues:
8772
- Error return value of .*log\.Logger\)\.Log\x60 is not checked
8873
- Error return value of .*.Log.* is not checked
8974
- Error return value of `` is not checked
75+
76+
# which dirs to skip: they won't be analyzed;
77+
# can use regexp here: generated.*, regexp is applied on full path;
78+
# default value is empty list, but next dirs are always skipped independently
79+
# from this option's value:
80+
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
81+
exclude-dirs:
82+
- win_eventlog$
83+
- pkg/og
84+
# which files to skip: they will be analyzed, but issues from them
85+
# won't be reported. Default value is empty list, but there is
86+
# no need to include all autogenerated files, we confidently recognize
87+
# autogenerated files. If it's not please let us know.
88+
exclude-files:
89+
- .*.pb.go
90+
- .*.y.go
91+
- .*.rl.go
92+

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ $(BIN)/buf: Makefile
260260

261261
$(BIN)/golangci-lint: Makefile
262262
@mkdir -p $(@D)
263-
GOBIN=$(abspath $(@D)) $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0
263+
GOBIN=$(abspath $(@D)) $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.2
264264

265265
$(BIN)/protoc-gen-go: Makefile go.mod
266266
@mkdir -p $(@D)

pkg/frontend/frontend_scheduler_worker.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ import (
2929
"github.com/grafana/pyroscope/pkg/util/servicediscovery"
3030
)
3131

32-
func init() {
33-
rand.Seed(time.Now().UnixNano())
34-
}
35-
3632
const (
3733
schedulerAddressLabel = "scheduler_address"
3834
// schedulerWorkerCancelChanCapacity should be at least as big as the number of sub-queries issued by a single query

pkg/model/stacktraces.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"container/heap"
66
"io"
7-
"reflect"
87
"sync"
98
"unsafe"
109

@@ -303,11 +302,5 @@ func (t *StacktraceTree) Bytes(dst io.Writer, maxNodes int64, funcs []string) {
303302
}
304303

305304
func unsafeStringBytes(s string) []byte {
306-
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
307-
var b []byte
308-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
309-
hdr.Data = uintptr(p)
310-
hdr.Cap = len(s)
311-
hdr.Len = len(s)
312-
return b
305+
return unsafe.Slice(unsafe.StringData(s), len(s))
313306
}

pkg/phlaredb/symdb/dedup_slice.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package symdb
44
import (
55
"fmt"
66
"hash/maphash"
7-
"reflect"
87
"sort"
98
"sync"
109
"unsafe"
@@ -424,23 +423,17 @@ func hashLines(s []schemav1.InMemoryLine) uint64 {
424423
if len(s) == 0 {
425424
return 0
426425
}
427-
var b []byte
428-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
429-
hdr.Len = len(s) * int(lineSize)
430-
hdr.Cap = hdr.Len
431-
hdr.Data = uintptr(unsafe.Pointer(&s[0]))
426+
p := (*byte)(unsafe.Pointer(&s[0]))
427+
b := unsafe.Slice(p, len(s)*int(lineSize))
432428
return maphash.Bytes(mapHashSeed, b)
433429
}
434430

435431
func hashLocations(s []uint64) uint64 {
436432
if len(s) == 0 {
437433
return 0
438434
}
439-
var b []byte
440-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
441-
hdr.Len = len(s) * 8
442-
hdr.Cap = hdr.Len
443-
hdr.Data = uintptr(unsafe.Pointer(&s[0]))
435+
p := (*byte)(unsafe.Pointer(&s[0]))
436+
b := unsafe.Slice(p, len(s)*8)
444437
return maphash.Bytes(mapHashSeed, b)
445438
}
446439

pkg/phlaredb/symdb/resolver_pprof_truncate.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package symdb
22

33
import (
4-
"reflect"
54
"unsafe"
65

76
googlev1 "github.com/grafana/pyroscope/api/gen/proto/go/google/v1"
@@ -228,13 +227,11 @@ func truncateLocations(locations []uint64, functions []int32, offset int, symbol
228227
}
229228

230229
func uint64sliceString(u []uint64) string {
231-
var s string
232-
if len(u) != 0 {
233-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s))
234-
hdr.Data = uintptr(unsafe.Pointer(&u[0]))
235-
hdr.Len = len(u) * 8
230+
if len(u) == 0 {
231+
return ""
236232
}
237-
return s
233+
p := (*byte)(unsafe.Pointer(&u[0]))
234+
return unsafe.String(p, len(u)*8)
238235
}
239236

240237
func (r *pprofProtoTruncatedSymbols) createStubSample() {

pkg/pprof/fix_go_heap_truncated.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package pprof
22

33
import (
44
"bytes"
5-
"reflect"
65
"slices"
76
"sort"
87
"unsafe"
@@ -224,11 +223,8 @@ func topToken(s []uint64) []byte {
224223
}
225224

226225
func locBytes(s []uint64) []byte {
227-
size := len(s) * 8
228-
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
229-
h.Len = size
230-
h.Cap = size
231-
return *(*[]byte)(unsafe.Pointer(h))
226+
p := (*byte)(unsafe.Pointer(&s[0]))
227+
return unsafe.Slice(p, len(s)*8)
232228
}
233229

234230
func unsafeString(b []byte) string {

pkg/pprof/pprof.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"io"
88
"os"
9-
"reflect"
109
"sort"
1110
"strings"
1211
"sync"
@@ -578,12 +577,8 @@ func uint64Bytes(s []uint64) []byte {
578577
if len(s) == 0 {
579578
return nil
580579
}
581-
var bs []byte
582-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
583-
hdr.Len = len(s) * 8
584-
hdr.Cap = hdr.Len
585-
hdr.Data = uintptr(unsafe.Pointer(&s[0]))
586-
return bs
580+
p := (*byte)(unsafe.Pointer(&s[0]))
581+
return unsafe.Slice(p, len(s)*8)
587582
}
588583

589584
type SamplesByLabels []*profilev1.Sample

pkg/querier/worker/scheduler_processor.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ import (
3939
"github.com/grafana/pyroscope/pkg/util/httpgrpcutil"
4040
)
4141

42-
func init() {
43-
rand.Seed(time.Now().UnixNano())
44-
}
45-
4642
var processorBackoffConfig = backoff.Config{
4743
MinBackoff: 250 * time.Millisecond,
4844
MaxBackoff: 2 * time.Second,

pkg/util/unsafe.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)