Skip to content

Commit

Permalink
internal: Add and use a generic pool instead of using sync.Pool directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mway committed Mar 20, 2023
1 parent 05c4b6c commit 889c171
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 53 deletions.
20 changes: 12 additions & 8 deletions buffer/pool.go
Expand Up @@ -20,25 +20,29 @@

package buffer

import "sync"
import (
"go.uber.org/zap/internal/pool"
)

// A Pool is a type-safe wrapper around a sync.Pool.
type Pool struct {
p *sync.Pool
p *pool.Pool[*Buffer]
}

// NewPool constructs a new Pool.
func NewPool() Pool {
return Pool{p: &sync.Pool{
New: func() interface{} {
return &Buffer{bs: make([]byte, 0, _size)}
},
}}
return Pool{
p: pool.New(func() *Buffer {
return &Buffer{
bs: make([]byte, 0, _size),
}
}),
}
}

// Get retrieves a Buffer from the pool, creating one if necessary.
func (p Pool) Get() *Buffer {
buf := p.p.Get().(*Buffer)
buf := p.p.Get()
buf.Reset()
buf.pool = p
return buf
Expand Down
9 changes: 4 additions & 5 deletions error.go
Expand Up @@ -21,14 +21,13 @@
package zap

import (
"sync"

"go.uber.org/zap/internal/pool"
"go.uber.org/zap/zapcore"
)

var _errArrayElemPool = sync.Pool{New: func() interface{} {
var _errArrayElemPool = pool.New(func() *errArrayElem {
return &errArrayElem{}
}}
})

// Error is shorthand for the common idiom NamedError("error", err).
func Error(err error) Field {
Expand Down Expand Up @@ -60,7 +59,7 @@ func (errs errArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
// potentially an "errorVerbose" attribute, we need to wrap it in a
// type that implements LogObjectMarshaler. To prevent this from
// allocating, pool the wrapper type.
elem := _errArrayElemPool.Get().(*errArrayElem)
elem := _errArrayElemPool.Get()
elem.error = errs[i]
arr.AppendObject(elem)
elem.error = nil
Expand Down
63 changes: 63 additions & 0 deletions internal/pool/pool.go
@@ -0,0 +1,63 @@
// 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

Check failure on line 21 in internal/pool/pool.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

should have a package comment

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()

Check warning on line 57 in internal/pool/pool.go

View check run for this annotation

Codecov / codecov/patch

internal/pool/pool.go#L57

Added line #L57 was not covered by tests
}

// Put puts x into the pool.
func (p *Pool[T]) Put(x T) {
p.pool.Put(x)
}
56 changes: 56 additions & 0 deletions internal/pool/pool_norace_test.go
@@ -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())
}
69 changes: 69 additions & 0 deletions internal/pool/pool_race_test.go
@@ -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)
}
}()
}
}
16 changes: 7 additions & 9 deletions stacktrace.go
Expand Up @@ -22,19 +22,17 @@ package zap

import (
"runtime"
"sync"

"go.uber.org/zap/buffer"
"go.uber.org/zap/internal/bufferpool"
"go.uber.org/zap/internal/pool"
)

var _stacktracePool = sync.Pool{
New: func() interface{} {
return &stacktrace{
storage: make([]uintptr, 64),
}
},
}
var _stacktracePool = pool.New(func() *stacktrace {
return &stacktrace{
storage: make([]uintptr, 64),
}
})

type stacktrace struct {
pcs []uintptr // program counters; always a subslice of storage
Expand Down Expand Up @@ -68,7 +66,7 @@ const (
//
// The caller must call Free on the returned stacktrace after using it.
func captureStacktrace(skip int, depth stacktraceDepth) *stacktrace {
stack := _stacktracePool.Get().(*stacktrace)
stack := _stacktracePool.Get()

switch depth {
case stacktraceFirst:
Expand Down
14 changes: 7 additions & 7 deletions zapcore/console_encoder.go
Expand Up @@ -22,20 +22,20 @@ package zapcore

import (
"fmt"
"sync"

"go.uber.org/zap/buffer"
"go.uber.org/zap/internal/bufferpool"
"go.uber.org/zap/internal/pool"
)

var _sliceEncoderPool = sync.Pool{
New: func() interface{} {
return &sliceArrayEncoder{elems: make([]interface{}, 0, 2)}
},
}
var _sliceEncoderPool = pool.New(func() *sliceArrayEncoder {
return &sliceArrayEncoder{
elems: make([]interface{}, 0, 2),
}
})

func getSliceEncoder() *sliceArrayEncoder {
return _sliceEncoderPool.Get().(*sliceArrayEncoder)
return _sliceEncoderPool.Get()
}

func putSliceEncoder(e *sliceArrayEncoder) {
Expand Down
18 changes: 8 additions & 10 deletions zapcore/entry.go
Expand Up @@ -24,25 +24,23 @@ import (
"fmt"
"runtime"
"strings"
"sync"
"time"

"go.uber.org/multierr"
"go.uber.org/zap/internal/bufferpool"
"go.uber.org/zap/internal/exit"
"go.uber.org/zap/internal/pool"
)

var (
_cePool = sync.Pool{New: func() interface{} {
// Pre-allocate some space for cores.
return &CheckedEntry{
cores: make([]Core, 4),
}
}}
)
var _cePool = pool.New(func() *CheckedEntry {
// Pre-allocate some space for cores.
return &CheckedEntry{
cores: make([]Core, 4),
}
})

func getCheckedEntry() *CheckedEntry {
ce := _cePool.Get().(*CheckedEntry)
ce := _cePool.Get()
ce.reset()
return ce
}
Expand Down
9 changes: 5 additions & 4 deletions zapcore/error.go
Expand Up @@ -23,7 +23,8 @@ package zapcore
import (
"fmt"
"reflect"
"sync"

"go.uber.org/zap/internal/pool"
)

// Encodes the given error into fields of an object. A field with the given
Expand Down Expand Up @@ -103,17 +104,17 @@ func (errs errArray) MarshalLogArray(arr ArrayEncoder) error {
return nil
}

var _errArrayElemPool = sync.Pool{New: func() interface{} {
var _errArrayElemPool = pool.New(func() *errArrayElem {
return &errArrayElem{}
}}
})

// Encodes any error into a {"error": ...} re-using the same errors logic.
//
// May be passed in place of an array to build a single-element array.
type errArrayElem struct{ err error }

func newErrArrayElem(err error) *errArrayElem {
e := _errArrayElemPool.Get().(*errArrayElem)
e := _errArrayElemPool.Get()
e.err = err
return e
}
Expand Down

0 comments on commit 889c171

Please sign in to comment.