Skip to content

Commit 87cc6d0

Browse files
yglukhovringabout
authored andcommitted
Optimize @, fixes #25063 (#25064)
Co-authored-by: ringabout <[email protected]> (cherry picked from commit 49e66e8)
1 parent 031bbcd commit 87cc6d0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/system.nim

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,15 +1449,26 @@ proc isNil*[T: proc | iterator {.closure.}](x: T): bool {.noSideEffect, magic: "
14491449
## Fast check whether `x` is nil. This is sometimes more efficient than
14501450
## `== nil`.
14511451

1452+
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
1453+
14521454
when defined(nimHasTopDownInference):
14531455
# magic used for seq type inference
14541456
proc `@`*[T](a: openArray[T]): seq[T] {.magic: "OpenArrayToSeq".} =
14551457
## Turns an *openArray* into a sequence.
14561458
##
14571459
## This is not as efficient as turning a fixed length array into a sequence
14581460
## as it always copies every element of `a`.
1459-
newSeq(result, a.len)
1460-
for i in 0..a.len-1: result[i] = a[i]
1461+
let sz = a.len
1462+
when supportsCopyMem(T) and not defined(js):
1463+
result = newSeqUninit[T](sz)
1464+
when nimvm:
1465+
for i in 0..sz-1: result[i] = a[i]
1466+
else:
1467+
if sz != 0:
1468+
copyMem(addr result[0], addr a[0], sizeof(T) * sz)
1469+
else:
1470+
newSeq(result, sz)
1471+
for i in 0..sz-1: result[i] = a[i]
14611472
else:
14621473
proc `@`*[T](a: openArray[T]): seq[T] =
14631474
## Turns an *openArray* into a sequence.
@@ -1628,8 +1639,6 @@ when not defined(js) and defined(nimV2):
16281639
vTable: UncheckedArray[pointer] # vtable for types
16291640
PNimTypeV2 = ptr TNimTypeV2
16301641

1631-
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
1632-
16331642
when notJSnotNims and defined(nimSeqsV2):
16341643
include "system/strs_v2"
16351644
include "system/seqs_v2"

0 commit comments

Comments
 (0)