perf: pool hash tables in Go encode paths to reduce allocations#1143
perf: pool hash tables in Go encode paths to reduce allocations#1143klauspost merged 1 commit intoklauspost:masterfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReplaces per-call stack-allocated hash-table arrays in S2 encoders with pooled, preallocated table structs; introduces pool-backed table types and shared sizing constants, and updates encoder functions to borrow and return tables via pool accessors. Changes
Sequence Diagram(s)sequenceDiagram
participant Encoder as Encoder (encodeBlock*)
participant Pool as TablePool (sync.Pool)
participant Tables as Tables (better/best struct)
Encoder->>Pool: get*Tables()
Pool-->>Tables: return pooled *Tables
Encoder->>Tables: use lTable / sTable for hashing
Encoder--)Tables: finish using tables
Encoder->>Pool: Pool.Put(tbl)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
s2/hashtable_pool.go (1)
23-42: Consider wrapping pool checkout/reset in helpers.
Get+ full reset +defer Putis now repeated ins2/encode_better.goat Lines 70-75, 332-337, and 932-937, and ins2/encode_best.goat Lines 47-52 and 481-486. A small helper per pool that resets the whole struct before handing it out would keep future table-layout changes from drifting out of sync.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@s2/hashtable_pool.go` around lines 23 - 42, Create helper getters for each pool (e.g., getBetterTables, getBetterSnappyTables, getBestTables) that encapsulate pool.Get(), cast to the correct pointer type (betterTables, betterSnappyTables, bestTables), reset the entire struct to zero (e.g., assign a zero-value struct to *ptr) and return the pointer; replace the repeated pattern of betterTablePool.Get()+manual reset+defer Put with calls to these helpers so callers receive an already-zeroed table object and still call Put when done. Use the pool variables betterTablePool, betterSnappyTablePool, bestTablePool and the struct types betterTables, betterSnappyTables, bestTables to locate where to add the helpers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@s2/hashtable_pool.go`:
- Around line 23-42: Create helper getters for each pool (e.g., getBetterTables,
getBetterSnappyTables, getBestTables) that encapsulate pool.Get(), cast to the
correct pointer type (betterTables, betterSnappyTables, bestTables), reset the
entire struct to zero (e.g., assign a zero-value struct to *ptr) and return the
pointer; replace the repeated pattern of betterTablePool.Get()+manual
reset+defer Put with calls to these helpers so callers receive an already-zeroed
table object and still call Put when done. Use the pool variables
betterTablePool, betterSnappyTablePool, bestTablePool and the struct types
betterTables, betterSnappyTables, bestTables to locate where to add the helpers.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: d6a14610-8ea5-4caa-99f4-8066f002aca8
📒 Files selected for processing (3)
s2/encode_best.gos2/encode_better.gos2/hashtable_pool.go
Summary
Pool S2 Go-path hash tables (320KB–4.6MB/call) via
sync.Pool, matching the existing amd64 asm pattern (encPoolsinencode_amd64.go). Eliminates per-block stack growth and GC pressure on arm64.Changes
encodeBlockBetterGo,encodeBlockBetterSnappyGo,encodeBlockBetterDictuse pooled tables.encodeBlockBestGo,encodeBlockBestSnappyuse pooled tables.Benchmark
Checklist
Summary by CodeRabbit