-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improve][broker] Optimize TripleLongPriorityQueue heap operations #26010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodece
merged 3 commits into
apache:master
from
nodece:optimize-TripleLongPriorityQueue-heap-operations
Jun 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
...main/java/org/apache/pulsar/common/util/collections/TripleLongPriorityQueueBenchmark.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.pulsar.common.util.collections; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * JMH benchmarks for {@link TripleLongPriorityQueue} simulating Pulsar delayed delivery workloads. | ||
| * | ||
| * <p>Three scenarios matching real usage: | ||
| * <ul> | ||
| * <li>{@link #recoveryBulkAddThenPop} — snapshot recovery: bulk add all entries, then pop all. | ||
| * Cold cache, large heap. This is the <b>worst case</b> for the hole-based optimization | ||
| * because cache misses dominate over reduced readLong calls.</li> | ||
| * <li>{@link #interleavedAddPop} — steady-state delayed delivery: batch add (messages arriving | ||
| * between timer ticks), then batch pop (getScheduledMessages). Heap stays warm in cache. | ||
| * This is the <b>primary hot path</b>.</li> | ||
| * <li>{@link #steadyState} — constant-depth steady state: pre-fill queue, then alternating | ||
| * small add/pop batches. Simulates sustained throughput with ~10K queue depth.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Build and run: | ||
| * <pre> | ||
| * ./gradlew :microbench:shadowJar | ||
| * java -jar microbench/build/libs/microbench-*-benchmarks.jar ".*TripleLongPriorityQueue.*" | ||
| * </pre> | ||
| */ | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @Fork(2) | ||
| @Warmup(iterations = 3, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| @State(Scope.Thread) | ||
| public class TripleLongPriorityQueueBenchmark { | ||
|
|
||
| @Param({"50000", "500000", "2000000"}) | ||
| int size; | ||
|
|
||
| /** | ||
| * Recovery scenario: bulk add all then pop all. | ||
| * Simulates snapshot recovery from BookKeeper — cold cache, large heap. | ||
| */ | ||
| @Benchmark | ||
| public void recoveryBulkAddThenPop(Blackhole bh) { | ||
| try (TripleLongPriorityQueue pq = new TripleLongPriorityQueue()) { | ||
| long baseTs = System.currentTimeMillis(); | ||
| Random rng = new Random(42); | ||
| for (int i = 0; i < size; i++) { | ||
| long n1 = baseTs + rng.nextLong(3_600_000); | ||
| long n2 = i / 1000; | ||
| long n3 = i; | ||
| pq.add(n1, n2, n3); | ||
| } | ||
| while (!pq.isEmpty()) { | ||
| bh.consume(pq.peekN1()); | ||
| pq.pop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Interleaved scenario: batch add then batch pop, repeated. | ||
| * Simulates steady-state delayed delivery — messages arrive continuously, | ||
| * getScheduledMessages pops in batches of ~500 when consumers are ready. | ||
| * Heap is warm in L2/L3 cache between operations. | ||
| */ | ||
| @Benchmark | ||
| public void interleavedAddPop(Blackhole bh) { | ||
| try (TripleLongPriorityQueue pq = new TripleLongPriorityQueue()) { | ||
| Random rng = new Random(42); | ||
| long baseTs = System.currentTimeMillis(); | ||
| int batchSize = 500; | ||
| int totalAdded = 0; | ||
|
|
||
| while (totalAdded < size) { | ||
| // Batch add: messages arriving between timer ticks | ||
| int addCount = Math.min(batchSize + rng.nextInt(500), size - totalAdded); | ||
| for (int i = 0; i < addCount; i++) { | ||
| long n1 = baseTs + rng.nextLong(3_600_000); | ||
| long n2 = (totalAdded + i) / 1000; | ||
| long n3 = totalAdded + i; | ||
| pq.add(n1, n2, n3); | ||
| } | ||
| totalAdded += addCount; | ||
|
|
||
| // Batch pop: getScheduledMessages delivering to consumers | ||
| int popCount = (int) Math.min(batchSize, pq.size()); | ||
| for (int i = 0; i < popCount; i++) { | ||
| bh.consume(pq.peekN1()); | ||
| pq.pop(); | ||
| } | ||
| } | ||
| // Drain remaining | ||
| while (!pq.isEmpty()) { | ||
| bh.consume(pq.peekN1()); | ||
| pq.pop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Steady-state scenario: pre-fill queue, then alternating small add/pop. | ||
| * Simulates sustained throughput with constant ~10K queue depth. | ||
| * Heap stays hot in cache — this is where the readLong reduction matters most. | ||
| */ | ||
| @Benchmark | ||
| public void steadyState(Blackhole bh) { | ||
| int steadyDepth = 10_000; | ||
| try (TripleLongPriorityQueue pq = new TripleLongPriorityQueue()) { | ||
| Random rng = new Random(42); | ||
| long baseTs = System.currentTimeMillis(); | ||
| long seq = 0; | ||
|
|
||
| // Pre-fill | ||
| for (int i = 0; i < steadyDepth; i++) { | ||
| pq.add(baseTs + rng.nextLong(3_600_000), seq / 1000, seq); | ||
| seq++; | ||
| } | ||
|
|
||
| // Alternating add/pop to maintain steady depth | ||
| int ops = 0; | ||
| while (ops < size) { | ||
| // Small batch add | ||
| int addCount = 100 + rng.nextInt(100); | ||
| for (int i = 0; i < addCount && ops < size; i++) { | ||
| pq.add(baseTs + rng.nextLong(3_600_000), seq / 1000, seq); | ||
| seq++; | ||
| ops++; | ||
| } | ||
| // Small batch pop | ||
| int popCount = addCount - rng.nextInt(20); | ||
| for (int i = 0; i < popCount && !pq.isEmpty(); i++) { | ||
| bh.consume(pq.peekN1()); | ||
| pq.pop(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.