Skip to content

Commit

Permalink
avoid zeroMem when resizing trivial seq:s
Browse files Browse the repository at this point in the history
might as well grab a new sequence instead
  • Loading branch information
arnetheduck committed Jun 16, 2022
1 parent b3ea2c6 commit 4a3dd80
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion stew/assign2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ func assign*[T](tgt: var openArray[T], src: openArray[T]) =
func assign*[T](tgt: var seq[T], src: openArray[T]) =
mixin assign

tgt.setLen(src.len)
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)

func assign*(tgt: var string, src: string) =
Expand Down

0 comments on commit 4a3dd80

Please sign in to comment.