use system.move instead of system.shallowCopy if the GC mode requires it

This commit is contained in:
Andreas Rumpf 2019-10-03 16:46:09 +02:00
commit 60d64d1aef
14 changed files with 61 additions and 36 deletions

View file

@ -259,9 +259,11 @@ proc expandIfNeeded[T](deq: var Deque[T]) =
var cap = deq.mask + 1
if unlikely(deq.count >= cap):
var n = newSeq[T](cap * 2)
for i, x in pairs(deq): # don't use copyMem because the GC and because it's slower.
shallowCopy(n[i], x)
shallowCopy(deq.data, n)
var i = 0
for x in mitems(deq): # don't use copyMem because of the GC and because it's slower.
n[i] = move(x)
inc i
deq.data = move(n)
deq.mask = cap * 2 - 1
deq.tail = deq.count
deq.head = 0

View file

@ -417,7 +417,10 @@ proc keepIf*[T](s: var seq[T], pred: proc(x: T): bool {.closure.})
for i in 0 ..< len(s):
if pred(s[i]):
if pos != i:
shallowCopy(s[pos], s[i])
when defined(gcDestructors):
s[pos] = move(s[i])
else:
shallowCopy(s[pos], s[i])
inc(pos)
setLen(s, pos)
@ -436,7 +439,10 @@ proc delete*[T](s: var seq[T]; first, last: Natural) =
var j = min(len(s), last+1)
var newLen = len(s)-j+i
while i < newLen:
s[i].shallowCopy(s[j])
when defined(gcDestructors):
s[i] = move(s[j])
else:
s[i].shallowCopy(s[j])
inc(i)
inc(j)
setLen(s, newLen)
@ -461,7 +467,10 @@ proc insert*[T](dest: var seq[T], src: openArray[T], pos = 0) =
# Move items after `pos` to the end of the sequence.
while j >= pos:
dest[i].shallowCopy(dest[j])
when defined(gcDestructors):
dest[i] = move(dest[j])
else:
dest[i].shallowCopy(dest[j])
dec(i)
dec(j)
# Insert items from `dest` into `dest` at `pos`
@ -519,7 +528,10 @@ template keepItIf*(varSeq: seq, pred: untyped) =
let it {.inject.} = varSeq[i]
if pred:
if pos != i:
shallowCopy(varSeq[pos], varSeq[i])
when defined(gcDestructors):
varSeq[pos] = move(varSeq[i])
else:
shallowCopy(varSeq[pos], varSeq[i])
inc(pos)
setLen(varSeq, pos)

View file

@ -92,7 +92,7 @@ proc exclImpl[A](s: var HashSet[A], key: A): bool {.inline.} =
if isEmpty(s.data[i].hcode): # end of collision cluster; So all done
return
r = s.data[i].hcode and msk # "home" location of key@i
shallowCopy(s.data[j], s.data[i]) # data[i] will be marked EMPTY next loop
s.data[j] = move(s.data[i]) # data[i] will be marked EMPTY next loop
template dollarImpl() {.dirty.} =
result = "{"

View file

@ -99,7 +99,7 @@ template delImplIdx(t, i) =
when defined(js):
t.data[j] = t.data[i]
else:
shallowCopy(t.data[j], t.data[i]) # data[j] will be marked EMPTY next loop
t.data[j] = move(t.data[i]) # data[j] will be marked EMPTY next loop
template delImpl() {.dirty.} =
var hc: Hash

View file

@ -540,7 +540,7 @@ proc take*[A, B](t: var Table[A, B], key: A, val: var B): bool =
var index = rawGet(t, key, hc)
result = index >= 0
if result:
shallowCopy(val, t.data[index].val)
val = move(t.data[index].val)
delImplIdx(t, index)
proc clear*[A, B](t: var Table[A, B]) =