summary refs log tree commit diff stats
path: root/compiler/lambdalifting.nim
Commit message (Expand)AuthorAgeFilesLines
* ARC: implemented a simple cycle detectorAraq2019-11-281-4/+6
* Cosmetic compiler cleanup (#12718)Clyybber2019-11-281-41/+40
* ARC: closure bugfixes (#12677)Andreas Rumpf2019-11-181-2/+9
* ARC: fixes leaking new() statement (#12665)Andreas Rumpf2019-11-151-6/+9
* ARC: solves phase ordering problems (#12654)Andreas Rumpf2019-11-141-7/+4
* ARC: use the new .cursor annotation for 'up' pointersAraq2019-11-131-2/+6
* fixes #12577 [backport] (#12584)Andreas Rumpf2019-11-031-8/+4
* --gc:destructors: simple closures workAndreas Rumpf2019-11-021-9/+13
* --gc:destructors now means Nim uses pure refcounting (#12557)Andreas Rumpf2019-10-301-1/+2
* proof that refcounting can handle Nim's async (#12533)Andreas Rumpf2019-10-281-18/+18
* some progress on bug #12443Andreas Rumpf2019-10-271-1/+1
* refactoring: use the new strings and seqs when optSeqDestructors is activeAraq2019-10-201-3/+3
* refactoring: --newruntime consists of 3 different switchesAraq2019-10-201-10/+10
* fixes #12323 [backport]Andreas Rumpf2019-10-071-1/+3
* int128 on firstOrd, lastOrd and lengthOrd (#11701)Arne Döring2019-08-071-1/+1
* [refactoring] remove unused imports in the compiler and in some stdlib modulesAraq2019-07-181-1/+1
* newruntime: progress to make 'async' work with --newruntimeAraq2019-07-151-1/+1
* styleCheck: make the compiler and large parts of the stdlib compatible with -...Araq2019-07-101-4/+4
* fixes #11523Andreas Rumpf2019-06-261-1/+2
* [bugfix] newruntime: trivial closure iterator worksAraq2019-06-201-0/+7
* [bugfix] owned closures (#11544)Andreas Rumpf2019-06-201-15/+47
* [refactoring] liftdestructors.nim is callable from lambdalifting; refs #11217Araq2019-06-121-1/+6
* make fullpaths the default in error messages and stack traces for mor… (#11...Andreas Rumpf2019-06-051-3/+1
* make twidgets example green (#11135)Andreas Rumpf2019-04-281-19/+27
* make strscans module work with --newruntimeAndreas Rumpf2019-04-141-3/+6
* ignore nkCommentStmt in lambda liftingAndreas Rumpf2019-02-061-1/+1
* fixes silly typo causing tons of async regressionsAraq2019-01-301-1/+1
* destructors: do not produce strong backrefs in closure environments so that r...Araq2019-01-301-4/+17
* fixes nested gensym'ed parameters; fixes #9476Araq2018-12-081-4/+1
* fixes #9441Araq2018-12-061-2/+20
* lambdalifting: be consistent in its handling for 'func'Araq2018-12-051-9/+7
* Change the order of compilation passes, transformation is made lazy at code g...cooldome2018-10-181-6/+11
* improve error message, closes #2361 (#9387)Miran2018-10-171-2/+3
* Access implicit `result` trough envP in closures (#8471)LemonBoy2018-07-301-1/+13
* fixes yet another merge conflictAraq2018-06-111-86/+2
|\
| * Removed oldIterTranf featureYuriy Glukhov2018-06-101-86/+2
* | fixex merge conflictsAraq2018-06-081-21/+41
|\|
| * Merge branch 'devel' into yield-in-tryYuriy Glukhov2018-05-151-102/+101
| |\
| * | CosmeticsYuriy Glukhov2018-05-091-2/+2
| * | Small fix and cosmeticsYuriy Glukhov2018-05-091-2/+1
| * | Closure iter transformationYuriy Glukhov2018-05-091-18/+42
* | | baby steps for incremental compilationAndreas Rumpf2018-05-301-1/+1
* | | refactoring: remove idents.legacy global variable and pass the IdentCache aro...Andreas Rumpf2018-05-271-20/+20
* | | remove more global variables in the Nim compilerAndreas Rumpf2018-05-271-1/+1
* | | make tests green againAndreas Rumpf2018-05-181-1/+1
* | | remove ast.emptyNode global; cleanup configuration.nimAraq2018-05-161-2/+2
| |/ |/|
* | options.nim: no global variables anymoreAndreas Rumpf2018-05-131-13/+10
* | lambda lifting compiles againAndreas Rumpf2018-05-121-85/+90
|/
* Remove PHP backend. (#7606)treeform2018-04-181-2/+2
* Fix some usages of typedesc in async procsZahary Karadjov2018-04-131-1/+2
hoang/Nim/commit/doc/gc.rst?h=devel&id=8aecacc1dfd10cfbaf30964e2eb78dd672623d31'>8aecacc1d ^
4aba7421f ^




0b44d812f ^
4aba7421f ^





d63e8e5d1 ^
0b44d812f ^
4aba7421f ^


afd8ca2f1 ^
4aba7421f ^



7e7c514df ^

0b44d812f ^
7e7c514df ^



4aba7421f ^
7956737ef ^



3ea644690 ^
7956737ef ^











915cc86f5 ^
















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                          
                       

                          


                      
  
 
 
                                                   
 
 


            

                                                               
 
                                                                          
                                                                            



                                                                           

 
                                                                              

                                                      






                                                                             
                                                                             
                                                                     




                
                                                                       
                                                                         
                                                                         
 
                   
                                         
                                                              
 
                                                                       
 
                                                              
 


                                                                              
                                                                          
                                                                              
                                                                          




                                                                          

                                                                      






                                                                               



                                                                         

                                                                             




                
                                                                          





                                                                            
                                                                           
                                  


                                                                             
                                                                             



                                

               
                                                                        



                                                                            
 



                       
                                                                        











                                                                              
















                                                                                                 
==========================
Nim's Garbage Collector
==========================

:Author: Andreas Rumpf
:Version: |nimversion|

..


  "The road to hell is paved with good intentions."


Introduction
============

This document describes how the GC works and how to tune it for
(soft) `realtime systems`:idx:.

The basic algorithm is *Deferred Reference Counting* with cycle detection.
References on the stack are not counted for better performance (and easier C
code generation). Cycle detection is currently done by a simple mark&sweep
GC that has to scan the full (thread local heap). ``--gc:v2`` replaces this
with an incremental mark and sweep. That it is not production ready yet,
however.


The GC is only triggered in a memory allocation operation. It is not triggered
by some timer and does not run in a background thread.

To force a full collection call ``GC_fullCollect``. Note that it is generally
better to let the GC do its work and not enforce a full collection.


Cycle collector
===============

The cycle collector can be en-/disabled independently from the other parts of
the GC with ``GC_enableMarkAndSweep`` and ``GC_disableMarkAndSweep``.


Realtime support
================

To enable realtime support, the symbol `useRealtimeGC`:idx: needs to be
defined via ``--define:useRealtimeGC`` (you can put this into your config
file as well). With this switch the GC supports the following operations:

.. code-block:: nim
  proc GC_setMaxPause*(maxPauseInUs: int)
  proc GC_step*(us: int, strongAdvice = false, stackSize = -1)

The unit of the parameters ``maxPauseInUs`` and ``us`` is microseconds.

These two procs are the two modus operandi of the realtime GC:

(1) GC_SetMaxPause Mode

    You can call ``GC_SetMaxPause`` at program startup and then each triggered
    GC run tries to not take longer than ``maxPause`` time. However, it is
    possible (and common) that the work is nevertheless not evenly distributed
    as each call to ``new`` can trigger the GC and thus take  ``maxPause``
    time.

(2) GC_step Mode

    This allows the GC to perform some work for up to ``us`` time. This is
    useful to call in a main loop to ensure the GC can do its work. To
    bind all GC activity to a ``GC_step`` call, deactivate the GC with
    ``GC_disable`` at program startup. If ``strongAdvice`` is set to ``true``,
    GC will be forced to perform collection cycle. Otherwise, GC may decide not
    to do anything, if there is not much garbage to collect.
    You may also specify the current stack size via ``stackSize`` parameter.
    It can improve performance, when you know that there are no unique Nim
    references below certain point on the stack. Make sure the size you specify
    is greater than the potential worst case size.

These procs provide a "best effort" realtime guarantee; in particular the
cycle collector is not aware of deadlines yet. Deactivate it to get more
predictable realtime behaviour. Tests show that a 2ms max pause
time will be met in almost all cases on modern CPUs (with the cycle collector
disabled).


Time measurement
----------------

The GC's way of measuring time uses (see ``lib/system/timers.nim`` for the
implementation):

1) ``QueryPerformanceCounter`` and ``QueryPerformanceFrequency`` on Windows.
2) ``mach_absolute_time`` on Mac OS X.
3) ``gettimeofday`` on Posix systems.

As such it supports a resolution of nanoseconds internally; however the API
uses microseconds for convenience.


Define the symbol ``reportMissedDeadlines`` to make the GC output whenever it
missed a deadline. The reporting will be enhanced and supported by the API in
later versions of the collector.


Tweaking the GC
---------------

The collector checks whether there is still time left for its work after
every ``workPackage``'th iteration. This is currently set to 100 which means
that up to 100 objects are traversed and freed before it checks again. Thus
``workPackage`` affects the timing granularity and may need to be tweaked in
highly specialized environments or for older hardware.


Keeping track of memory
-----------------------

If you need to pass around memory allocated by Nim to C, you can use the
procs ``GC_ref`` and ``GC_unref`` to mark objects as referenced to avoid them
being freed by the GC. Other useful procs from `system <system.html>`_ you can
use to keep track of memory are:

* getTotalMem(): returns the amount of total memory managed by the GC.
* getOccupiedMem(): bytes reserved by the GC and used by objects.
* getFreeMem(): bytes reserved by the GC and not in use.

In addition to ``GC_ref`` and ``GC_unref`` you can avoid the GC by manually
allocating memory with procs like ``alloc``, ``allocShared``, or
``allocCStringArray``. The GC won't try to free them, you need to call their
respective *dealloc* pairs when you are done with them or they will leak.


Heap dump
=========

The heap dump feature is still in its infancy, but it already proved
useful for us, so it might be useful for you. To get a heap dump, compile
with ``-d:nimTypeNames`` and call ``dumpNumberOfInstances`` at a strategic place in your program.
This produces a list of used types in your program and for every type
the total amount of object instances for this type as well as the total
amount of bytes these instances take up. This list is currently unsorted!
You need to use external shell script hacking to sort it.

The numbers count the number of objects in all GC heaps, they refer to
all running threads, not only to the current thread. (The current thread
would be the thread that calls ``dumpNumberOfInstances``.) This might
change in later versions.