Skip to content

perf(iobufpool): optimize pool index calculations with O(1) bit manipulation#2432

Merged
jackc merged 1 commit into
jackc:masterfrom
analytically:iobufpool_putoptim
Nov 28, 2025
Merged

perf(iobufpool): optimize pool index calculations with O(1) bit manipulation#2432
jackc merged 1 commit into
jackc:masterfrom
analytically:iobufpool_putoptim

Conversation

@analytically

@analytically analytically commented Nov 15, 2025

Copy link
Copy Markdown
Contributor

Replace O(n) loops in getPoolIdx and putPoolIdx with O(1) direct calculation using bits.Len and bits.TrailingZeros. Pool sizes are powers of 2, so we can derive indices directly from buffer size/capacity exponents.

getPoolIdx: Use bits.Len to calculate ceil(log2(size)) in constant time
putPoolIdx: Use bits.TrailingZeros to calculate log2(cap) in constant time

Benchmarks (Apple M1 Pro):

  • getPoolIdx: 16.97ns → 3.52ns (4.8x faster)
  • putPoolIdx: 24.75ns → 4.16ns (5.9x faster)
  • Get/Put cycle: 89.15ns → 76.57ns (14% faster)
  • Get(32K): 15.38ns → 10.52ns (32% faster)

…ulation

Replace O(n) loops in getPoolIdx and putPoolIdx with O(1) direct calculation
using bits.Len and bits.TrailingZeros. Pool sizes are powers of 2, so we can
derive indices directly from buffer size/capacity exponents.

getPoolIdx:  Use bits.Len to calculate ceil(log2(size)) in constant time
putPoolIdx:  Use bits.TrailingZeros to calculate log2(cap) in constant time

Benchmarks (Apple M1 Pro):
  getPoolIdx:      16.97ns → 3.52ns   (4.8x faster)
  putPoolIdx:      24.75ns → 4.16ns   (5.9x faster)
  Get/Put cycle:   89.15ns → 76.57ns  (14% faster)
  Get(32K):        15.38ns → 10.52ns  (32% faster)

Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
@analytically

Copy link
Copy Markdown
Contributor Author

Failing CI doesn't seem related?

@jackc jackc merged commit 0859fbd into jackc:master Nov 28, 2025
16 of 28 checks passed
@analytically analytically deleted the iobufpool_putoptim branch November 29, 2025 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants