Skip to content

Commit

Permalink
🧹 v3 (chore): replace dictpool with sync.Map
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Nov 12, 2022
1 parent 24fd165 commit 73b43cc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
18 changes: 7 additions & 11 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/gofiber/utils/v2"
"github.com/savsgio/dictpool"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ type DefaultCtx struct {
values [maxParams]string // Route parameter values
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
matched bool // Non use route matched
viewBindMap *dictpool.Dict // Default view map to bind template engine
viewBindMap sync.Map // Default view map to bind template engine
bind *Bind // Default bind reference
redirect *Redirect // Default redirect reference
redirectionMessages []string // Messages of the previous redirect
Expand Down Expand Up @@ -976,11 +975,8 @@ func (c *DefaultCtx) Redirect() *Redirect {
// Variables are read by the Render method and may be overwritten.
func (c *DefaultCtx) BindVars(vars Map) error {
// init viewBindMap - lazy map
if c.viewBindMap == nil {
c.viewBindMap = dictpool.AcquireDict()
}
for k, v := range vars {
c.viewBindMap.Set(k, v)
c.viewBindMap.Store(k, v)
}

return nil
Expand Down Expand Up @@ -1081,11 +1077,11 @@ func (c *DefaultCtx) Render(name string, bind Map, layouts ...string) error {

func (c *DefaultCtx) renderExtensions(bind Map) {
// Bind view map
if c.viewBindMap != nil {
for _, v := range c.viewBindMap.D {
bind[v.Key] = v.Value
}
}
c.viewBindMap.Range(func(key, value any) bool {
bind[key.(string)] = value

return true
})

// Check if the PassLocalsToViews option is enabled (by default it is disabled)
if c.app.config.PassLocalsToViews {
Expand Down
7 changes: 2 additions & 5 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"crypto/tls"
"io"
"mime/multipart"
"sync"

"github.com/savsgio/dictpool"
"github.com/valyala/fasthttp"
)

Expand Down Expand Up @@ -439,10 +439,7 @@ func (c *DefaultCtx) release() {
c.fasthttp = nil
c.bind = nil
c.redirectionMessages = c.redirectionMessages[:0]
if c.viewBindMap != nil {
dictpool.ReleaseDict(c.viewBindMap)
c.viewBindMap = nil
}
c.viewBindMap = sync.Map{}
if c.redirect != nil {
ReleaseRedirect(c.redirect)
c.redirect = nil
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/google/uuid v1.3.0
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94
github.com/stretchr/testify v1.8.1
github.com/tinylib/msgp v1.1.6
github.com/valyala/bytebufferpool v1.0.0
Expand All @@ -21,7 +20,6 @@ require (
github.com/klauspost/compress v1.15.12 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4=
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8=
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo=
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down

1 comment on commit 73b43cc

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 73b43cc Previous: 5cac575 Ratio
Benchmark_AcquireCtx 1562 ns/op 1568 B/op 5 allocs/op 615.4 ns/op 1568 B/op 5 allocs/op 2.54

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.