Skip to content

Apache Lucene open-source search software

SOLR-7239: improved performance of min & max in StatsComponent, as well as situations where local params disable all stats

Chris M. Hostetterc9d344f09dfd
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index e960c9380df..883b675bd1d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -243,6 +243,9 @@ Optimizations
* SOLR-7116: Distributed facet refinement requests would needlessly compute other types
of faceting that have already been computed. (David Smiley, Hossman)
+ * SOLR-7239: improved performance of min & max in StatsComponent, as well as situations
+ where local params disable all stats (hossman)
+
Other Changes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/component/StatsField.java b/solr/core/src/java/org/apache/solr/handler/component/StatsField.java
index 78d3a5e1273..747f914674e 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/StatsField.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/StatsField.java
@@ -346,6 +346,12 @@ public class StatsField {
*/
public StatsValues computeLocalStatsValues(DocSet base) throws IOException {
+ if (statsToCalculate.isEmpty()) {
+ // perf optimization for the case where we compute nothing
+ // ie: stats.field={!min=$domin}myfield&domin=false
+ return StatsValuesFactory.createStatsValues(this);
+ }
+
if (null != schemaField
&& (schemaField.multiValued() || schemaField.getType().multiValuedFieldCache())) {
diff --git a/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java b/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java
index c4682032587..f2cb8d9043d 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java
@@ -347,6 +347,9 @@ class NumericStatsValues extends AbstractStatsValues<Number> {
double sum;
double sumOfSquares;
+ double minD; // perf optimization, only valid if (null != this.min)
+ double maxD; // perf optimization, only valid if (null != this.max)
+
final protected boolean computeSum;
final protected boolean computeSumOfSquares;
@@ -405,13 +408,19 @@ class NumericStatsValues extends AbstractStatsValues<Number> {
if (computeMin) { // nested if to encourage JIT to optimize aware final var?
if (null != min) {
double minD = min.doubleValue();
- this.min = (null == this.min) ? minD : Math.min(this.min.doubleValue(), minD);
+ if (null == this.min || minD < this.minD) {
+ // Double for result & cached primitive doulbe to minimize unboxing in future comparisons
+ this.min = this.minD = minD;
+ }
}
}
if (computeMax) { // nested if to encourage JIT to optimize aware final var?
if (null != max) {
double maxD = max.doubleValue();
- this.max = (null == this.max) ? maxD : Math.max(this.max.doubleValue(), maxD);
+ if (null == this.max || this.maxD < maxD) {
+ // Double for result & cached primitive doulbe to minimize unboxing in future comparisons
+ this.max = this.maxD = maxD;
+ }
}
}
}
diff --git a/solr/core/src/test/org/apache/solr/TestDistributedSearch.java b/solr/core/src/test/org/apache/solr/TestDistributedSearch.java
index 9bf807d5b18..45894dc3641 100644
--- a/solr/core/src/test/org/apache/solr/TestDistributedSearch.java
+++ b/solr/core/src/test/org/apache/solr/TestDistributedSearch.java
@@ -494,6 +494,26 @@ public class TestDistributedSearch extends BaseDistributedSearchTestCase {
assertNull("expected null for sum", s.getSum());
}
+ // request stats, but disable them all via param refs
+ rsp = query("q","*:*", "sort",i1+" desc", "stats", "true", "doMin", "false",
+ "stats.field", "{!min=$doMin}" + i1);
+ { // don't leak variables
+ FieldStatsInfo s = rsp.getFieldStatsInfo().get(i1);
+ // stats section should exist, even though stats should be null
+ assertNotNull("no stats for " + i1, s);
+ //
+ assertNull("expected null for min", s.getMin() );
+ assertNull("expected null for mean", s.getMean() );
+ assertNull("expected null for stddev", s.getStddev() );
+ //
+ assertNull("expected null for count", s.getCount());
+ assertNull("expected null for calcDistinct", s.getCountDistinct());
+ assertNull("expected null for distinct vals", s.getDistinctValues());
+ assertNull("expected null for max", s.getMax());
+ assertNull("expected null for missing", s.getMissing());
+ assertNull("expected null for sum", s.getSum());
+ }
+
final String[] stats = new String[] {
"min", "max", "sum", "sumOfSquares", "stddev", "mean", "missing", "count"
};
diff --git a/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java
index f4cf3841592..3c983fbf39d 100644
--- a/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java
@@ -1163,6 +1163,9 @@ public class StatsComponentTest extends AbstractSolrTestCase {
"but since only local param is false no stats should be returned",
req("q","*:*", "stats", "true",
"stats.field", "{!key=k min=false}a_i")
+ // section of stats for this field should exist ...
+ , XPRE + "lst[@name='stats_fields']/lst[@name='k']"
+ // ...but be empty
, "count(" + kpre + "*)=0"
);