Skip to content

fix: eliminate per-element heap allocs in is_in kernel for binary types#745

Open
zeroshade wants to merge 1 commit intoapache:mainfrom
zeroshade:fix/binary-memo-table-escape-736
Open

fix: eliminate per-element heap allocs in is_in kernel for binary types#745
zeroshade wants to merge 1 commit intoapache:mainfrom
zeroshade:fix/binary-memo-table-escape-736

Conversation

@zeroshade
Copy link
Copy Markdown
Member

Summary

  • Add BinaryMemoTable.ExistsDirect that inlines the hash table probe loop, avoiding the closure in HashTable.Lookup that causes val []byte to escape to the heap
  • Add isInBinaryDirect specialized kernel path that bypasses the visitBinaryVisitBitBlocksShort closure chain by directly iterating with OptionalBitBlockCounter
  • Route BinaryDataType dispatch in DispatchIsIn to the new direct path (handles both int32 and int64 offsets)

Motivation

The is_in kernel for binary types allocated once per input element because the []byte value escaped to the heap through a closure chain:

  1. visitBinary slices rawBytes[offsets[pos]:offsets[pos+1]] and passes to a callback
  2. The callback calls BinaryMemoTable.Exists(v)
  3. Exists calls lookup which creates a closure capturing val
  4. The closure is passed to HashTable.Lookup, causing escape analysis to move val to the heap

Closes #736

Benchmark (100k rows, 10-element value set)

Metric Before After Improvement
ns/op 4,133,679 923,565 4.5x faster
B/op 2,435,327 33,092 73x less memory
allocs/op 100,075 70 1,430x fewer allocs

All existing TestIsInBinary subtests pass (binary, large_binary, utf8, large_utf8 × all null matching behaviors).

Add BinaryMemoTable.ExistsDirect that inlines the hash table probe
loop, avoiding the closure in HashTable.Lookup that causes val to
escape. Add isInBinaryDirect that bypasses the visitBinary and
VisitBitBlocksShort closure chain by directly iterating with
OptionalBitBlockCounter.

Closes apache#736

Benchmark (100k rows, 10-element value set):
  Before: 4,133,679 ns/op  2,435,327 B/op  100,075 allocs/op
  After:    923,565 ns/op     33,092 B/op       70 allocs/op
@zeroshade zeroshade requested review from amoeba, kou and lidavidm April 3, 2026 18:00
@zeroshade
Copy link
Copy Markdown
Member Author

CC @laskoviymishka

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.

BinaryMemoTable.Exists causes heap allocation per call due to closure escape

1 participant