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 <congqi.xia@zilliz.com>
pull/30936/head
congqixia 2024-03-19 14:05:05 +08:00 committed by GitHub
parent 8e293dc1ce
commit 74b7de3814
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 1 deletions

View File

@ -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
}