From d1e2e3ae682f93ae037859a1ddbf6ed04c608f3f Mon Sep 17 00:00:00 2001 From: He-Pin Date: Mon, 22 Dec 2025 10:56:09 +0800 Subject: [PATCH 1/2] chore: Avoid copy with ArrayBuilder --- sjsonnet/src/sjsonnet/stdlib/SetModule.scala | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala index 65f4a364..6613b82d 100644 --- a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala +++ b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala @@ -65,20 +65,23 @@ object SetModule extends AbstractFunctionModule { val out = new mutable.ArrayBuilder.ofRef[Lazy] // Set a reasonable size hint - in the worst case (no duplicates), we'll need arrValue.length elements out.sizeHint(arrValue.length) + var last: Lazy = null for (v <- arrValue) { - val outResult = out.result() - if (outResult.length == 0) { + if (out.knownSize == 0) { out.+=(v) + last = v } else if (keyF.isInstanceOf[Val.False]) { - if (!ev.equal(outResult.last.force, v.force)) { + if (!ev.equal(last.force, v.force)) { out.+=(v) + last = v } - } else if (!keyF.isInstanceOf[Val.False]) { + } else { val keyFFunc = keyF.asInstanceOf[Val.Func] val o1Key = keyFFunc.apply1(v, pos.noOffset)(ev, TailstrictModeDisabled) - val o2Key = keyFFunc.apply1(outResult.last, pos.noOffset)(ev, TailstrictModeDisabled) + val o2Key = keyFFunc.apply1(last, pos.noOffset)(ev, TailstrictModeDisabled) if (!ev.equal(o1Key, o2Key)) { out.+=(v) + last = v } } } From 2ae97d68c84875dd8f18c142f0cb1454d40f0953 Mon Sep 17 00:00:00 2001 From: He-Pin Date: Mon, 22 Dec 2025 11:02:30 +0800 Subject: [PATCH 2/2] . --- sjsonnet/src/sjsonnet/stdlib/SetModule.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala index 6613b82d..5a3fb0b0 100644 --- a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala +++ b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala @@ -66,14 +66,17 @@ object SetModule extends AbstractFunctionModule { // Set a reasonable size hint - in the worst case (no duplicates), we'll need arrValue.length elements out.sizeHint(arrValue.length) var last: Lazy = null + var knownSize = 0 for (v <- arrValue) { - if (out.knownSize == 0) { + if (knownSize == 0) { out.+=(v) last = v + knownSize += 1 } else if (keyF.isInstanceOf[Val.False]) { if (!ev.equal(last.force, v.force)) { out.+=(v) last = v + knownSize += 1 } } else { val keyFFunc = keyF.asInstanceOf[Val.Func] @@ -82,6 +85,7 @@ object SetModule extends AbstractFunctionModule { if (!ev.equal(o1Key, o2Key)) { out.+=(v) last = v + knownSize += 1 } } }