I was wondering if slices are still copied when used as proc parameters...
Found again Araqs comment
https://forum.nim-lang.org/t/4582#28715
So my guess was that they are still copied. But maybe overhead is small? So did this test:
# https://github.com/nim-lang/Nim/blob/devel/lib/system/iterators.nim#L1
# https://forum.nim-lang.org/t/4582#28715
iterator span*[T](a: openArray[T]; j, k: Natural): T {.inline.} =
assert k < a.len
var i: int = j
while i <= k:
yield a[i]
inc(i)
type
O = object
i: int
proc main =
var
s = newSeq[O](1000000)
for i in 0 .. (1000000 - 1):
s[i] = O(i: i)
var sum = 0
for x in s[1 .. ^1]:
#for x in s.span(1, s.high):
sum += x.i
echo sum
main()
Using span iterator reduces total running time by a factor of two. (And I think use cases are not that rare, I have a few where iteration has to skip first or last element.) Question: Will copying of Slices vanish in the future, or are span iterators useful. (At least they are less ugly than a while loop.)
I guess the sink and lent coming with destructors could help as well to return openarrays in some conservative situations but making openarrays first-class would be the ideal solution in my opinion.