mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
* fixes #15076 * heapqueue: optimized for ARC * added another test case [backport:1.4] * code cleanup
This commit is contained in:
21
tests/arc/tasyncleak4.nim
Normal file
21
tests/arc/tasyncleak4.nim
Normal file
@@ -0,0 +1,21 @@
|
||||
discard """
|
||||
cmd: "nim c --gc:orc -d:useMalloc $file"
|
||||
output: '''ok'''
|
||||
valgrind: "leaks"
|
||||
"""
|
||||
|
||||
# bug #15076
|
||||
import asyncdispatch
|
||||
|
||||
var futures: seq[Future[void]]
|
||||
|
||||
for i in 1..20:
|
||||
futures.add sleepAsync 1
|
||||
futures.add sleepAsync 1
|
||||
|
||||
futures.all.waitFor()
|
||||
futures.setLen 0
|
||||
|
||||
setGlobalDispatcher nil
|
||||
GC_fullCollect()
|
||||
echo "ok"
|
||||
74
tests/arc/tdestroy_in_loopcond.nim
Normal file
74
tests/arc/tdestroy_in_loopcond.nim
Normal file
@@ -0,0 +1,74 @@
|
||||
discard """
|
||||
output: '''400 true'''
|
||||
cmd: "nim c --gc:orc $file"
|
||||
"""
|
||||
|
||||
type HeapQueue*[T] = object
|
||||
data: seq[T]
|
||||
|
||||
|
||||
proc len*[T](heap: HeapQueue[T]): int {.inline.} =
|
||||
heap.data.len
|
||||
|
||||
proc `[]`*[T](heap: HeapQueue[T], i: Natural): T {.inline.} =
|
||||
heap.data[i]
|
||||
|
||||
proc push*[T](heap: var HeapQueue[T], item: T) =
|
||||
heap.data.add(item)
|
||||
|
||||
proc pop*[T](heap: var HeapQueue[T]): T =
|
||||
result = heap.data.pop
|
||||
|
||||
proc clear*[T](heap: var HeapQueue[T]) = heap.data.setLen 0
|
||||
|
||||
|
||||
type
|
||||
Future = ref object of RootObj
|
||||
s: string
|
||||
callme: proc()
|
||||
|
||||
var called = 0
|
||||
|
||||
proc consume(f: Future) =
|
||||
inc called
|
||||
|
||||
proc newFuture(s: string): Future =
|
||||
var r: Future
|
||||
r = Future(s: s, callme: proc() =
|
||||
consume r)
|
||||
result = r
|
||||
|
||||
var q: HeapQueue[tuple[finishAt: int64, fut: Future]]
|
||||
|
||||
proc sleep(f: int64): Future =
|
||||
q.push (finishAt: f, fut: newFuture("async-sleep"))
|
||||
|
||||
proc processTimers =
|
||||
# Pop the timers in the order in which they will expire (smaller `finishAt`).
|
||||
var count = q.len
|
||||
let t = high(int64)
|
||||
while count > 0 and t >= q[0].finishAt:
|
||||
q.pop().fut.callme()
|
||||
dec count
|
||||
|
||||
var futures: seq[Future]
|
||||
|
||||
proc main =
|
||||
for i in 1..200:
|
||||
futures.add sleep(56020904056300)
|
||||
futures.add sleep(56020804337500)
|
||||
#futures.add sleep(2.0)
|
||||
|
||||
#futures.add sleep(4.0)
|
||||
|
||||
processTimers()
|
||||
#q.pop()[1].callme()
|
||||
#q.pop()[1].callme()
|
||||
|
||||
futures.setLen 0
|
||||
|
||||
q.clear()
|
||||
|
||||
main()
|
||||
GC_fullCollect()
|
||||
echo called, " ", getOccupiedMem() < 160
|
||||
Reference in New Issue
Block a user