Use zeropool.Pool to workaround SA6002 (#12189)
* Use zeropool.Pool to workaround SA6002 I built a tiny library called https://github.com/colega/zeropool to workaround the SA6002 staticheck issue. While searching for the references of that SA6002 staticheck issues on Github first results was Prometheus itself, with quite a lot of ignores of it. This changes the usages of `sync.Pool` to `zeropool.Pool[T]` where a pointer is not available. Also added a benchmark for HeadAppender Append/Commit when series already exist, which is one of the most usual cases IMO, as I didn't find any. Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Improve BenchmarkHeadAppender with more cases Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * A little copying is better than a little dependency https://www.youtube.com/watch?v=PAAkCSZUG1c&t=9m28s Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Fix imports order Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Add license header Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Copyright should be on one of the first 3 lines Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Use require.Equal for testing I don't depend on testify in my lib, but here we have it available. Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Avoid flaky test Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> * Also use zeropool for pointsPool in engine.go Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com> --------- Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>pull/12133/head
parent
211ae4f1f0
commit
6e2905a4d4
@ -0,0 +1,77 @@ |
||||
// Copyright 2023 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Package zeropool provides a zero-allocation type-safe alternative for sync.Pool, used to workaround staticheck SA6002.
|
||||
// The contents of this package are brought from https://github.com/colega/zeropool because "little copying is better than little dependency".
|
||||
|
||||
package zeropool |
||||
|
||||
import "sync" |
||||
|
||||
// Pool is a type-safe pool of items that does not allocate pointers to items.
|
||||
// That is not entirely true, it does allocate sometimes, but not most of the time,
|
||||
// just like the usual sync.Pool pools items most of the time, except when they're evicted.
|
||||
// It does that by storing the allocated pointers in a secondary pool instead of letting them go,
|
||||
// so they can be used later to store the items again.
|
||||
//
|
||||
// Zero value of Pool[T] is valid, and it will return zero values of T if nothing is pooled.
|
||||
type Pool[T any] struct { |
||||
// items holds pointers to the pooled items, which are valid to be used.
|
||||
items sync.Pool |
||||
// pointers holds just pointers to the pooled item types.
|
||||
// The values referenced by pointers are not valid to be used (as they're used by some other caller)
|
||||
// and it is safe to overwrite these pointers.
|
||||
pointers sync.Pool |
||||
} |
||||
|
||||
// New creates a new Pool[T] with the given function to create new items.
|
||||
// A Pool must not be copied after first use.
|
||||
func New[T any](item func() T) Pool[T] { |
||||
return Pool[T]{ |
||||
items: sync.Pool{ |
||||
New: func() interface{} { |
||||
val := item() |
||||
return &val |
||||
}, |
||||
}, |
||||
} |
||||
} |
||||
|
||||
// Get returns an item from the pool, creating a new one if necessary.
|
||||
// Get may be called concurrently from multiple goroutines.
|
||||
func (p *Pool[T]) Get() T { |
||||
pooled := p.items.Get() |
||||
if pooled == nil { |
||||
// The only way this can happen is when someone is using the zero-value of zeropool.Pool, and items pool is empty.
|
||||
// We don't have a pointer to store in p.pointers, so just return the empty value.
|
||||
var zero T |
||||
return zero |
||||
} |
||||
|
||||
ptr := pooled.(*T) |
||||
item := *ptr // ptr still holds a reference to a copy of item, but nobody will use it.
|
||||
p.pointers.Put(ptr) |
||||
return item |
||||
} |
||||
|
||||
// Put adds an item to the pool.
|
||||
func (p *Pool[T]) Put(item T) { |
||||
var ptr *T |
||||
if pooled := p.pointers.Get(); pooled != nil { |
||||
ptr = pooled.(*T) |
||||
} else { |
||||
ptr = new(T) |
||||
} |
||||
*ptr = item |
||||
p.items.Put(ptr) |
||||
} |
||||
@ -0,0 +1,178 @@ |
||||
// Copyright 2023 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package zeropool_test |
||||
|
||||
import ( |
||||
"math" |
||||
"sync" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
"go.uber.org/atomic" |
||||
|
||||
"github.com/prometheus/prometheus/util/zeropool" |
||||
) |
||||
|
||||
func TestPool(t *testing.T) { |
||||
t.Run("provides correct values", func(t *testing.T) { |
||||
pool := zeropool.New(func() []byte { return make([]byte, 1024) }) |
||||
item1 := pool.Get() |
||||
require.Equal(t, 1024, len(item1)) |
||||
|
||||
item2 := pool.Get() |
||||
require.Equal(t, 1024, len(item2)) |
||||
|
||||
pool.Put(item1) |
||||
pool.Put(item2) |
||||
|
||||
item1 = pool.Get() |
||||
require.Equal(t, 1024, len(item1)) |
||||
|
||||
item2 = pool.Get() |
||||
require.Equal(t, 1024, len(item2)) |
||||
}) |
||||
|
||||
t.Run("is not racy", func(t *testing.T) { |
||||
pool := zeropool.New(func() []byte { return make([]byte, 1024) }) |
||||
|
||||
const iterations = 1e6 |
||||
const concurrency = math.MaxUint8 |
||||
var counter atomic.Int64 |
||||
|
||||
do := make(chan struct{}, 1e6) |
||||
for i := 0; i < iterations; i++ { |
||||
do <- struct{}{} |
||||
} |
||||
close(do) |
||||
|
||||
run := make(chan struct{}) |
||||
done := sync.WaitGroup{} |
||||
done.Add(concurrency) |
||||
for i := 0; i < concurrency; i++ { |
||||
go func(worker int) { |
||||
<-run |
||||
for range do { |
||||
item := pool.Get() |
||||
item[0] = byte(worker) |
||||
counter.Add(1) // Counts and also adds some delay to add raciness.
|
||||
if item[0] != byte(worker) { |
||||
panic("wrong value") |
||||
} |
||||
pool.Put(item) |
||||
} |
||||
done.Done() |
||||
}(i) |
||||
} |
||||
close(run) |
||||
done.Wait() |
||||
t.Logf("Done %d iterations", counter.Load()) |
||||
}) |
||||
|
||||
t.Run("does not allocate", func(t *testing.T) { |
||||
pool := zeropool.New(func() []byte { return make([]byte, 1024) }) |
||||
// Warm up, this will alloate one slice.
|
||||
slice := pool.Get() |
||||
pool.Put(slice) |
||||
|
||||
allocs := testing.AllocsPerRun(1000, func() { |
||||
slice := pool.Get() |
||||
pool.Put(slice) |
||||
}) |
||||
// Don't compare to 0, as when passing all the tests the GC could flush the pools during this test and we would allocate.
|
||||
// Just check that it's less than 1 on average, which is mostly the same thing.
|
||||
require.Less(t, allocs, 1., "Should not allocate.") |
||||
}) |
||||
|
||||
t.Run("zero value is valid", func(t *testing.T) { |
||||
var pool zeropool.Pool[[]byte] |
||||
slice := pool.Get() |
||||
pool.Put(slice) |
||||
|
||||
allocs := testing.AllocsPerRun(1000, func() { |
||||
slice := pool.Get() |
||||
pool.Put(slice) |
||||
}) |
||||
// Don't compare to 0, as when passing all the tests the GC could flush the pools during this test and we would allocate.
|
||||
// Just check that it's less than 1 on average, which is mostly the same thing.
|
||||
require.Less(t, allocs, 1., "Should not allocate.") |
||||
}) |
||||
} |
||||
|
||||
func BenchmarkZeropoolPool(b *testing.B) { |
||||
pool := zeropool.New(func() []byte { return make([]byte, 1024) }) |
||||
|
||||
// Warmup
|
||||
item := pool.Get() |
||||
pool.Put(item) |
||||
|
||||
b.ResetTimer() |
||||
for i := 0; i < b.N; i++ { |
||||
item := pool.Get() |
||||
pool.Put(item) |
||||
} |
||||
} |
||||
|
||||
// BenchmarkSyncPoolValue uses sync.Pool to store values, which makes an allocation on each Put call.
|
||||
func BenchmarkSyncPoolValue(b *testing.B) { |
||||
pool := sync.Pool{New: func() any { |
||||
return make([]byte, 1024) |
||||
}} |
||||
|
||||
// Warmup
|
||||
item := pool.Get().([]byte) |
||||
pool.Put(item) //nolint:staticcheck // This allocates.
|
||||
|
||||
b.ResetTimer() |
||||
for i := 0; i < b.N; i++ { |
||||
item := pool.Get().([]byte) |
||||
pool.Put(item) //nolint:staticcheck // This allocates.
|
||||
} |
||||
} |
||||
|
||||
// BenchmarkSyncPoolNewPointer uses sync.Pool to store pointers, but it calls Put with a new pointer every time.
|
||||
func BenchmarkSyncPoolNewPointer(b *testing.B) { |
||||
pool := sync.Pool{New: func() any { |
||||
v := make([]byte, 1024) |
||||
return &v |
||||
}} |
||||
|
||||
// Warmup
|
||||
item := pool.Get().(*[]byte) |
||||
pool.Put(item) //nolint:staticcheck // This allocates.
|
||||
|
||||
b.ResetTimer() |
||||
for i := 0; i < b.N; i++ { |
||||
item := pool.Get().(*[]byte) |
||||
buf := *item |
||||
pool.Put(&buf) //nolint:staticcheck // New pointer.
|
||||
} |
||||
} |
||||
|
||||
// BenchmarkSyncPoolPointer illustrates the optimal usage of sync.Pool, not always possible.
|
||||
func BenchmarkSyncPoolPointer(b *testing.B) { |
||||
pool := sync.Pool{New: func() any { |
||||
v := make([]byte, 1024) |
||||
return &v |
||||
}} |
||||
|
||||
// Warmup
|
||||
item := pool.Get().(*[]byte) |
||||
pool.Put(item) |
||||
|
||||
b.ResetTimer() |
||||
for i := 0; i < b.N; i++ { |
||||
item := pool.Get().(*[]byte) |
||||
pool.Put(item) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue