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 10, 2024
1 parent e327369 commit 4d5571e
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)
}()

Check warning on line 138 in schema.go

View check run for this annotation

Codecov / codecov/patch

schema.go#L135-L138

Added lines #L135 - L138 were not covered by tests
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{}

Check failure on line 617 in schema.go

View workflow job for this annotation

GitHub Actions / Lint

commentFormatting: put a space between `//` and comment text (gocritic)
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 4d5571e

Please sign in to comment.