refactor(btree): eliminate hot-path overhead from allocs and deserialization#252
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Optimizes hot-path performance for BTree global index reads by reducing allocations, introducing non-owning memory views, and avoiding per-entry key deserialization during range scans.
Changes:
- Introduce non-owning
MemorySegment::WrapViewand propagate view-based APIs (MemorySlice::Data,MemorySliceInput::ReadSlicezero-copy). - Refactor varint encoding/decoding into
VarLengthIntUtilsfunctions operating on raw buffers and inline hot-pathMemorySliceInput. - Speed up BTree range queries by comparing serialized keys via a
MemorySlice::SliceComparatorinstead of deserializing toLiteralper entry; adjust SST block iteration to returnBlockEntryby value.
Reviewed changes
Copilot reviewed 29 out of 29 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/paimon/core/mergetree/lookup/persist_position_processor.h | Switch varint encode/decode calls to raw-buffer APIs. |
| src/paimon/common/utils/var_length_int_utils_test.cpp | Update varint tests to use stack buffers and new APIs. |
| src/paimon/common/utils/var_length_int_utils.h | Rework varint utilities to encode/decode from char* with a 1-byte decode fast path. |
| src/paimon/common/sst/sst_file_reader.cpp | Adapt to BlockIterator::Next() returning BlockEntry by value; use MemorySegment::Data/Size/MutableData. |
| src/paimon/common/sst/block_iterator.h | Change iterator API to return BlockEntry by value. |
| src/paimon/common/sst/block_iterator.cpp | Implement value-returning Next/ReadEntry. |
| src/paimon/common/sst/block_entry.h | Remove now-unneeded <memory> include; keep BlockEntry as slices. |
| src/paimon/common/sst/block_cache_test.cpp | Update test to use new segment heap accessor naming. |
| src/paimon/common/sst/block_cache.h | Read blocks via MemorySegment::MutableData(). |
| src/paimon/common/memory/memory_slice_test.cpp | Rename test to match new heap accessor API. |
| src/paimon/common/memory/memory_slice_output.cpp | Encode varints via VarLengthIntUtils into the output buffer. |
| src/paimon/common/memory/memory_slice_input.h | Inline hot-path methods; make ReadSlice return a non-owning view. |
| src/paimon/common/memory/memory_slice_input.cpp | Remove out-of-line implementation (file deleted). |
| src/paimon/common/memory/memory_slice.h | Add Data() accessor; rename heap accessor to GetOrCreateHeapMemory(). |
| src/paimon/common/memory/memory_slice.cpp | Implement GetOrCreateHeapMemory(), ReadStringView() via Data(), and faster CopyBytes(). |
| src/paimon/common/memory/memory_segment_utils.cpp | Update to GetOrCreateHeapMemory() naming/behavior. |
| src/paimon/common/memory/memory_segment_test.cpp | Add coverage for non-owning WrapView behavior. |
| src/paimon/common/memory/memory_segment.h | Add WrapView, cached data_/size_, and Data()/MutableData() accessors; rename heap accessor. |
| src/paimon/common/memory/memory_segment.cpp | Remove SwapBytes implementation. |
| src/paimon/common/io/cache/lru_cache_test.cpp | Use MemorySegment::MutableData() for fills. |
| src/paimon/common/global_index/btree/key_serializer.cpp | Add fast-path MemorySlice comparators to avoid Literal deserialization. |
| src/paimon/common/global_index/btree/btree_index_meta.cpp | Use GetOrCreateHeapMemory() for serialized output. |
| src/paimon/common/global_index/btree/btree_global_index_reader.h | Store a pre-built slice comparator for range queries. |
| src/paimon/common/global_index/btree/btree_global_index_reader.cpp | Compare keys via SliceComparator and serialize bounds once per query. |
| src/paimon/common/data/serializer/row_compacted_serializer.h | Decode varints from MemorySegment::Data(). |
| src/paimon/common/data/serializer/row_compacted_serializer.cpp | Encode varints into raw buffers; read strings via segment_.Data(). |
| src/paimon/common/data/binary_string.cpp | Read string views via MemorySegment::Data(). |
| src/paimon/common/data/abstract_binary_writer.cpp | Grow by copying from GetOrCreateHeapMemory() instead of direct array access. |
| src/paimon/CMakeLists.txt | Remove memory_slice_input.cpp from build. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lxy-9602
commented
Apr 27, 2026
b9af71f to
109ef9a
Compare
lszskye
approved these changes
Apr 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Linked issue: #38
Optimize BTree global index read performance by reducing memory allocations and eliminating unnecessary deserialization in the hot path.
Changes
WrapViewmode with cacheddata_/size_, avoidingshared_ptrdereference on every access.ReadSlicenow returns a zero-copy non-owning view.static inlinefunctions with 1-byte fast path for values 0–127.memory_slice_input.cpp.SliceComparatorinstead of deserializing toLiteralper entry.BlockEntryby value instead ofunique_ptr.Tests
MemorySegmentTest.TestNonOwningWrapView
API and Format
Documentation
Generative AI tooling
Partially Generated-by: Claude-4.6-Opus