From 74b7de381409ed485743c39b8e1311d3783e8678 Mon Sep 17 00:00:00 2001 From: congqixia Date: Tue, 19 Mar 2024 14:05:05 +0800 Subject: [PATCH] enhance: Cache formatted key for param item (#31388) See also #30806 `formatKey` may cost lots of CPU on string processing under high QPS scenario, this PR adds a formattedKeys cache preventing string operation in each param get value. --------- Signed-off-by: Congqi Xia --- pkg/config/config.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 320261d3cd..fc93c086f7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,6 +20,8 @@ import ( "strings" "github.com/cockroachdb/errors" + + "github.com/milvus-io/milvus/pkg/util/typeutil" ) var ( @@ -51,6 +53,14 @@ func Init(opts ...Option) (*Manager, error) { return sourceManager, nil } +var formattedKeys = typeutil.NewConcurrentMap[string, string]() + func formatKey(key string) string { - return strings.NewReplacer("/", "", "_", "", ".", "").Replace(strings.ToLower(key)) + cached, ok := formattedKeys.Get(key) + if ok { + return cached + } + result := strings.NewReplacer("/", "", "_", "", ".", "").Replace(strings.ToLower(key)) + formattedKeys.Insert(key, result) + return result }