labels: cope with mutating Builder during Range call
Although we had a different slice, the underlying memory was the same so any changes meant we could skip some values. Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
3c4ab7a069
commit
3743d87c56
|
@ -545,9 +545,12 @@ func (b *Builder) Get(n string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range calls f on each label in the Builder.
|
// Range calls f on each label in the Builder.
|
||||||
// If f calls Set or Del on b then this may affect what callbacks subsequently happen.
|
|
||||||
func (b *Builder) Range(f func(l Label)) {
|
func (b *Builder) Range(f func(l Label)) {
|
||||||
origAdd, origDel := b.add, b.del
|
// Stack-based arrays to avoid heap allocation in most cases.
|
||||||
|
var addStack [1024]Label
|
||||||
|
var delStack [1024]string
|
||||||
|
// Take a copy of add and del, so they are unaffected by calls to Set() or Del().
|
||||||
|
origAdd, origDel := append(addStack[:0], b.add...), append(delStack[:0], b.del...)
|
||||||
b.base.Range(func(l Label) {
|
b.base.Range(func(l Label) {
|
||||||
if !slices.Contains(origDel, l.Name) && !contains(origAdd, l.Name) {
|
if !slices.Contains(origDel, l.Name) && !contains(origAdd, l.Name) {
|
||||||
f(l)
|
f(l)
|
||||||
|
|
|
@ -599,9 +599,12 @@ func (b *Builder) Get(n string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range calls f on each label in the Builder.
|
// Range calls f on each label in the Builder.
|
||||||
// If f calls Set or Del on b then this may affect what callbacks subsequently happen.
|
|
||||||
func (b *Builder) Range(f func(l Label)) {
|
func (b *Builder) Range(f func(l Label)) {
|
||||||
origAdd, origDel := b.add, b.del
|
// Stack-based arrays to avoid heap allocation in most cases.
|
||||||
|
var addStack [1024]Label
|
||||||
|
var delStack [1024]string
|
||||||
|
// Take a copy of add and del, so they are unaffected by calls to Set() or Del().
|
||||||
|
origAdd, origDel := append(addStack[:0], b.add...), append(delStack[:0], b.del...)
|
||||||
b.base.Range(func(l Label) {
|
b.base.Range(func(l Label) {
|
||||||
if !slices.Contains(origDel, l.Name) && !contains(origAdd, l.Name) {
|
if !slices.Contains(origDel, l.Name) && !contains(origAdd, l.Name) {
|
||||||
f(l)
|
f(l)
|
||||||
|
|
Loading…
Reference in New Issue