Skip to content

Commit 8c65d90

Browse files
authored
feat: Add ldcontext.NewBuilderWithCapacity constructor (#40)
1 parent 2fc5d43 commit 8c65d90

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

internal/sharedtest/custom_attributes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
const (
10-
SmallNumberOfCustomAttributes = 2 //nolint:revive
11-
LargeNumberOfCustomAttributes = 20 //nolint:revive
10+
SmallNumberOfCustomAttributes = 2 //nolint:revive
11+
MiddleNumberOfCustomAttributes = 10 //nolint:revive
12+
LargeNumberOfCustomAttributes = 50
1213
)
1314

1415
type NameAndLDValue struct { //nolint:revive

ldcontext/builder_benchmark_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ func BenchmarkBuildWithNoCustomAttrs(b *testing.B) {
3131
}
3232

3333
func BenchmarkBuildWithCustomAttributes(b *testing.B) {
34-
for _, n := range []int{sharedtest.SmallNumberOfCustomAttributes, sharedtest.LargeNumberOfCustomAttributes} {
34+
for _, n := range []int{
35+
sharedtest.SmallNumberOfCustomAttributes,
36+
sharedtest.MiddleNumberOfCustomAttributes,
37+
sharedtest.LargeNumberOfCustomAttributes,
38+
} {
3539
b.Run(fmt.Sprintf("with %d attributes", n), func(b *testing.B) {
3640
attrs := sharedtest.MakeCustomAttributeNamesAndValues(n)
3741
b.ResetTimer()
@@ -46,6 +50,26 @@ func BenchmarkBuildWithCustomAttributes(b *testing.B) {
4650
}
4751
}
4852

53+
func BenchmarkBuildWithCustomAttributesWithCapacity(b *testing.B) {
54+
for _, n := range []int{
55+
sharedtest.SmallNumberOfCustomAttributes,
56+
sharedtest.MiddleNumberOfCustomAttributes,
57+
sharedtest.LargeNumberOfCustomAttributes,
58+
} {
59+
b.Run(fmt.Sprintf("with %d attributes", n), func(b *testing.B) {
60+
attrs := sharedtest.MakeCustomAttributeNamesAndValues(n)
61+
b.ResetTimer()
62+
for i := 0; i < b.N; i++ {
63+
builder := NewBuilderWithCapacity("key", n)
64+
for _, a := range attrs {
65+
builder.SetValue(a.Name, a.Value)
66+
}
67+
benchmarkContext = builder.Build()
68+
}
69+
})
70+
}
71+
}
72+
4973
func BenchmarkBuildWithPrivate(b *testing.B) {
5074
for i := 0; i < b.N; i++ {
5175
benchmarkContext = NewBuilder("key").Name("name").Private("name").Build()

ldcontext/builder_simple.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ func NewBuilder(key string) *Builder {
126126
return b.Key(key)
127127
}
128128

129+
// NewBuilderWithCapacity extends the behavior of NewBuilder to pre-allocate capacity for attributes.
130+
func NewBuilderWithCapacity(key string, capacity int) *Builder {
131+
b := &Builder{
132+
attributes: *ldvalue.ValueMapBuildWithCapacity(capacity),
133+
}
134+
return b.Key(key)
135+
}
136+
129137
// NewBuilderFromContext creates a Builder whose properties are the same as an existing
130138
// single context.
131139
//

0 commit comments

Comments
 (0)