Skip to content

Commit 878795a

Browse files
optimize configmeta generation (#58521)
* optimize config meta building Signed-off-by: Rama Chavali <[email protected]> * add benchmark Signed-off-by: Rama Chavali <[email protected]> * remove string builder Signed-off-by: Rama Chavali <[email protected]> --------- Signed-off-by: Rama Chavali <[email protected]>
1 parent bb1b3c2 commit 878795a

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

pilot/pkg/networking/util/util.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sort"
2323
"strconv"
2424
"strings"
25+
"sync"
2526

2627
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2728
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
@@ -92,6 +93,20 @@ const (
9293
AlpnOverrideMetadataKey = "alpn_override"
9394
)
9495

96+
// kindToKebabCache caches the conversion from CamelCase Kind to kebab-case.
97+
// This avoids repeated string allocations for the same Kind values.
98+
var kindToKebabCache sync.Map
99+
100+
// getKebabKind returns the kebab-case version of a Kind, using a cache to avoid repeated allocations.
101+
func getKebabKind(kind string) string {
102+
if cached, ok := kindToKebabCache.Load(kind); ok {
103+
return cached.(string)
104+
}
105+
kebab := strcase.CamelCaseToKebabCase(kind)
106+
kindToKebabCache.Store(kind, kebab)
107+
return kebab
108+
}
109+
95110
// ALPNH2Only advertises that Proxy is going to use HTTP/2 when talking to the cluster.
96111
var ALPNH2Only = pm.ALPNH2Only
97112

@@ -356,17 +371,21 @@ func BuildConfigInfoMetadata(config config.Meta) *core.Metadata {
356371
func AddConfigInfoMetadata(metadata *core.Metadata, config config.Meta) *core.Metadata {
357372
if metadata == nil {
358373
metadata = &core.Metadata{
359-
FilterMetadata: map[string]*structpb.Struct{},
374+
FilterMetadata: make(map[string]*structpb.Struct, 1),
360375
}
361376
}
377+
362378
s := "/apis/" + config.GroupVersionKind.Group + "/" + config.GroupVersionKind.Version + "/namespaces/" + config.Namespace + "/" +
363-
strcase.CamelCaseToKebabCase(config.GroupVersionKind.Kind) + "/" + config.Name
364-
if _, ok := metadata.FilterMetadata[IstioMetadataKey]; !ok {
365-
metadata.FilterMetadata[IstioMetadataKey] = &structpb.Struct{
366-
Fields: map[string]*structpb.Value{},
379+
getKebabKind(config.GroupVersionKind.Kind) + "/" + config.Name
380+
381+
istioMeta, ok := metadata.FilterMetadata[IstioMetadataKey]
382+
if !ok {
383+
istioMeta = &structpb.Struct{
384+
Fields: make(map[string]*structpb.Value, 1),
367385
}
386+
metadata.FilterMetadata[IstioMetadataKey] = istioMeta
368387
}
369-
metadata.FilterMetadata[IstioMetadataKey].Fields["config"] = &structpb.Value{
388+
istioMeta.Fields["config"] = &structpb.Value{
370389
Kind: &structpb.Value_StringValue{
371390
StringValue: s,
372391
},

pilot/pkg/networking/util/util_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,3 +1864,54 @@ func TestMeshNetworksToEnvoyInternalAddressConfig(t *testing.T) {
18641864
})
18651865
}
18661866
}
1867+
1868+
func BenchmarkAddConfigInfoMetadata(b *testing.B) {
1869+
configs := []config.Meta{
1870+
{
1871+
Name: "my-virtual-service",
1872+
Namespace: "default",
1873+
GroupVersionKind: gvk.VirtualService,
1874+
},
1875+
{
1876+
Name: "my-destination-rule",
1877+
Namespace: "istio-system",
1878+
GroupVersionKind: gvk.DestinationRule,
1879+
},
1880+
{
1881+
Name: "my-gateway",
1882+
Namespace: "production",
1883+
GroupVersionKind: gvk.Gateway,
1884+
},
1885+
}
1886+
1887+
b.Run("NilMetadata", func(b *testing.B) {
1888+
cfg := configs[0]
1889+
b.ReportAllocs()
1890+
b.ResetTimer()
1891+
for i := 0; i < b.N; i++ {
1892+
_ = AddConfigInfoMetadata(nil, cfg)
1893+
}
1894+
})
1895+
1896+
b.Run("ExistingMetadata", func(b *testing.B) {
1897+
cfg := configs[0]
1898+
b.ReportAllocs()
1899+
b.ResetTimer()
1900+
for i := 0; i < b.N; i++ {
1901+
meta := &core.Metadata{
1902+
FilterMetadata: map[string]*structpb.Struct{},
1903+
}
1904+
_ = AddConfigInfoMetadata(meta, cfg)
1905+
}
1906+
})
1907+
1908+
b.Run("MultipleConfigs", func(b *testing.B) {
1909+
b.ReportAllocs()
1910+
b.ResetTimer()
1911+
for i := 0; i < b.N; i++ {
1912+
for _, cfg := range configs {
1913+
_ = AddConfigInfoMetadata(nil, cfg)
1914+
}
1915+
}
1916+
})
1917+
}

0 commit comments

Comments
 (0)