avoid zeroMem when resizing trivial seq:s

might as well grab a new sequence instead
This commit is contained in:
Jacek Sieka 2022-04-14 21:57:11 +02:00
parent 266e9002f3
commit 063e0c98b8
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
1 changed files with 8 additions and 1 deletions

View File

@ -32,6 +32,13 @@ func assign*[T](tgt: var openArray[T], src: openArray[T]) =
func assign*[T](tgt: var seq[T], src: openArray[T]) =
mixin assign
when T is SomeNumber:
# `setLen` does costly zero:ing both when growing _and_ shrinking
if tgt.len > src.len or tgt.len < src.len div 4:
tgt = newSeqUninitialized[T](src.len)
else:
tgt.setLen(src.len)
else:
tgt.setLen(src.len)
assignImpl(tgt.toOpenArray(0, tgt.high), src)