From 1f7615ad9db3e8f90b20aa9932c3c11ee1793218 Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 25 Apr 2019 13:54:25 +0200 Subject: make seq.add more effective for --newruntime --- lib/core/seqs.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/core') 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: -- cgit 1.4.1-2-gfad0