mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-08 04:32:37 +00:00
apiextensions: Pool schemas in SchemaHas
Using a `sync.Pool` to re-use the buffers that have to escape in the predicate significantly reduces the number of allocations needed to run the SchemaHas method, as shown in the following example: ``` > benchstat old.bench new.bench name old time/op new time/op delta SchemaHas-8 11.0ms ± 0% 2.1ms ± 1% -80.60% (p=0.008 n=5+5) name old alloc/op new alloc/op delta SchemaHas-8 37.5MB ± 0% 0.0MB ± 0% -100.00% (p=0.008 n=5+5) name old allocs/op new allocs/op delta SchemaHas-8 73.1k ± 0% 0.0k -100.00% (p=0.008 n=5+5) ```
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
@@ -1364,6 +1365,22 @@ func HasSchemaWith(spec *apiextensions.CustomResourceDefinitionSpec, pred func(s
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var schemaPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return new(apiextensions.JSONSchemaProps)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func schemaHasRecurse(s *apiextensions.JSONSchemaProps, pred func(s *apiextensions.JSONSchemaProps) bool) bool {
|
||||||
|
if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
schema := schemaPool.Get().(*apiextensions.JSONSchemaProps)
|
||||||
|
defer schemaPool.Put(schema)
|
||||||
|
*schema = *s
|
||||||
|
return SchemaHas(schema, pred)
|
||||||
|
}
|
||||||
|
|
||||||
func SchemaHas(s *apiextensions.JSONSchemaProps, pred func(s *apiextensions.JSONSchemaProps) bool) bool {
|
func SchemaHas(s *apiextensions.JSONSchemaProps, pred func(s *apiextensions.JSONSchemaProps) bool) bool {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return false
|
return false
|
||||||
@@ -1374,60 +1391,60 @@ func SchemaHas(s *apiextensions.JSONSchemaProps, pred func(s *apiextensions.JSON
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.Items != nil {
|
if s.Items != nil {
|
||||||
if s.Items != nil && SchemaHas(s.Items.Schema, pred) {
|
if s.Items != nil && schemaHasRecurse(s.Items.Schema, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for i := range s.Items.JSONSchemas {
|
for i := range s.Items.JSONSchemas {
|
||||||
if SchemaHas(&s.Items.JSONSchemas[i], pred) {
|
if schemaHasRecurse(&s.Items.JSONSchemas[i], pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := range s.AllOf {
|
for i := range s.AllOf {
|
||||||
if SchemaHas(&s.AllOf[i], pred) {
|
if schemaHasRecurse(&s.AllOf[i], pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := range s.AnyOf {
|
for i := range s.AnyOf {
|
||||||
if SchemaHas(&s.AnyOf[i], pred) {
|
if schemaHasRecurse(&s.AnyOf[i], pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := range s.OneOf {
|
for i := range s.OneOf {
|
||||||
if SchemaHas(&s.OneOf[i], pred) {
|
if schemaHasRecurse(&s.OneOf[i], pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if SchemaHas(s.Not, pred) {
|
if schemaHasRecurse(s.Not, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for _, s := range s.Properties {
|
for _, s := range s.Properties {
|
||||||
if SchemaHas(&s, pred) {
|
if schemaHasRecurse(&s, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.AdditionalProperties != nil {
|
if s.AdditionalProperties != nil {
|
||||||
if SchemaHas(s.AdditionalProperties.Schema, pred) {
|
if schemaHasRecurse(s.AdditionalProperties.Schema, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, s := range s.PatternProperties {
|
for _, s := range s.PatternProperties {
|
||||||
if SchemaHas(&s, pred) {
|
if schemaHasRecurse(&s, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.AdditionalItems != nil {
|
if s.AdditionalItems != nil {
|
||||||
if SchemaHas(s.AdditionalItems.Schema, pred) {
|
if schemaHasRecurse(s.AdditionalItems.Schema, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, s := range s.Definitions {
|
for _, s := range s.Definitions {
|
||||||
if SchemaHas(&s, pred) {
|
if schemaHasRecurse(&s, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, d := range s.Dependencies {
|
for _, d := range s.Dependencies {
|
||||||
if SchemaHas(d.Schema, pred) {
|
if schemaHasRecurse(d.Schema, pred) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user