-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: reuse requests to reduce allocs (#127)
- Loading branch information
Showing
9 changed files
with
198 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package don | ||
|
||
var MakeNilCheck = makeNilCheck | ||
|
||
func NewRequestPool[T any](v T) pool[T] { | ||
return newRequestPool(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package don | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
type pool[T any] interface { | ||
Get() *T | ||
Put(*T) | ||
} | ||
|
||
type resetter interface { | ||
Reset() | ||
} | ||
|
||
var resetterType = reflect.TypeOf((*resetter)(nil)).Elem() | ||
|
||
type requestPool[T any] struct { | ||
pool sync.Pool | ||
reset func(*T) | ||
} | ||
|
||
func newRequestPool[T any](zero T) pool[T] { | ||
typ := reflect.TypeOf(zero) | ||
if typ == nil { | ||
return &fakePool[T]{&zero} | ||
} | ||
|
||
p := &requestPool[T]{} | ||
|
||
if typ.Kind() != reflect.Pointer { | ||
p.pool.New = func() any { | ||
return new(T) | ||
} | ||
p.reset = func(v *T) { | ||
*v = zero | ||
} | ||
} else { | ||
elem := typ.Elem() | ||
p.pool.New = func() any { | ||
v := reflect.New(elem).Interface().(T) //nolint:forcetypeassert | ||
return &v | ||
} | ||
|
||
if typ.Implements(resetterType) { | ||
p.reset = func(v *T) { | ||
any(*v).(resetter).Reset() //nolint:forcetypeassert | ||
} | ||
} else { | ||
zeroValue := reflect.New(elem).Elem() | ||
p.reset = func(v *T) { | ||
reflect.ValueOf(v).Elem().Elem().Set(zeroValue) | ||
} | ||
} | ||
} | ||
|
||
return p | ||
} | ||
|
||
func (p *requestPool[T]) Get() *T { | ||
return p.pool.Get().(*T) //nolint:forcetypeassert | ||
} | ||
|
||
func (p *requestPool[T]) Put(v *T) { | ||
p.reset(v) | ||
p.pool.Put(v) | ||
} | ||
|
||
type fakePool[T any] struct{ v *T } | ||
|
||
func (p *fakePool[T]) Get() *T { return p.v } | ||
|
||
func (p *fakePool[T]) Put(*T) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package don_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/abemedia/go-don" | ||
) | ||
|
||
func TestRequestPool(t *testing.T) { | ||
type item struct { | ||
String string | ||
Pointer *string | ||
} | ||
|
||
t.Run("Nil", func(t *testing.T) { | ||
var zero any | ||
pool := don.NewRequestPool(zero) | ||
|
||
pool.Put(pool.Get()) | ||
|
||
if !reflect.DeepEqual(&zero, pool.Get()) { | ||
t.Fatal("should be zero value") | ||
} | ||
}) | ||
|
||
t.Run("Struct", func(t *testing.T) { | ||
zero := item{} | ||
pool := don.NewRequestPool(zero) | ||
|
||
for i := 0; i < 100; i++ { | ||
v := pool.Get() | ||
v.String = "test" | ||
v.Pointer = &v.String | ||
pool.Put(v) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if !reflect.DeepEqual(&zero, pool.Get()) { | ||
t.Fatal("should be zero value") | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Pointer", func(t *testing.T) { | ||
zero := &item{} | ||
pool := don.NewRequestPool(zero) | ||
|
||
for i := 0; i < 100; i++ { | ||
p := pool.Get() | ||
v := *p | ||
v.String = "test" | ||
v.Pointer = &v.String | ||
pool.Put(p) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if !reflect.DeepEqual(&zero, pool.Get()) { | ||
t.Fatal("should be zero value") | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Resetter", func(t *testing.T) { | ||
zero := &itemResetter{} | ||
pool := don.NewRequestPool(zero) | ||
|
||
for i := 0; i < 100; i++ { | ||
p := pool.Get() | ||
v := *p | ||
v.String = "test" | ||
v.Pointer = &v.String | ||
pool.Put(p) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if !reflect.DeepEqual(&zero, pool.Get()) { | ||
t.Fatal("should be zero value") | ||
} | ||
} | ||
}) | ||
} | ||
|
||
type itemResetter struct { | ||
String string | ||
Pointer *string | ||
} | ||
|
||
func (ir *itemResetter) Reset() { | ||
*ir = itemResetter{} | ||
} |