acyclic pragma

The acyclic pragma can be used for object types to mark them as acyclic even though they seem to be cyclic. This is an optimization for the garbage collector to not consider objects of this type as part of a cycle:

  1. type
  2. Node = ref NodeObj
  3. NodeObj {.acyclic.} = object
  4. left, right: Node
  5. data: string

Or if we directly use a ref object:

  1. type
  2. Node {.acyclic.} = ref object
  3. left, right: Node
  4. data: string

In the example a tree structure is declared with the Node type. Note that the type definition is recursive and the GC has to assume that objects of this type may form a cyclic graph. The acyclic pragma passes the information that this cannot happen to the GC. If the programmer uses the acyclic pragma for data types that are in reality cyclic, the memory leaks can be the result, but memory safety is preserved.