-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Apply GCD bound transform to sorted numeric rangeIntoBitSet #16285
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
costin
wants to merge
7
commits into
apache:main
Choose a base branch
from
costin:lucene/sorted-numeric-gcd-range
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.
+526
−28
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50446c5
Apply GCD bound transform to sorted numeric rangeIntoBitSet
costin c670d3c
Add CHANGES.txt entries for sorted numeric rangeIntoBitSet
costin c2c502f
Use TermQuery lead in benchmark to force DenseConjunction path
costin d1d5f36
Add test coverage for GCD transform edge cases
costin 2be9d4e
Unify GCD and non-GCD rangeIntoBitSet paths
costin ae645ed
Merge remote-tracking branch 'upstream/main' into lucene/sorted-numer…
costin d02f0cb
Update CHANGES.txt
costin 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
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
194 changes: 194 additions & 0 deletions
194
...mh/src/java/org/apache/lucene/benchmark/jmh/SortedNumericGcdRangeIntoBitSetBenchmark.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,194 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Comparator; | ||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Stream; | ||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.Field; | ||
| import org.apache.lucene.document.SortedNumericDocValuesField; | ||
| import org.apache.lucene.document.StringField; | ||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.IndexWriter; | ||
| import org.apache.lucene.index.IndexWriterConfig; | ||
| import org.apache.lucene.index.Term; | ||
| import org.apache.lucene.search.BooleanClause.Occur; | ||
| import org.apache.lucene.search.BooleanQuery; | ||
| import org.apache.lucene.search.IndexSearcher; | ||
| import org.apache.lucene.search.Query; | ||
| import org.apache.lucene.search.TermQuery; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.MMapDirectory; | ||
| 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.TearDown; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * Benchmarks range queries over GCD/delta-encoded sorted numeric doc values with multiple values | ||
| * per doc. | ||
| */ | ||
| @State(Scope.Thread) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @Warmup(iterations = 3, time = 3) | ||
| @Measurement(iterations = 5, time = 5) | ||
| public class SortedNumericGcdRangeIntoBitSetBenchmark { | ||
|
|
||
| private static final String FIELD = "val"; | ||
| private static final String LEAD_FIELD = "lead"; | ||
| private static final String LEAD_VALUE = "yes"; | ||
| private static final long DOMAIN = 10_000_000L; | ||
| private static final long DELTA = 1_700_000_000_000L; | ||
|
|
||
| private Directory dir; | ||
| private DirectoryReader reader; | ||
| private IndexSearcher searcher; | ||
| private Path path; | ||
| private Query query; | ||
|
|
||
| @Param({"1000000"}) | ||
| public int numDocs; | ||
|
|
||
| @Param({"delta_only", "gcd_1000", "gcd_100_delta"}) | ||
| public String encoding; | ||
|
|
||
| @Param({"1", "3", "5"}) | ||
| public int cardinality; | ||
|
|
||
| @Param({"0.01", "0.1", "0.5"}) | ||
| public double selectivity; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() throws Exception { | ||
| path = Files.createTempDirectory("sortedNumericGcdRange"); | ||
| dir = MMapDirectory.open(path); | ||
|
|
||
| Random random = new Random(0); | ||
| try (IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig())) { | ||
| for (int i = 0; i < numDocs; i++) { | ||
| Document doc = new Document(); | ||
| long base = valueForDoc(i, random); | ||
| for (int c = 0; c < cardinality; c++) { | ||
| doc.add(SortedNumericDocValuesField.indexedField(FIELD, base + c * step())); | ||
| } | ||
| doc.add(new StringField(LEAD_FIELD, LEAD_VALUE, Field.Store.NO)); | ||
| writer.addDocument(doc); | ||
| } | ||
| writer.forceMerge(1); | ||
| } | ||
|
|
||
| reader = DirectoryReader.open(dir); | ||
| searcher = new IndexSearcher(reader); | ||
| query = rangeQuery(); | ||
| } | ||
|
|
||
| private long valueForDoc(int doc, Random random) { | ||
| long value = random.nextLong(0, DOMAIN); | ||
| return switch (encoding) { | ||
| case "delta_only" -> DELTA + value; | ||
| case "gcd_1000" -> value * 1_000L; | ||
| case "gcd_100_delta" -> DELTA + value * 100L; | ||
| default -> throw new IllegalArgumentException("Unknown encoding: " + encoding); | ||
| }; | ||
| } | ||
|
|
||
| private long step() { | ||
| return switch (encoding) { | ||
| case "delta_only" -> 1; | ||
| case "gcd_1000" -> 1_000L; | ||
| case "gcd_100_delta" -> 100L; | ||
| default -> throw new IllegalArgumentException("Unknown encoding: " + encoding); | ||
| }; | ||
| } | ||
|
|
||
| private Query rangeQuery() { | ||
| long range = Math.max(1, (long) (DOMAIN * selectivity)); | ||
| long min = (DOMAIN - range) / 2; | ||
| long max = min + range; | ||
| long actualMin = actualValue(min); | ||
| long actualMax = actualValue(max); | ||
| Query rangeQuery = SortedNumericDocValuesField.newSlowRangeQuery(FIELD, actualMin, actualMax); | ||
| return new BooleanQuery.Builder() | ||
| .add(new TermQuery(new Term(LEAD_FIELD, LEAD_VALUE)), Occur.FILTER) | ||
| .add(rangeQuery, Occur.FILTER) | ||
| .build(); | ||
| } | ||
|
|
||
| private long actualValue(long value) { | ||
| return switch (encoding) { | ||
| case "delta_only" -> DELTA + value; | ||
| case "gcd_1000" -> value * 1_000L; | ||
| case "gcd_100_delta" -> DELTA + value * 100L; | ||
| default -> throw new IllegalArgumentException("Unknown encoding: " + encoding); | ||
| }; | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() throws Exception { | ||
| reader.close(); | ||
| dir.close(); | ||
| if (Files.exists(path)) { | ||
| try (Stream<Path> walk = Files.walk(path)) { | ||
| walk.sorted(Comparator.reverseOrder()) | ||
| .forEach( | ||
| p -> { | ||
| try { | ||
| Files.delete(p); | ||
| } catch (IOException _) { | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork( | ||
| value = 1, | ||
| jvmArgsAppend = {"-Xmx2g", "-Xms2g", "-XX:+AlwaysPreTouch"}) | ||
| public int rangeQueryDefaultProvider() throws IOException { | ||
| return searcher.count(query); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork( | ||
| value = 1, | ||
| jvmArgsAppend = { | ||
| "--add-modules", | ||
| "jdk.incubator.vector", | ||
| "-Xmx2g", | ||
| "-Xms2g", | ||
| "-XX:+AlwaysPreTouch" | ||
| }) | ||
| public int rangeQueryPanamaProvider() throws IOException { | ||
| return searcher.count(query); | ||
| } | ||
| } |
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.