t24402.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. joinable: false
  3. """
  4. # bug #24402
  5. iterator myPairsInline*[T](twoDarray: seq[seq[T]]): (int, seq[T]) {.inline.} =
  6. for indexValuePair in twoDarray.pairs:
  7. yield indexValuePair
  8. iterator myPairsClosure*[T](twoDarray: seq[seq[T]]): (int, seq[T]) {.closure.} =
  9. for indexValuePair in twoDarray.pairs:
  10. yield indexValuePair
  11. template testTotalMem(iter: untyped): int =
  12. proc innerTestTotalMem(): int {.gensym.} =
  13. result = 0
  14. # do the same operation 100 times, which should have similar mem footprint
  15. # as doing it once.
  16. for iterNum in 0..100:
  17. result = max(result, getTotalMem()) # record current mem footprint
  18. # initialize nested sequence
  19. var my2dArray: seq[seq[int32]] = @[]
  20. # fill with some data...
  21. for i in 0'i32..10_000:
  22. var z = @[i, i+1]
  23. my2dArray.add z
  24. # use that data somehow...
  25. var otherContainer: seq[int32] = @[]
  26. var count = 0'i32
  27. for oneDindex, innerArray in my2dArray.iter:
  28. for value in innerArray:
  29. inc count
  30. if oneDindex > 50 and value < 200:
  31. otherContainer.add count
  32. innerTestTotalMem()
  33. proc main =
  34. let closureMem = testTotalMem(myPairsClosure) #1052672
  35. let inlineMem = testTotalMem(myPairsInline) #20328448
  36. when defined(echoFootprint):
  37. echo "Closure memory footprint: " & $closureMem
  38. echo "Inline memory footprint: " & $inlineMem
  39. # check that mem footprint is relatively similar b/t each method
  40. doAssert (closureMem - inlineMem).abs < (closureMem div 10)
  41. main()