nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.63k stars 1.47k forks source link

fixes #24402; Memory leak under Arc/Orc on inline iterators with nested seq #24419

Closed ringabout closed 1 week ago

ringabout commented 2 weeks ago

fixes #24402

iterator myPairsInline*[T](twoDarray: seq[seq[T]]): (int, seq[T]) {.inline.} =
  for indexValuePair in twoDarray.pairs:
    yield indexValuePair

proc innerTestTotalMem() =
  var my2dArray: seq[seq[int32]] = @[]

  # fill with some data...
  for i in 0'i32..100:
    var z = @[i, i+1]
    my2dArray.add z

  for oneDindex, innerArray in myPairsInline(my2dArray):
    discard

innerTestTotalMem()

In for oneDindex, innerArray in myPairsInline(my2dArray), oneDindex and innerArray becomes cursors because they satisfy the criterion of isSimpleIteratorVar. On the one hand, it is not correct to have them point to the temporary generated by tuple unpacking, which left the memory of the temporary uncleaned up. On the other hand, we don't need to generate a temporary for a symbol node when unpacking the tuple.

github-actions[bot] commented 1 week ago

Thanks for your hard work on this PR! The lines below are statistics of the Nim compiler built from 21420d8b0976dc034feb90ab2878ae0dd63121ae

Hint: mm: orc; opt: speed; options: -d:release 177497 lines; 8.663s; 653.258MiB peakmem