apiextensions: Benchmark escaping in SchemaHas

SchemaHas needs to escape the apiextensions.JSONSchemaProps pointer
since it's calling an arbitrary predicate function that can keep a copy
of the pointer (even though it shouldn't, but the compiler can't know
that). The leak is resulting in a fair amount of memory allocation when
installing many CRDs in a row (about 3% of the memory allocated happens
in this method).
This commit is contained in:
Antoine Pelisse 2023-01-30 09:46:23 -08:00
parent 7b243cef1a
commit a7c0467e01

View File

@ -8624,6 +8624,30 @@ func TestSchemaHasDefaults(t *testing.T) {
}
}
func BenchmarkSchemaHas(b *testing.B) {
scheme := runtime.NewScheme()
codecs := serializer.NewCodecFactory(scheme)
if err := apiextensions.AddToScheme(scheme); err != nil {
b.Fatal(err)
}
fuzzerFuncs := fuzzer.MergeFuzzerFuncs(apiextensionsfuzzer.Funcs)
seed := int64(5577006791947779410)
f := fuzzer.FuzzerFor(fuzzerFuncs, rand.NewSource(seed), codecs)
// fuzz internal types
schema := &apiextensions.JSONSchemaProps{}
f.NilChance(0).NumElements(10, 10).MaxDepth(10).Fuzz(schema)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if SchemaHas(schema, func(_ *apiextensions.JSONSchemaProps) bool {
return false
}) {
b.Errorf("Function returned true")
}
}
}
var example = apiextensions.JSON(`"This is an example"`)
var validValidationSchema = &apiextensions.JSONSchemaProps{