Skip to content

Commit

Permalink
added pool for internal maps
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Jan 11, 2024
1 parent 5c7759f commit d8542b1
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"fmt"
"strings"
"sync"

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

0 comments on commit d8542b1

Please sign in to comment.