From b9d479c81911f1e4eee2f72b4bb1bf60699ae497 Mon Sep 17 00:00:00 2001 From: He-Pin Date: Sat, 4 Apr 2026 23:31:38 +0800 Subject: [PATCH] perf: array compare reference equality + inline numeric fast path Add reference equality check (xi eq yi) in the array comparison loop before type dispatch. When arrays share elements (e.g., long_array + [1] < long_array + [2] where long_array has 1M shared elements), this skips type dispatch for all identical references. Also inline numeric fast path (double compare without polymorphic compare() dispatch) for the common case where both elements are Num. Upstream: jit branch commits f7ad961a, 62437d8f --- sjsonnet/src/sjsonnet/Evaluator.scala | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/sjsonnet/src/sjsonnet/Evaluator.scala b/sjsonnet/src/sjsonnet/Evaluator.scala index 2e52c2cd..783e5fd7 100644 --- a/sjsonnet/src/sjsonnet/Evaluator.scala +++ b/sjsonnet/src/sjsonnet/Evaluator.scala @@ -1190,11 +1190,24 @@ class Evaluator( val len = math.min(x.length, y.length) var i = 0 while (i < len) { - val cmp = compare(x.value(i), y.value(i)) - if (cmp != 0) return cmp + val xi = x.value(i) + val yi = y.value(i) + // Reference equality short-circuit for shared array elements + if (!(xi eq yi)) { + // Inline numeric fast path to avoid polymorphic compare() dispatch + val cmp = xi match { + case xn: Val.Num => + yi match { + case yn: Val.Num => java.lang.Double.compare(xn.asDouble, yn.asDouble) + case _ => compare(xi, yi) + } + case _ => compare(xi, yi) + } + if (cmp != 0) return cmp + } i += 1 } - Ordering[Int].compare(x.length, y.length) + Integer.compare(x.length, y.length) case _ => Error.fail("Cannot compare " + x.prettyName + " with " + y.prettyName, x.pos) }