Evaluate pooling and reflection costs
Evaluate pooling and reflection costs
sync.Pool can reuse temporary objects among independent callers and reduce allocation traffic, but the runtime may discard pooled objects at any time. It is not a cache of required state. Reflection enables runtime type inspection at the cost of dynamic checks and possible allocation. Compare these costs with simple typed code while preserving the same behavior.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "bytes" "sync" "testing")
var pool = sync.Pool{New: func() any { return new(bytes.Buffer) }}var result string
func render(reuse bool) string { var x *bytes.Buffer if reuse { x = pool.Get().(*bytes.Buffer) } else { x = new(bytes.Buffer) } x.Reset() x.WriteString("go") s := x.String() if reuse && x.Cap() <= 4096 { pool.Put(x) } return s}func TestLesson(t *testing.T) { for i := 0; i < 20; i++ { if render(true) != "go" || render(false) != "go" { t.Fatal("output") } }}func BenchmarkFresh(b *testing.B) { for i := 0; i < b.N; i++ { result = render(false) }}func BenchmarkPool(b *testing.B) { for i := 0; i < b.N; i++ { result = render(true) }}Only the current borrower owns the buffer. String returns a string representing the current contents; do not return a Bytes slice that aliases a buffer already returned to the pool. Cap bounds the size accepted back into this pool, not its total memory. Reset does not securely erase underlying bytes: avoid pooling sensitive data without a reviewed lifecycle.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCompare direct and reflected field reads
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "reflect" "testing")
type Item struct{ Count int64 }
var value int64
func dynamic(x Item) int64 { return reflect.ValueOf(x).FieldByName("Count").Int() }func TestLesson(t *testing.T) { x := Item{5} if dynamic(x) != x.Count { t.Fatal("value") }}func BenchmarkDirect(b *testing.B) { x := Item{5} for i := 0; i < b.N; i++ { value = x.Count }}func BenchmarkReflect(b *testing.B) { x := Item{5} for i := 0; i < b.N; i++ { value = dynamic(x) }}This fixture has a known exported integer field. For arbitrary input, check IsValid and Kind before calling Int; a missing field or wrong type can panic. Reflection metadata lookup by name can be expensive in hot loops; consider caching validated metadata when type sets are bounded. The constant typed read may be optimized heavily, so compare representative nonconstant inputs before making an architectural decision.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Write a reflection helper returning (int64,bool) for a direct exported int64 field on a struct; test missing, wrong-kind, promoted and valid fields. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance engineering 06: Evaluate pooling and reflection costs
Check the lesson objective before moving on.
Can sync.Pool discard unused objects?
What must you avoid after returning a buffer to a pool?