summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-01-18 11:11:16 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-01-18 11:11:16 +0100
commit6f6551d77dba7d80e3287243fa1b4048e8e67ccd (patch)
tree7aa107b0ec8dbf73e0aacf7581a7a8012d1ab47a
parent03b7df74a9f50d619c8293beb68efa878e327393 (diff)
downloadNim-6f6551d77dba7d80e3287243fa1b4048e8e67ccd.tar.gz
leak detector: sort type based entries by total used bytes
-rw-r--r--lib/system/gc_common.nim30
1 files changed, 28 insertions, 2 deletions
diff --git a/lib/system/gc_common.nim b/lib/system/gc_common.nim
index ad37df0e0..d33e271ce 100644
--- a/lib/system/gc_common.nim
+++ b/lib/system/gc_common.nim
@@ -18,12 +18,38 @@ proc protect*(x: pointer): ForeignCell =
   result.owner = addr(gch)
 
 when defined(nimTypeNames):
+  type InstancesInfo = array[400, (cstring, int, int)]
+  proc sortInstances(a: var InstancesInfo; n: int) =
+    # we use shellsort here; fast and simple
+    var h = 1
+    while true:
+      h = 3 * h + 1
+      if h > n: break
+    while true:
+      h = h div 3
+      for i in countup(h, n - 1):
+        var v = a[i]
+        var j = i
+        while a[j - h][2] < v[2]:
+          a[j] = a[j - h]
+          j = j - h
+          if j < h: break
+        a[j] = v
+      if h == 1: break
+
   proc dumpNumberOfInstances* =
+    var a: InstancesInfo
+    var n = 0
     var it = nimTypeRoot
     while it != nil:
-      if it.instances > 0:
-        c_fprintf(stdout, "[Heap] %s: #%ld; bytes: %ld\n", it.name, it.instances, it.sizes)
+      if it.instances > 0 and n < a.len:
+        a[n] = (it.name, it.instances, it.sizes)
+        inc n
       it = it.nextType
+    sortInstances(a, n)
+    for i in 0 .. n-1:
+      c_fprintf(stdout, "[Heap] %s: #%ld; bytes: %ld\n", a[i][0], a[i][1], a[i][2])
+
 
   when defined(nimGcRefLeak):
     proc oomhandler() =