diff options
author | Araq <rumpf_a@web.de> | 2019-04-25 13:54:25 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2019-04-25 13:54:25 +0200 |
commit | 1f7615ad9db3e8f90b20aa9932c3c11ee1793218 (patch) | |
tree | e48b0e34af7e3a4b2366d3d7f94b2b421edd8c4e /lib/core | |
parent | a36d8bbf6c493e30df45bbb0a17fd793e7c1f12e (diff) | |
download | Nim-1f7615ad9db3e8f90b20aa9932c3c11ee1793218.tar.gz |
make seq.add more effective for --newruntime
Diffstat (limited to 'lib/core')
-rw-r--r-- | lib/core/seqs.nim | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim index 5483c0ebc..9c88040ba 100644 --- a/lib/core/seqs.nim +++ b/lib/core/seqs.nim @@ -153,6 +153,20 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) = for i in oldLen .. newLen-1: xu.p.data[i] = value +proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect.} = + ## Generic proc for adding a data item `y` to a container `x`. + ## + ## For containers that have an order, `add` means *append*. New generic + ## containers should also call their adding proc `add` for consistency. + ## Generic code becomes much easier to write if the Nim naming scheme is + ## respected. + let oldLen = x.len + var xu = cast[ptr NimSeqV2[T]](addr x) + if xu.p == nil or xu.p.cap < oldLen+1: + xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T))) + xu.len = oldLen+1 + xu.p.data[oldLen] = value + proc setLen[T](s: var seq[T], newlen: Natural) = {.noSideEffect.}: if newlen < s.len: |