Skip to content

Commit 7c4a006

Browse files
committed
Optimize @, fixes #25063
1 parent cd806f9 commit 7c4a006

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
@@ -1465,15 +1465,26 @@ proc isNil*[T: proc | iterator {.closure.}](x: T): bool {.noSideEffect, magic: "
14651465
## Fast check whether `x` is nil. This is sometimes more efficient than
14661466
## `== nil`.
14671467

1468+
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
1469+
14681470
when defined(nimHasTopDownInference):
14691471
# magic used for seq type inference
14701472
proc `@`*[T](a: openArray[T]): seq[T] {.magic: "OpenArrayToSeq".} =
14711473
## Turns an *openArray* into a sequence.
14721474
##
14731475
## This is not as efficient as turning a fixed length array into a sequence
14741476
## as it always copies every element of `a`.
1475-
newSeq(result, a.len)
1476-
for i in 0..a.len-1: result[i] = a[i]
1477+
let sz = a.len
1478+
when supportsCopyMem(T) and not defined(js):
1479+
result = newSeqUninit[T](sz)
1480+
when nimvm:
1481+
for i in 0..sz-1: result[i] = a[i]
1482+
else:
1483+
if sz != 0:
1484+
copyMem(addr result[0], addr a[0], sizeof(T) * sz)
1485+
else:
1486+
newSeq(result, sz)
1487+
for i in 0..sz-1: result[i] = a[i]
14771488
else:
14781489
proc `@`*[T](a: openArray[T]): seq[T] =
14791490
## Turns an *openArray* into a sequence.
@@ -1644,8 +1655,6 @@ when not defined(js) and defined(nimV2):
16441655
vTable: UncheckedArray[pointer] # vtable for types
16451656
PNimTypeV2 = ptr TNimTypeV2
16461657

1647-
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
1648-
16491658
when notJSnotNims and defined(nimSeqsV2):
16501659
include "system/strs_v2"
16511660
include "system/seqs_v2"

0 commit comments

Comments
 (0)