mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-09 19:56:09 +00:00
d8d914332d
* random helpers * arrayops as a home for small array/openArray utilities * assign2 - a replacement for genericAssign and assignment operators in general which in nim are very slow * use assign2 in a few places to speed things up * fixes * fixes
23 lines
833 B
Nim
23 lines
833 B
Nim
import
|
|
std/typetraits,
|
|
./assign2
|
|
|
|
func write*[T](s: var seq[T], v: openArray[T]) =
|
|
# The Nim standard library is inefficient when copying simple types
|
|
# into a seq: it will first zero-init the new memory then copy the items
|
|
# one by one, when a copyMem would be sufficient - semantically, this
|
|
# function performs the same thing as `add`, but similar to faststreams, from
|
|
# where the `write` name comes from, it is much faster. Unfortunately, there's
|
|
# no easy way to avoid the zero-init, but a smart compiler might be able
|
|
# to elide it.
|
|
when nimvm:
|
|
s.add(v)
|
|
else:
|
|
if v.len > 0:
|
|
let start = s.len
|
|
s.setLen(start + v.len)
|
|
when supportsCopyMem(T): # shortcut
|
|
copyMem(addr s[start], unsafeAddr v[0], v.len * sizeof(T))
|
|
else:
|
|
assign(s.toOpenArray(start, s.high), v)
|