The value_count aggregation uses the bytesValue() of its ValuesSource. This is simple/convenient since all the different types implement it, and ValueCount doesn't need to specify what kind of ValuesSource it expects.
But the downside is that when value_count is counting up float/doubles or longs we are converting the numerics to Strings first.
This is pretty slow, and creates a ton of temporary garbage due to all the string manipulation:

Since value_count doesn't actually need the value, just the count of values in the field, it wouldn't be too difficult to specialize the aggregator so that slightly different versions are used for each type of ValuesSource. Then it can call the appropriate method (doubleValues(), etc) and avoid all the string casting.
The
value_countaggregation uses thebytesValue()of itsValuesSource. This is simple/convenient since all the different types implement it, and ValueCount doesn't need to specify what kind of ValuesSource it expects.But the downside is that when
value_countis counting up float/doubles or longs we are converting the numerics to Strings first.This is pretty slow, and creates a ton of temporary garbage due to all the string manipulation:
Since
value_countdoesn't actually need the value, just the count of values in the field, it wouldn't be too difficult to specialize the aggregator so that slightly different versions are used for each type of ValuesSource. Then it can call the appropriate method (doubleValues(), etc) and avoid all the string casting.