#160426·elasticsearch

`NumberType#objectToLong` round-trips boxed `Integer`/`Short`/`Byte` through a string

Author: uchiha-ashaCreated Sep 28, 2026Updated Sep 28, 2026
LabelsTeam:Search Foundations:Search Foundations/Search

Description

NumberFieldMapper.NumberType#objectToLong returns Long inputs directly. Every other input, boxed Integer, Short and Byte included, goes the general way:

java
double doubleValue = objectToDouble(value);
// range check, decimal-part check ...
String stringValue = (value instanceof BytesRef) ? ((BytesRef) value).utf8ToString() : value.toString();
return Numbers.toLong(stringValue, coerce);

So an Integer is converted to a double, then formatted with Integer.toString, then parsed back with Long.parseLong: a string conversion for every value. An Integer, Short or Byte always fits in a long and never has a decimal part, so none of those steps can change the result or throw.

Why it is on the hot path

TermsQueryBuilder.parseValues keeps numbers as the parser returns them, and the JSON parser returns Integer for any integral value that fits in an int. A terms query on a long field with an id list, the typical shape, therefore reaches LONG.termsQuery as a list of Integers, and each one goes through the string round trip:

  • TermsQueryBuilder#doToQuery → NumberFieldType#termsQuery → NumberType.LONG#termsQuery
  • LONG#termsQuery → LONG#parse(Object, boolean) → objectToLong → toString → Long.parseLong

LONG#parse only delegates to objectToLong and boxes its long result into a Long, which is then unboxed straight into a long[]. So each value goes through a string conversion and an intermediate Long box. How much of that is actually allocated depends on the Long cache (-128 to 127) and on whether the JIT's escape analysis removes the box; the benchmark below measures it rather than assuming it.

The cost scales with (values per query) × (shards per request) × query rate, and it is paid again every time the query is rebuilt on a shard.

Evidence

The following comes from a JFR allocation profile of a production node (allocation sampling, not reproduced here). terms queries on a long field carried about 33k integer ids, and each query was built twice per shard request. The profile attributed 28.4% of all allocation, about 2.6 GiB/s, to the TermsQueryBuilder#doToQuery subtree. Within it, NumberType#objectToLong accounted for 15.3% of total allocation and NumberType$LONG#parse for 6.6%. Those are the frames where the string round trip and the Long boxing described above happen.

A JMH benchmark (LongTermsQueryBuildBenchmark, added in the PR) builds NumberType.LONG#termsQuery from Integer and from Long lists, and times objectToLong on its own. Apple M1 Max, 10 cores, JDK 27-ea+34, -f 1 -t 1 -wi 3 -i 5 -w 1s -r 1s -prof gc. One op converts the whole list:

Method nTerms Input With round trip (µs/op) With fast path (µs/op) B/value
objectToLong 10000 Integer 284.785 ± 1.868 8.027 ± 0.060 24.0 → 0.0
objectToLong 100000 Integer 2945.082 ± 453.361 80.453 ± 1.528 24.0 → 0.0
termsQuery 10000 Integer 1169.785 ± 96.895 809.069 ± 22.551 45.6 → 21.6
termsQuery 100000 Integer 13846.347 ± 1772.008 9842.920 ± 204.962 51.9 → 27.9
termsQuery 100000 Long (control) 10316.518 ± 989.346 9855.301 ± 49.567 27.9 → 27.9

With the fast path, Integer input allocates approximately 24 fewer bytes per value, the same as Long input within measurement noise. Dropping parse also removes a source-level boxing operation; these measurements do not isolate its allocation contribution. The benchmark, not the profile above, carries the quantitative claim. Caveats: single fork, 5 iterations, one machine, so treat these as directional.

Precedent in the codebase

The same short-circuit already exists elsewhere for exactly these types:

  • Numbers#toLongExact(Number) handles Byte/Short/Integer/Long with longValue() directly.
  • NumberType.LONG#encodeIndexTermOrNull, added in #155545, handles Long, Integer, Short and Byte directly before falling back to parse.
  • NumberType#hasDecimalPart already returns false early for all four types, so LONG#termsQuery has already classified each value as integral before calling parse.

Proposal

  • In objectToLong, return ((Number) value).longValue() for Integer, Short and Byte, next to the existing Long check.
  • In LONG#termsQuery, call objectToLong(value, true) instead of parse(value, true), so the result goes straight into the long[] without being boxed into a Long first.

Results and exceptions stay the same for every input, because the three types cannot be out of range or carry a decimal part. The other callers of objectToLong get the same speed-up for integral boxed input: long range query bounds, LongScriptFieldType#termQuery/termsQuery, and LongFieldScript#emit(Object).

Opened as #160427.