Skip to content

Commit 62a74d0

Browse files
authored
Speed up interval rounding (#63245)
This speeds up date_histogram by precomputing the rounding points for date intervals like `10d`. The speedup for the rounding itself is between 18% (UTC many buckets) and 65% (US Eastern Time few buckets). 43% seems like it'd be pretty common: ``` Benchmark (count) (interval) (range) (zone) Mode Cnt Score Error Units before 10000000 10d 2000-10-28 to 2000-10-31 UTC avgt 10 1308223.700 ± 177466.657 ns/op before 10000000 10d 2000-10-28 to 2000-10-31 America/New_York avgt 10 189236837.930 ± 7958933.566 ns/op after 10000000 10d 2000-10-28 to 2000-10-31 UTC avgt 10 66413746.325 ± 1578834.032 ns/op after 10000000 10d 2000-10-28 to 2000-10-31 America/New_York avgt 10 65656941.375 ± 291608.870 ns/op before 10000000 2h 2000-10-28 to 2000-10-31 UTC avgt 10 130854975.013 ± 369133.702 ns/op before 10000000 2h 2000-10-28 to 2000-10-31 America/New_York avgt 10 165831615.257 ± 139074.982 ns/op after 10000000 2h 2000-10-28 to 2000-10-31 UTC avgt 10 107832636.671 ± 3502704.198 ns/op after 10000000 2h 2000-10-28 to 2000-10-31 America/New_York avgt 10 107608802.940 ± 979286.160 ns/op ``` Speedup for the date_histogram is likely to vary based on how much IO dominates the collection.
1 parent 490501d commit 62a74d0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

server/src/main/java/org/elasticsearch/common/Rounding.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,15 @@ public byte id() {
904904

905905
@Override
906906
public Prepared prepare(long minUtcMillis, long maxUtcMillis) {
907+
/*
908+
* 128 is a power of two that isn't huge. We might be able to do
909+
* better if the limit was based on the actual type of prepared
910+
* rounding but this'll do for now.
911+
*/
912+
return prepareOffsetOrJavaTimeRounding(minUtcMillis, maxUtcMillis).maybeUseArray(minUtcMillis, maxUtcMillis, 128);
913+
}
914+
915+
private TimeIntervalPreparedRounding prepareOffsetOrJavaTimeRounding(long minUtcMillis, long maxUtcMillis) {
907916
long minLookup = minUtcMillis - interval;
908917
long maxLookup = maxUtcMillis;
909918

@@ -928,7 +937,7 @@ public Prepared prepareForUnknown() {
928937
}
929938

930939
@Override
931-
Prepared prepareJavaTime() {
940+
TimeIntervalPreparedRounding prepareJavaTime() {
932941
return new JavaTimeRounding();
933942
}
934943

@@ -972,7 +981,7 @@ private long roundKey(long value, long interval) {
972981
}
973982
}
974983

975-
private abstract class TimeIntervalPreparedRounding implements Prepared {
984+
private abstract class TimeIntervalPreparedRounding extends PreparedRounding {
976985
@Override
977986
public double roundingSize(long utcMillis, DateTimeUnit timeUnit) {
978987
if (timeUnit.isMillisBased) {

0 commit comments

Comments
 (0)