Skip to content

Instantly share code, notes, and snippets.

@Araq
Created July 13, 2018 22:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Araq/b0ccaf122406a561fec71e5051d8028b to your computer and use it in GitHub Desktop.
Save Araq/b0ccaf122406a561fec71e5051d8028b to your computer and use it in GitHub Desktop.

Basic idea: A Nim DSL that describes the game model declaratively

There is a raw list/sequence of the data for fast iteration over all elements. The implementation should be either based on an ordinary contiguous sequence or something like: http://plflib.org/colony.htm which is essentially a linked list of growing arrays so that no reallocations are required.

In addition to the contiguous storage there can be multiple indexes into the data. The indexes can be hash based or based on a BTree depending on whether ranges queries need to be supported. There is always a primary key of type integer that can be used to index the contiguous array.

An example:

table t:
  type Entry = object
    key: string
    val: int
  hashIndex key
  btreeIndex val

  with: del   shared: true count C: val > 10 lazyCount B: val > 1000

From this declaration all the procedural code is generated, accessors ensure the different views on the data are kept up-to-date etc. t supports lookups by key because there is a hashIndex on key. It supports range queries on val because there is a BTree index on val.

  1. Deletion in a hash table implementation usually requires "tombstones", an implementation can be faster if deletion is not supported at all, hence deletion has to be enabled via with: del.
  2. shared: true means it should use Nim's shared memory heap. All operations on the data then needs to take a lock. The implementation can use "lock stripes" or even atomic compare and swap loops to improve performance.
  3. count C: p counts the number of entries that fullfill the predicate p. C is the name of the counter.
  4. lazyCount does the same but the counter is only available after a full run iteration over t and invalidated on every update operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment