Skip to content

Commit

Permalink
chore: Final batch of linting cleanup for Go 1.23 and newer golangci (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1r authored Sep 11, 2024
1 parent 93009d4 commit f226b59
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/kafka/ingester/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (c *consumer) flush(ctx context.Context) error {
wal.ReportSegmentStats(stats, c.metrics.segmentMetrics)

id := ulid.MustNew(ulid.Timestamp(time.Now()), rand.Reader).String()
if err := c.storage.PutObject(ctx, fmt.Sprintf(wal.Dir+id), c.flushBuf); err != nil {
if err := c.storage.PutObject(ctx, wal.Dir+id, c.flushBuf); err != nil {
return fmt.Errorf("failed to put object to object storage: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kafka/ingester/partition_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestPartitionReader_BasicFunctionality(t *testing.T) {
_, kafkaCfg := testkafka.CreateCluster(t, 1, "test-topic")
consumer := newMockConsumer()

consumerFactory := func(committer Committer) (Consumer, error) {
consumerFactory := func(_ Committer) (Consumer, error) {
return consumer, nil
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/logql/log/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"context"
"reflect"
"sync"
"unsafe"

Expand Down Expand Up @@ -383,11 +382,7 @@ func ReduceStages(stages []Stage) Stage {
}

func unsafeGetBytes(s string) []byte {
var buf []byte
p := unsafe.Pointer(&buf)
*(*string)(p) = s
(*reflect.SliceHeader)(p).Cap = len(s)
return buf
return unsafe.Slice(unsafe.StringData(s), len(s))
}

func unsafeGetString(buf []byte) string {
Expand Down
36 changes: 36 additions & 0 deletions pkg/logql/log/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,42 @@ func TestKeepLabelsPipeline(t *testing.T) {

}

func TestUnsafeGetBytes(t *testing.T) {
tests := []struct {
name string
input string
want []byte
}{
{
name: "empty string",
input: "",
want: nil,
},
{
name: "simple string",
input: "hello",
want: []byte{'h', 'e', 'l', 'l', 'o'},
},
{
name: "string with spaces",
input: "hello world",
want: []byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'},
},
{
name: "string with special characters",
input: "hello\nworld\t!",
want: []byte{'h', 'e', 'l', 'l', 'o', '\n', 'w', 'o', 'r', 'l', 'd', '\t', '!'},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := unsafeGetBytes(tt.input)
require.Equal(t, tt.want, got)
})
}
}

func Benchmark_Pipeline(b *testing.B) {
b.ReportAllocs()

Expand Down
9 changes: 1 addition & 8 deletions pkg/logql/sketch/topk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sketch

import (
"container/heap"
"reflect"
"sort"
"unsafe"

Expand Down Expand Up @@ -210,14 +209,8 @@ func (t *Topk) updateBF(removed, added string) {
}
}

// todo: is there a way to save more bytes/allocs via a pool?
func unsafeGetBytes(s string) []byte {
if s == "" {
return nil // or []byte{}
}
return (*[0x7fff0000]byte)(unsafe.Pointer(
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
)[:len(s):len(s)]
return unsafe.Slice(unsafe.StringData(s), len(s))
}

// Observe is our sketch event observation function, which is a bit more complex than the original count min sketch + heap TopK
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/bloom/v1/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestMergeBuilderFingerprintCollision(t *testing.T) {
}

// We're not testing the ability to extend a bloom in this test
pop := func(s *Series, _ iter.SizedIterator[*Bloom], _ ChunkRefs, ch chan *BloomCreation) {
pop := func(_ *Series, _ iter.SizedIterator[*Bloom], _ ChunkRefs, ch chan *BloomCreation) {
bloom := NewBloom()
stats := indexingInfo{
sourceBytes: int(bloom.Capacity()) / 8,
Expand Down
7 changes: 1 addition & 6 deletions pkg/storage/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"fmt"
"hash/crc32"
"reflect"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -215,11 +214,7 @@ func readOneHexPart(hex []byte) (part []byte, i int) {
}

func unsafeGetBytes(s string) []byte {
var buf []byte
p := unsafe.Pointer(&buf)
*(*string)(p) = s
(*reflect.SliceHeader)(p).Cap = len(s)
return buf
return unsafe.Slice(unsafe.StringData(s), len(s))
}

func unsafeGetString(buf []byte) string {
Expand Down

0 comments on commit f226b59

Please sign in to comment.