From d31b232c0d6b1fe86b275ce9f1a04815b082ed6e Mon Sep 17 00:00:00 2001 From: He-Pin Date: Sun, 5 Apr 2026 20:54:06 +0800 Subject: [PATCH] perf: inline numeric fast path in array comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inline Val.Num type check in the array comparison while loop to avoid polymorphic recursive compare() dispatch per element. For numeric array comparisons (e.g. 1M elements), this eliminates 1M recursive method calls with 5-branch pattern matching overhead. Uses asDouble (not raw extraction) to preserve NaN error behavior — std.log(-1) etc. can produce NaN in Val.Num, and asDouble correctly throws 'not a number', matching the official C++ jsonnet behavior. Uses java.lang.Double.compare() instead of compareTo() to avoid autoboxing overhead. Upstream: jit branch commit 62437d8f --- sjsonnet/src/sjsonnet/Evaluator.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sjsonnet/src/sjsonnet/Evaluator.scala b/sjsonnet/src/sjsonnet/Evaluator.scala index a37fcf99..d37c3fd0 100644 --- a/sjsonnet/src/sjsonnet/Evaluator.scala +++ b/sjsonnet/src/sjsonnet/Evaluator.scala @@ -1387,7 +1387,9 @@ class Evaluator( 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 + // Inline numeric fast path to avoid polymorphic compare() dispatch per element. + // In numeric array comparisons (e.g. 1M elements), this avoids recursive + // method calls with polymorphic dispatch across 5 type branches. val cmp = xi match { case xn: Val.Num => yi match {