optimizes setLen for orc; disabling overflow checks (#25722)

ref https://github.com/nim-lang/Nim/issues/25695
ref https://github.com/nim-lang/Nim/pull/25715

This pull request introduces a minor but important change to the
`setLen` procedure in `lib/system/seqs_v2.nim`. The main update is the
temporary disabling of overflow checks during the initialization loop
when extending the sequence length, which can improve performance and
avoid unnecessary checks during this operation.

Memory and performance improvement:

* Disabled overflow checks for the loop that initializes new elements to
their default value when increasing the length of a sequence in
`setLen`, by wrapping the loop with `{.push overflowChecks: off.}` and
`{.pop.}`.

(cherry picked from commit c8e6b059a4)
This commit is contained in:
ringabout
2026-04-09 17:07:04 +08:00
committed by narimiran
parent 73102bddab
commit 488aa1ce86

View File

@@ -174,8 +174,11 @@ proc setLen[T](s: var seq[T], newlen: Natural) {.nodestroy.} =
if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
xu.len = newlen
{.push overflowChecks: off.}
for i in oldLen..<newlen:
xu.p.data[i] = default(T)
{.pop.}
proc newSeq[T](s: var seq[T], len: Natural) =
shrink(s, 0)