Skip to content

Optimize consuming MAP key access - #19168

Open
xiangfu0 wants to merge 2 commits into
apache:masterfrom
xiangfu0:agent/apache-consuming-map-key-extractor
Open

Optimize consuming MAP key access#19168
xiangfu0 wants to merge 2 commits into
apache:masterfrom
xiangfu0:agent/apache-consuming-map-key-extractor

Conversation

@xiangfu0

@xiangfu0 xiangfu0 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

Consuming MAP key lookups currently materialize and deserialize the full MAP value before extracting one requested key. This change adds a selective forward-index read path that scans the serialized MAP entries and deserializes only the matching value.

Changes

  • Add a default ForwardIndexReader#getMapValue API while preserving the existing full-MAP fallback.
  • Add a selective MAP-value deserializer that compares serialized UTF-8 key bytes without allocating non-matching values.
  • Add a read-only zero-copy view for mutable off-heap forward-index values.
  • Use the selective path from MapKeyIndexReader and add focused unit coverage.
  • Add BenchmarkMapKeyAccess for comparing full-map and selective lookup costs.

Performance

Isolated JMH run for a 64-entry MAP, looking up the last key, on JDK 25 (1 fork, 2 warmup iterations, 3 measurement iterations):

Path Score
Full map copy + deserialize 11.154 +/- 1.970 us/op
Selective direct-buffer access 0.609 +/- 0.077 us/op

The benchmark is an isolated forward-index lookup measurement; it does not represent an end-to-end broker/server query latency result.

Validation

  • ./mvnw -pl pinot-spi,pinot-segment-local -am -Dtest=MapUtilsTest,VarByteSVMutableForwardIndexTest -Dsurefire.failIfNoSpecifiedTests=false test
  • ./mvnw -pl pinot-perf -am -DskipTests package
  • ./mvnw spotless:apply -pl pinot-spi,pinot-segment-spi,pinot-segment-local,pinot-perf
  • ./mvnw license:format license:check -pl pinot-spi,pinot-segment-spi,pinot-segment-local,pinot-perf
  • git diff --check

@codecov-commenter

codecov-commenter commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.55102% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.95%. Comparing base (4fa3bac) to head (041ffa6).

Files with missing lines Patch % Lines
...l/io/writer/impl/MutableOffHeapByteArrayStore.java 54.54% 2 Missing and 3 partials ⚠️
...main/java/org/apache/pinot/spi/utils/MapUtils.java 85.71% 4 Missing and 1 partial ⚠️
...t/segment/spi/index/reader/ForwardIndexReader.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #19168      +/-   ##
============================================
+ Coverage     66.94%   66.95%   +0.01%     
  Complexity     1423     1423              
============================================
  Files          3452     3452              
  Lines        218534   218581      +47     
  Branches      34741    34751      +10     
============================================
+ Hits         146288   146356      +68     
+ Misses        60559    60533      -26     
- Partials      11687    11692       +5     
Flag Coverage Δ
integration 100.00% <ø> (ø)
integration1 100.00% <ø> (ø)
integration2 0.00% <ø> (ø)
java-25 66.95% <77.55%> (+0.01%) ⬆️
lane-a 100.00% <ø> (ø)
lane-b 0.00% <ø> (ø)
temurin 66.95% <77.55%> (+0.01%) ⬆️
unittests 66.95% <77.55%> (+0.01%) ⬆️
unittests1 57.68% <61.22%> (+<0.01%) ⬆️
unittests2 39.07% <65.30%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xiangfu0
xiangfu0 marked this pull request as ready for review August 6, 2026 04:53
@xiangfu0
xiangfu0 requested review from Jackie-Jiang and a lite review from Copilot August 6, 2026 07:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes MAP-key lookups in Pinot’s forward index path by enabling selective value extraction (scan serialized MAP entries and deserialize only the matching value) instead of deserializing the entire MAP for every key access.

Changes:

  • Added ForwardIndexReader#getMapValue(...) as a default SPI API, and switched MapKeyIndexReader to use it (with a full-map fallback via the default implementation).
  • Implemented MapUtils.deserializeMapValue(...) to scan length-prefixed MAP frames and deserialize only the selected value, including support for direct ByteBuffer inputs (e.g., off-heap views).
  • Introduced a read-only zero-copy ByteBuffer view in MutableOffHeapByteArrayStore, added focused unit tests, and added a JMH benchmark to compare approaches.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pinot-spi/src/test/java/org/apache/pinot/spi/utils/MapUtilsTest.java Adds unit coverage for selective MAP-value extraction (colliding keys, non-ASCII keys, byte order).
pinot-spi/src/main/java/org/apache/pinot/spi/utils/MapUtils.java Adds selective MAP-value deserialization from a length-prefixed MAP frame, including direct-buffer support.
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/reader/ForwardIndexReader.java Adds default getMapValue(...) API to allow optimized implementations while preserving fallback behavior.
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/map/MapKeyIndexReaderTest.java Verifies MapKeyIndexReader works both with selective and fallback implementations.
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/forward/mutable/VarByteSVMutableForwardIndexTest.java Adds coverage for the mutable forward index’s selective getMapValue(...) path.
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/map/MapKeyIndexReader.java Switches extraction to ForwardIndexReader#getMapValue(...) to enable selective reads.
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/forward/VarByteSVMutableForwardIndex.java Overrides getMapValue(...) to use selective ByteBuffer-based extraction.
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/writer/impl/MutableOffHeapByteArrayStore.java Adds a read-only, zero-copy ByteBuffer accessor for stored values.
pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkMapKeyAccess.java Adds JMH benchmark comparing full-map deserialize vs selective key lookup.

Comment on lines +233 to +241
int valueLength = byteBuffer.getInt();
if (!matches) {
skip(byteBuffer, valueLength);
continue;
}
// Keys within a frame are unique - the write path iterates a Map - so the first match is the only match and
// the remaining entries never need to be scanned.
byte[] valueBytes = new byte[valueLength];
byteBuffer.get(valueBytes);
assertReaderBehavior(new FullMapOnlyReader());
}

private static void assertReaderBehavior(ForwardIndexReader reader) {
Comment on lines +76 to +77
@SuppressWarnings("rawtypes")
private abstract static class BaseReader implements ForwardIndexReader {
Comment on lines +189 to +196
/// Deserializes only the value for the requested key from a length-prefixed MAP frame.
/// Non-matching keys and values are skipped without allocating byte arrays or invoking Jackson.
///
/// @param bytes Serialized MAP frame
/// @param key Key whose value should be deserialized
/// @return Deserialized value, or `null` if the key is missing, has a null value, or cannot be deserialized
@Nullable
public static Object deserializeMapValue(byte[] bytes, String key) {
@xiangfu0
xiangfu0 force-pushed the agent/apache-consuming-map-key-extractor branch 5 times, most recently from 15c0fff to acbae9a Compare August 11, 2026 09:02
xiangfu0 and others added 2 commits August 12, 2026 02:03
deserializeMapValue walked every key one relative get at a time - even
after a mismatch was already certain - purely to advance the position,
and kept scanning the frame after the match was found. Compare through
absolute gets so a length mismatch or a differing byte skips the rest of
the key outright, and return on the first match. Keys within a frame are
unique because the write path iterates a Map, so the first match is the
only match.

Bounds-check the key length up front so the absolute gets are provably in
range and a truncated frame still surfaces as BufferUnderflowException.

Isolated JMH, flat string values, fixed-length dotted keys, JDK 25:

  entries  key    full map   before   after
  4        first  0.556      0.166    0.114 us/op
  16       first  2.163      0.360    0.112 us/op
  64       first  8.886      1.074    0.118 us/op
  64       last   8.763      1.245    0.593 us/op

First-key lookup no longer scales with map size. Allocation is unchanged
at 856 B/op versus 62792 B/op for the full-map path.

Also cover MapKeyIndexReader, which had no test despite being the caller
that changed, over both a reader that overrides getMapValue and one that
inherits the default, plus non-ASCII keys and a little-endian buffer.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@xiangfu0
xiangfu0 force-pushed the agent/apache-consuming-map-key-extractor branch from acbae9a to 041ffa6 Compare August 12, 2026 09:03
@Jackie-Jiang
Jackie-Jiang requested a balanced review from Copilot August 13, 2026 00:04
@Jackie-Jiang Jackie-Jiang added enhancement Improvement to existing functionality performance Related to performance optimization index Related to indexing (general) labels Aug 13, 2026

/// Returns a view of the value at the given index without copying it.
/// The returned buffer must not be used after this store is closed.
public ByteBuffer getByteBuffer(int index) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) Move this before getValueSize()

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Suppressed comments (3)

pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/forward/VarByteSVMutableForwardIndex.java:106

  • Place @Nullable above @Override to follow Pinot's annotation ordering convention (kb/code-review-principles.md:1023-1025).
  @Override
  @Nullable

pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/map/MapKeyIndexReaderTest.java:67

  • Place @Nullable above @Override to follow Pinot's annotation ordering convention (kb/code-review-principles.md:1023-1025).
    @Override
    @Nullable

pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkMapKeyAccess.java:112

  • This reuses an already-created _directBuffer, but the production path creates a direct view and then a read-only view through _byteArrayStore.getByteBuffer(docId) on every lookup. The selective score therefore omits newly introduced per-row allocation and lookup overhead, so it is not the isolated forward-index lookup described in the PR. Benchmark VarByteSVMutableForwardIndex#getMapValue (or both MutableOffHeapByteArrayStore#get and getByteBuffer) directly so the comparison covers the actual old and new paths.
  public Object selectiveMapValue() {
    _directBuffer.position(0);
    return MapUtils.deserializeMapValue(_directBuffer, _targetKey);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality index Related to indexing (general) performance Related to performance optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants