attribute: preallocate map in NewAllowKeysFilter and NewDenyKeysFilter (#6455)

preallocate map in `NewAllowKeysFilter` and `NewDenyKeysFilter` to avoid
necessary allocations
This commit is contained in:
ian 2025-03-18 23:38:17 +08:00 committed by GitHub
parent e15c058035
commit 8dc08e26b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 2 deletions

View File

@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- ⚠️ Update `github.com/prometheus/client_golang` to `v1.21.1`, which changes the `NameValidationScheme` to `UTF8Validation`. - ⚠️ Update `github.com/prometheus/client_golang` to `v1.21.1`, which changes the `NameValidationScheme` to `UTF8Validation`.
This allows metrics names to keep original delimiters (e.g. `.`), rather than replacing with underscores. This allows metrics names to keep original delimiters (e.g. `.`), rather than replacing with underscores.
This can be reverted by setting `github.com/prometheus/common/model.NameValidationScheme` to `LegacyValidation` in `github.com/prometheus/common/model`. (#6433) This can be reverted by setting `github.com/prometheus/common/model.NameValidationScheme` to `LegacyValidation` in `github.com/prometheus/common/model`. (#6433)
- Initialize map with `len(keys)` in `NewAllowKeysFilter` and `NewDenyKeysFilter` to avoid unnecessary allocations in `go.opentelemetry.io/otel/attribute`. (#6455)
### Fixes ### Fixes

View File

@ -19,7 +19,7 @@ func NewAllowKeysFilter(keys ...Key) Filter {
return func(kv KeyValue) bool { return false } return func(kv KeyValue) bool { return false }
} }
allowed := make(map[Key]struct{}) allowed := make(map[Key]struct{}, len(keys))
for _, k := range keys { for _, k := range keys {
allowed[k] = struct{}{} allowed[k] = struct{}{}
} }
@ -38,7 +38,7 @@ func NewDenyKeysFilter(keys ...Key) Filter {
return func(kv KeyValue) bool { return true } return func(kv KeyValue) bool { return true }
} }
forbid := make(map[Key]struct{}) forbid := make(map[Key]struct{}, len(keys))
for _, k := range keys { for _, k := range keys {
forbid[k] = struct{}{} forbid[k] = struct{}{}
} }