diff --git a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala index 65f4a364..5a3fb0b0 100644 --- a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala +++ b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala @@ -65,20 +65,27 @@ 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 + var knownSize = 0 for (v <- arrValue) { - val outResult = out.result() - if (outResult.length == 0) { + if (knownSize == 0) { out.+=(v) + last = v + knownSize += 1 } 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 + knownSize += 1 } - } 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 + knownSize += 1 } } }