← Back to Pull requests

perf(dotc1z): pool zstd encoders and decoders to reduce allocations #773

Closed
unknown closed this pull request without merging on Apr 18, 2026 16:16 UTC
View on GitHub
unknown unknown commented on Apr 18, 2026 00:21 UTC
Author

Reviving #622 (closed, not merged). This re-opens the same branch with btipling's two non-blocking follow-ups from the prior review folded in.

Summary

  • Add sync.Pool for zstd.Encoder and zstd.Decoder to eliminate per-call allocation overhead on the dotc1z hot path.

  • Profiling showed zstd.ensureHist allocating ~215 MB/min in temporal_sync from encoder churn, and the decoder side allocating ~3.2 GB/min in temporal_worker.

  • Pool only engages when options match pool defaults; callers with custom concurrency/memory settings fall back to fresh instances.
  • Changes

    | File | Change |
    |---|---|
    | pkg/dotc1z/pool.go | NEW — encoder/decoder pool with get/put helpers |
    | pkg/dotc1z/pool_test.go | NEW — unit tests, concurrent stress, round-trip, benchmarks |
    | pkg/dotc1z/file.go | saveC1z uses pooled encoder when concurrency matches |
    | pkg/dotc1z/decoder.go | decoder uses pooled decoder when options match |

    Follow-ups from prior review (new in this revival)

    1. Pool now engages at the production default. In the prior PR, pooledEncoderConcurrency = GOMAXPROCS but the callers default to encoderConcurrency = 1, so the gate at file.go:158 was always false and the pool carried zero traffic in prod. Pinning pooledEncoderConcurrency = 1 makes the pool match the actual sync-path default. The alternative — flipping callers to 0 (→GOMAXPROCS) — was deliberately avoided because klauspost/compress's multi-threaded encoder has theoretical race concerns.

    2. Pool-growth tests are now hermetic. TestPoolGrowsFromSaveC1z, TestPoolGrowsFromDecoder, TestEncoderPool, and TestDecoderPool now swap the package-global sync.Pool via t.Cleanup, so GC eviction and cross-test ordering can't flake the exact-membership assertions. The package globals became pointer-valued to allow the swap.

    Safety Measures

  • Pool only used when options match pool defaults (concurrency, maxMemory).

  • Encoders always closed on error paths (not returned to pool in bad state).

  • Reset(nil) called before returning to pool to release writer/reader references.

  • Decoder Reset() errors handled gracefully (don't return bad decoders to pool).
  • Benchmark (apples-to-apples at concurrency=1)

    BenchmarkEncoderAllocationOnly/pooled-16          1,202,396     1000 ns/op     116 B/op     2 allocs/op
    BenchmarkEncoderAllocationOnly/new_each_time-16       4,809   338275 ns/op 1758312 B/op    19 allocs/op
    

    ~15,000x reduction in bytes allocated per encode.

    Production Profile Context

  • temporal_sync (60s profile): zstd.(*fastBase).ensureHist ≈ 384 MB, total zstd allocs ≈ 665 MB (12.4% of total).

  • temporal_worker (62s profile): decoder-side allocs ≈ 3.26 GB (17.9% of total) via protozstd.Unmarshal → decompressValue.
  • Test plan

  • All existing dotc1z tests pass

  • Pool unit tests (concurrent access, round-trip correctness)

  • Regression tests verify pool grows from empty state

  • Isolated-pool tests are no longer sensitive to cross-test ordering

  • Benchmark demonstrates allocation reduction at production concurrency

  • Deploy to staging and profile to verify improvement
  • Prior approval: @btipling on #622.

    🤖 Generated with Claude Code

    Diff: 410 lines (+307 / -103) across 5 files Reviewer: arreyder Review duration: 955m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.45) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 410 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.360
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #773"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.4% · Safe to merge
    19.4%