mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 14:09:41 -08:00
6e2905a4d4
* 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>
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
// 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)
|
|
}
|