Optimize storing into uninit locations for arrays and seqs. (#24619)

(cherry picked from commit 6481482e0e)
This commit is contained in:
Antonis Geralis
2025-01-19 15:20:54 +02:00
committed by narimiran
parent 528e2b2271
commit 52cadfc3d7
2 changed files with 12 additions and 5 deletions

View File

@@ -2963,12 +2963,16 @@ when notJSnotNims and not defined(nimSeqsV2):
assert y == "abcgh"
discard
proc arrayWith*[T](y: T, size: static int): array[size, T] {.raises: [].} =
proc arrayWith*[T](y: T, size: static int): array[size, T] {.noinit, nodestroy, raises: [].} =
## Creates a new array filled with `y`.
for i in 0..size-1:
result[i] = y
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
result[i] = `=dup`(y)
else:
wasMoved(result[i])
`=copy`(result[i], y)
proc arrayWithDefault*[T](size: static int): array[size, T] {.raises: [].} =
proc arrayWithDefault*[T](size: static int): array[size, T] {.noinit, nodestroy, raises: [].} =
## Creates a new array filled with `default(T)`.
for i in 0..size-1:
result[i] = default(T)

View File

@@ -144,8 +144,11 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) {.nodestroy.} =
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
xu.len = newLen
for i in oldLen .. newLen-1:
wasMoved(xu.p.data[i])
`=copy`(xu.p.data[i], value)
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
xu.p.data[i] = `=dup`(value)
else:
wasMoved(xu.p.data[i])
`=copy`(xu.p.data[i], value)
proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
## Generic proc for adding a data item `y` to a container `x`.