Skip to content

refactor(btree): eliminate hot-path overhead from allocs and deserialization#252

Merged
lxy-9602 merged 7 commits into
alibaba:mainfrom
lxy-9602:improve-btree-efficiency
Apr 28, 2026
Merged

refactor(btree): eliminate hot-path overhead from allocs and deserialization#252
lxy-9602 merged 7 commits into
alibaba:mainfrom
lxy-9602:improve-btree-efficiency

Conversation

@lxy-9602
Copy link
Copy Markdown
Collaborator

@lxy-9602 lxy-9602 commented Apr 25, 2026

Purpose

Linked issue: #38

Optimize BTree global index read performance by reducing memory allocations and eliminating unnecessary deserialization in the hot path.

Changes

  • MemorySegment: add non-owning WrapView mode with cached data_/size_, avoiding shared_ptr dereference on every access.
  • MemorySlice: ReadSlice now returns a zero-copy non-owning view.
  • VarLengthIntUtils: unify varint encode/decode into static inline functions with 1-byte fast path for values 0–127.
  • MemorySliceInput: inline all hot-path methods into header; delete memory_slice_input.cpp.
  • BTreeGlobalIndexReader: compare keys at byte level via SliceComparator instead of deserializing to Literal per entry.
  • KeySerializer: add fast-path comparator for integer and string.
  • BlockIterator: return BlockEntry by value instead of unique_ptr.

Tests

MemorySegmentTest.TestNonOwningWrapView

API and Format

Documentation

Generative AI tooling

Partially Generated-by: Claude-4.6-Opus

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::WrapView and propagate view-based APIs (MemorySlice::Data, MemorySliceInput::ReadSlice zero-copy).
  • Refactor varint encoding/decoding into VarLengthIntUtils functions operating on raw buffers and inline hot-path MemorySliceInput.
  • Speed up BTree range queries by comparing serialized keys via a MemorySlice::SliceComparator instead of deserializing to Literal per entry; adjust SST block iteration to return BlockEntry by 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.

Comment thread src/paimon/common/memory/memory_slice_input.h Outdated
Comment thread src/paimon/common/global_index/btree/key_serializer.cpp
Comment thread src/paimon/common/memory/memory_slice_input.h Outdated
Comment thread src/paimon/common/utils/var_length_int_utils.h
@lxy-9602 lxy-9602 force-pushed the improve-btree-efficiency branch from b9af71f to 109ef9a Compare April 27, 2026 10:42
@lxy-9602 lxy-9602 merged commit 7bae4c5 into alibaba:main Apr 28, 2026
9 checks passed
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.

3 participants