-
Notifications
You must be signed in to change notification settings - Fork 1.4k
perf(util): add FixedBitSet.copyOf() fast paths for SparseLiveDocs and DenseLiveDocs
#16282
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
Open
salvatorecampagna
wants to merge
5
commits into
apache:main
Choose a base branch
from
salvatorecampagna:perf/fixedbitset-copyof-livedocs-fast-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6067606
perf(util): add fast paths to FixedBitSet.copyOf() for LiveDocs types
salvatore-campagna 6402096
CHANGES: update issue number to GITHUB#16282
salvatore-campagna 4913df0
tidy: fix google-java-format violation in LiveDocsCopyOfBenchmark
salvatore-campagna fef04b7
simplify SparseLiveDocs.toFixedBitSet() to use FixedBitSet API
salvatore-campagna 7f1bf5e
Merge branch 'main' into perf/fixedbitset-copyof-livedocs-fast-paths
salvatorecampagna 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
There are no files selected for viewing
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
118 changes: 118 additions & 0 deletions
118
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/LiveDocsCopyOfBenchmark.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,118 @@ | ||
| /* | ||
| * 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.lucene.benchmark.jmh; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.apache.lucene.util.DenseLiveDocs; | ||
| import org.apache.lucene.util.FixedBitSet; | ||
| import org.apache.lucene.util.SparseFixedBitSet; | ||
| import org.apache.lucene.util.SparseLiveDocs; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| 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.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * Benchmarks {@link FixedBitSet#copyOf(org.apache.lucene.util.Bits)} for {@link SparseLiveDocs} and | ||
| * {@link DenseLiveDocs} inputs. | ||
| * | ||
| * <p>This benchmark measures the speedup from the fast paths added to {@code copyOf()} for the | ||
| * {@link SparseLiveDocs} and {@link DenseLiveDocs} types introduced by GITHUB#15413. Without these | ||
| * fast paths, both types fall through to the generic O(maxDoc) loop. With them: | ||
| * | ||
| * <ul> | ||
| * <li>{@link SparseLiveDocs}: O(deletedDocs) by iterating only the set bits of the deleted-docs | ||
| * bitset, then clearing those positions in the result | ||
| * <li>{@link DenseLiveDocs}: O(maxDoc/64) by cloning the backing {@link FixedBitSet} directly | ||
| * </ul> | ||
| * | ||
| * <h2>Usage</h2> | ||
| * | ||
| * <p>Run all benchmarks: | ||
| * | ||
| * <pre> | ||
| * java -jar lucene-benchmark-jmh.jar "LiveDocsCopyOfBenchmark" | ||
| * </pre> | ||
| * | ||
| * @see SparseLiveDocs | ||
| * @see DenseLiveDocs | ||
| * @see LiveDocsBenchmark | ||
| */ | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 3, time = 2) | ||
| @Measurement(iterations = 5, time = 3) | ||
| @Fork( | ||
| value = 1, | ||
| jvmArgsAppend = {"-Xmx2g", "-Xms2g"}) | ||
| public class LiveDocsCopyOfBenchmark { | ||
|
|
||
| /** Number of documents in the segment. */ | ||
| @Param({"1000000", "10000000", "100000000"}) | ||
| int maxDoc; | ||
|
|
||
| /** | ||
| * Percentage of documents to delete. | ||
| * | ||
| * <p>Kept low to stay in the SparseLiveDocs regime ({@literal <=}1%). At these rates the | ||
| * O(deletedDocs) vs O(maxDoc) difference is most pronounced. | ||
| */ | ||
| @Param({"0.001", "0.01"}) | ||
| double deletionRate; | ||
|
|
||
| private SparseLiveDocs sparseLiveDocs; | ||
| private DenseLiveDocs denseLiveDocs; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| Random random = new Random(42); | ||
| int numDeleted = Math.max(1, (int) (maxDoc * deletionRate)); | ||
|
|
||
| SparseFixedBitSet sparseSet = new SparseFixedBitSet(maxDoc); | ||
| FixedBitSet fixedSet = new FixedBitSet(maxDoc); | ||
| fixedSet.set(0, maxDoc); | ||
|
|
||
| for (int i = 0; i < numDeleted; i++) { | ||
| int doc = random.nextInt(maxDoc); | ||
| sparseSet.set(doc); | ||
| fixedSet.clear(doc); | ||
| } | ||
|
|
||
| sparseLiveDocs = SparseLiveDocs.builder(sparseSet, maxDoc).build(); | ||
| denseLiveDocs = DenseLiveDocs.builder(fixedSet, maxDoc).build(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public FixedBitSet copyOfSparseLiveDocs() { | ||
| return FixedBitSet.copyOf(sparseLiveDocs); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public FixedBitSet copyOfDenseLiveDocs() { | ||
| return FixedBitSet.copyOf(denseLiveDocs); | ||
| } | ||
| } |
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
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
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have an interface (Have FixedBitSet, DenseLiveDocs, and SparseLiveDocs all implement it) which could be used here instead of multiple if/else if?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. The interface would make
copyOf()cleaner, but the tricky part is thatFixedBitSetitself would also need to implement it (to handle the case after theFixedBitsunwrap at the top of the method). That means adding atoFixedBitSet()method toFixedBitSetwhose only implementation isreturn clone(), which feels redundant and a bit odd semantically. Happy to go that route if the consensus is that the cleaner dispatch is worth it, but leaning toward keeping theinstanceofchain since it mirrors the existing pattern already in the method forFixedBits/FixedBitSet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, if the interface only covers
DenseLiveDocsandSparseLiveDocs(notFixedBitSet), the semantic oddity goes away. Is that what you had in mind?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either way is fine - if it leads to a reduction of
instanceof, not a huge fan of it (if it keeps on branching)