Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

faster HashDocSet.unionSize

Yonik Seeley2f9eb801bac2
diff --git a/CHANGES.txt b/CHANGES.txt
index b667825c293..fcc89de12c7 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -30,8 +30,9 @@ Optimizations
2. BitDocSet.intersectionSize(HashDocSet) no longer generates an intermediate
set
3. OpenBitSet completed, replaces BitSet as the implementation for BitDocSet.
- Iteration is faster, and BitDocSet.intersectionSize(BitDocSet)
+ Iteration is faster, and BitDocSet.intersectionSize(BitDocSet) and unionSize
is between 3 and 4 times faster. (yonik, SOLR-15)
+ 4. much faster unionSize when one of the sets is a HashDocSet: O(smaller_set_size)
Bug Fixes
1. Fixed delete-by-id for field types who's indexed form is different
diff --git a/src/java/org/apache/solr/search/HashDocSet.java b/src/java/org/apache/solr/search/HashDocSet.java
index 14991d72cee..97962bd3b37 100644
--- a/src/java/org/apache/solr/search/HashDocSet.java
+++ b/src/java/org/apache/solr/search/HashDocSet.java
@@ -212,7 +212,7 @@ public final class HashDocSet extends DocSetBase {
return (tablesize<<2) + 20;
}
-
+ @Override
public DocSet intersection(DocSet other) {
if (other instanceof HashDocSet) {
// set "a" to the smallest doc set for the most efficient
@@ -245,6 +245,7 @@ public final class HashDocSet extends DocSetBase {
}
+ @Override
public int intersectionSize(DocSet other) {
if (other instanceof HashDocSet) {
// set "a" to the smallest doc set for the most efficient
@@ -273,5 +274,33 @@ public final class HashDocSet extends DocSetBase {
}
+ @Override
+ public int unionSize(DocSet other) {
+ if (other instanceof HashDocSet) {
+ // set "a" to the smallest doc set for the most efficient
+ // intersection.
+ final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other;
+ final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this;
+
+ int resultCount=b.size();
+ for (int i=0; i<a.table.length; i++) {
+ int id=a.table[i];
+ if (id >= 0 && !b.exists(id)) {
+ resultCount++;
+ }
+ }
+ return resultCount;
+ } else {
+ int resultCount=other.size();
+ for (int i=0; i<table.length; i++) {
+ int id=table[i];
+ if (id >= 0 && !other.exists(id)) {
+ resultCount++;
+ }
+ }
+ return resultCount;
+ }
+ }
+
}