Skip to content

Commit 6481482

Browse files
authored
Optimize storing into uninit locations for arrays and seqs. (#24619)
1 parent 2af9ddc commit 6481482

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/system.nim

+7-5
Original file line numberDiff line numberDiff line change
@@ -2965,14 +2965,16 @@ when notJSnotNims and not defined(nimSeqsV2):
29652965
assert y == "abcgh"
29662966
discard
29672967

2968-
proc arrayWith*[T](y: T, size: static int): array[size, T] {.raises: [].} =
2968+
proc arrayWith*[T](y: T, size: static int): array[size, T] {.noinit, nodestroy, raises: [].} =
29692969
## Creates a new array filled with `y`.
2970-
result = zeroDefault(array[size, T])
29712970
for i in 0..size-1:
2972-
result[i] = y
2971+
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
2972+
result[i] = `=dup`(y)
2973+
else:
2974+
wasMoved(result[i])
2975+
`=copy`(result[i], y)
29732976

2974-
proc arrayWithDefault*[T](size: static int): array[size, T] {.raises: [].} =
2977+
proc arrayWithDefault*[T](size: static int): array[size, T] {.noinit, nodestroy, raises: [].} =
29752978
## Creates a new array filled with `default(T)`.
2976-
result = zeroDefault(array[size, T])
29772979
for i in 0..size-1:
29782980
result[i] = default(T)

lib/system/seqs_v2.nim

+5-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) {.nodestroy.} =
144144
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
145145
xu.len = newLen
146146
for i in oldLen .. newLen-1:
147-
wasMoved(xu.p.data[i])
148-
`=copy`(xu.p.data[i], value)
147+
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
148+
xu.p.data[i] = `=dup`(value)
149+
else:
150+
wasMoved(xu.p.data[i])
151+
`=copy`(xu.p.data[i], value)
149152

150153
proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
151154
## Generic proc for adding a data item `y` to a container `x`.

0 commit comments

Comments
 (0)