-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: Add and use a generic pool instead of using sync.Pool directly
- Loading branch information
Showing
12 changed files
with
317 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
// Package pool provides internal pool utilities. | ||
package pool | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// A Constructor is a function that creates a T when a [Pool] is empty. | ||
type Constructor[T any] func() T | ||
|
||
// A Pool is a generic wrapper around [sync.Pool] to provide strongly-typed | ||
// object pooling. | ||
type Pool[T any] struct { | ||
pool sync.Pool | ||
ctor Constructor[T] | ||
} | ||
|
||
// New returns a new [Pool] for T, and will use ctor to construct new Ts when | ||
// the pool is empty. | ||
func New[T any](ctor Constructor[T]) *Pool[T] { | ||
return &Pool[T]{ | ||
pool: sync.Pool{ | ||
New: func() any { | ||
return ctor() | ||
}, | ||
}, | ||
ctor: ctor, | ||
} | ||
} | ||
|
||
// Get gets a new T from the pool, or creates a new one if the pool is empty. | ||
func (p *Pool[T]) Get() T { | ||
if x, ok := p.pool.Get().(T); ok { | ||
return x | ||
} | ||
|
||
// n.b. This branch is effectively unreachable. | ||
return p.ctor() | ||
} | ||
|
||
// Put puts x into the pool. | ||
func (p *Pool[T]) Put(x T) { | ||
p.pool.Put(x) | ||
} |
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,81 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package pool | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPool_ConstructorCalledIfWrongType(t *testing.T) { | ||
cases := []struct { | ||
input any | ||
expectCall bool | ||
}{ | ||
{ | ||
input: int64(123), | ||
expectCall: false, | ||
}, | ||
{ | ||
input: uint64(123), | ||
expectCall: true, | ||
}, | ||
{ | ||
input: int(123), | ||
expectCall: true, | ||
}, | ||
{ | ||
input: uint(123), | ||
expectCall: true, | ||
}, | ||
{ | ||
input: struct{}{}, | ||
expectCall: true, | ||
}, | ||
{ | ||
input: nil, | ||
expectCall: true, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(fmt.Sprintf("%T", tt.input), func(t *testing.T) { | ||
var ( | ||
called bool | ||
pool = New(func() int64 { | ||
called = true | ||
return 0 | ||
}) | ||
) | ||
|
||
// Override the internal pool to provide unexpected types. | ||
pool.pool.New = func() any { | ||
return tt.input | ||
} | ||
|
||
pool.pool.Put(tt.input) | ||
pool.Get() | ||
require.Equal(t, tt.expectCall, called) | ||
}) | ||
} | ||
} |
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,56 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
//go:build !race | ||
|
||
package pool_test | ||
|
||
import ( | ||
"bytes" | ||
"runtime/debug" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/internal/pool" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
// n.b. Disable GC to avoid the victim cache during the test. | ||
defer debug.SetGCPercent(debug.SetGCPercent(-1)) | ||
|
||
p := pool.New(func() *bytes.Buffer { | ||
return bytes.NewBuffer([]byte("new")) | ||
}) | ||
p.Put(bytes.NewBuffer([]byte(t.Name()))) | ||
|
||
// Ensure that we always get the expected value. | ||
for i := 0; i < 1_000; i++ { | ||
func() { | ||
buf := p.Get() | ||
defer p.Put(buf) | ||
require.Equal(t, t.Name(), buf.String()) | ||
}() | ||
} | ||
|
||
// Depool an extra object to ensure that the constructor is called and | ||
// produces an expected value. | ||
require.Equal(t, t.Name(), p.Get().String()) | ||
require.Equal(t, "new", p.Get().String()) | ||
} |
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,69 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
//go:build race | ||
|
||
package pool_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"go.uber.org/zap/internal/pool" | ||
) | ||
|
||
type pooledValue struct { | ||
n int64 | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
// n.b. [sync.Pool] will randomly drop re-pooled objects when race is | ||
// enabled, so rather than testing nondeterminsitic behavior, we use | ||
// this test solely to prove that there are no races. See pool_test.go | ||
// for correctness testing. | ||
|
||
var ( | ||
p = pool.New(func() *pooledValue { | ||
return &pooledValue{ | ||
n: -1, | ||
} | ||
}) | ||
wg sync.WaitGroup | ||
) | ||
|
||
defer wg.Wait() | ||
|
||
for i := int64(0); i < 1_000; i++ { | ||
i := i | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
x := p.Get() | ||
defer p.Put(x) | ||
|
||
// n.b. Must read and write the field. | ||
if n := x.n; n >= -1 { | ||
x.n = int64(i) | ||
} | ||
}() | ||
} | ||
} |
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
Oops, something went wrong.