package zapjournal

import (
	
)

// pooledBufferCapacity is the capacity required for the encoder to be pooled.
// This is necessary since the operation of sync.Pool assumes that the memory
// cost of each element is approximately the same in order to be efficient.
// See https://go.dev/issue/23199
const pooledBufferCapacity = 8192

// varsEncoderPool is a pool of varsEncoder objects with fixed buffer capacity.
var varsEncoderPool = sync.Pool{
	New: func() any {
		 := &varsEncoder{
			buf: make([]byte, 0, pooledBufferCapacity),
		}
		.json.bufp = &.buf
		return 
	},
}

// getVarsEncoder returns a new varsEncoder instance from the pool with the
// given variable prefix set.
func ( string) *varsEncoder {
	 := varsEncoderPool.Get().(*varsEncoder)
	.prefix = 
	return 
}

// putVarsEncoder puts the varsEncoder instance back to the pool if possible.
func ( *varsEncoder) {
	if cap(.buf) != pooledBufferCapacity {
		return
	}
	.prefix = ""
	.buf = .buf[:0]
	varsEncoderPool.Put()
}

// cloneVarsEncoder clones the given varsEncoder, using the pooled objects when
// possible. It must not be called on varsEncoders between beginVar and endVar
// calls.
func ( *varsEncoder) *varsEncoder {
	if cap(.buf) != pooledBufferCapacity {
		 := make([]byte, len(.buf), cap(.buf))
		_ = copy(, .buf)
		// Note that we do not have to copy hdr and json fields since
		// they are reset to zero values after use in each method.
		return &varsEncoder{
			prefix: .prefix,
			buf:    ,
		}
	}

	 := getVarsEncoder(.prefix)
	.buf = .buf[:len(.buf)]
	_ = copy(.buf, .buf)
	return 
}