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.61%. Comparing base (d3604a5) to head (cf37985).
⚠️ Report is 1 commits behind head on master.

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.62%   66.61%   -0.01%     
+ Complexity     1423     1417       -6     
============================================
  Files          3443     3443              
  Lines        218577   218621      +44     
  Branches      34792    34801       +9     
============================================
+ Hits         145624   145637      +13     
- Misses        61218    61248      +30     
- Partials      11735    11736       +1     
Flag Coverage Δ
custom-integration1 ?
integration ?
integration1 ?
integration2 ?
java-25 66.61% <77.55%> (-0.01%) ⬇️
temurin 66.61% <77.55%> (-0.01%) ⬇️
unittests 66.61% <77.55%> (-0.01%) ⬇️
unittests1 57.13% <61.22%> (+<0.01%) ⬆️
unittests2 38.95% <65.30%> (+0.01%) ⬆️

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 and others added 2 commits August 7, 2026 02:04
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 9a75197 to cf37985 Compare August 7, 2026 09:28
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