From d8542b1563f84b982cedaa84e70fde6e242e2ee1 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Wed, 10 Jan 2024 15:30:01 +0100 Subject: [PATCH] added pool for internal maps Signed-off-by: Frederic BIDON --- schema.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/schema.go b/schema.go index 6936ad6..92e71c3 100644 --- a/schema.go +++ b/schema.go @@ -17,6 +17,7 @@ package spec import ( "fmt" "strings" + "sync" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" @@ -131,7 +132,10 @@ func (r SchemaURL) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshal this from JSON func (r *SchemaURL) UnmarshalJSON(data []byte) error { - var v map[string]interface{} + v := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(v) + }() if err := json.Unmarshal(data, &v); err != nil { return err } @@ -610,7 +614,11 @@ func (s *Schema) UnmarshalJSON(data []byte) error { SwaggerSchemaProps: props.SwaggerSchemaProps, } - var d map[string]interface{} + //var d map[string]interface{} + d := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(d) + }() if err := json.Unmarshal(data, &d); err != nil { return err } @@ -643,3 +651,29 @@ func (s *Schema) UnmarshalJSON(data []byte) error { return nil } + +type mapPool struct { + *sync.Pool +} + +var poolOfMaps = mapPool{ + Pool: &sync.Pool{ + New: func() any { + return make(map[string]any) + }, + }, +} + +func (p mapPool) BorrowMap() map[string]any { + m := p.Get().(map[string]any) + // go1.21 clear(m) + for k := range m { + delete(m, k) + } + + return m +} + +func (p mapPool) RedeemMap(m map[string]any) { + p.Put(m) +}