From 34c3464bc9ef488aab262a8da624415d416b1a8c Mon Sep 17 00:00:00 2001 From: Charles Allen Date: Thu, 13 Nov 2014 10:47:56 -0800 Subject: [PATCH] Added more explanatory comments in PooledTopNAlgorithm --- .../druid/query/topn/PooledTopNAlgorithm.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/processing/src/main/java/io/druid/query/topn/PooledTopNAlgorithm.java b/processing/src/main/java/io/druid/query/topn/PooledTopNAlgorithm.java index 26ddd446c86..294c13d7db5 100644 --- a/processing/src/main/java/io/druid/query/topn/PooledTopNAlgorithm.java +++ b/processing/src/main/java/io/druid/query/topn/PooledTopNAlgorithm.java @@ -144,7 +144,24 @@ public class PooledTopNAlgorithm { return makeBufferAggregators(params.getCursor(), query.getAggregatorSpecs()); } - + /** + * Use aggressive loop unrolling to aggregate the data + * + * How this works: The aggregates are evaluated AGG_UNROLL_COUNT at a time. This was chosen to be 8 rather arbitrarily. + * The offsets into the output buffer are precalculated and stored in aggregatorOffsets + * + * For queries whose aggregate count is less than AGG_UNROLL_COUNT, the aggregates evaluted in a switch statement. + * See http://en.wikipedia.org/wiki/Duff's_device for more information on this kind of approach + * + * This allows out of order execution of the code. In local tests, the JVM inlines all the way to this function. + * + * If there are more than AGG_UNROLL_COUNT aggregates, then the remainder is calculated with the switch, and the + * blocks of AGG_UNROLL_COUNT are calculated in a partially unrolled for-loop. + * + * Putting the switch first allows for optimization for the common case (less than AGG_UNROLL_COUNT aggs) but + * still optimizes the high quantity of aggregate queries which benefit greatly from any speed improvements + * (they simply take longer to start with). + */ @Override protected void scanAndAggregate( final PooledTopNParams params,