diff options
author | cooldome <cdome@bk.ru> | 2019-04-11 22:09:11 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-04-11 23:09:11 +0200 |
commit | 041d15392aaf732665abab290f0cf5993d909efc (patch) | |
tree | 4a4aff6b33707850f7e38e680652bf00e6c5c0df /tests | |
parent | de02fd0b898e41fa91087300d82573d83e357b34 (diff) | |
download | Nim-041d15392aaf732665abab290f0cf5993d909efc.tar.gz |
Compiler plugin for implementing incremental computation in user space (#10819)
This plugin provides essential building block for implementing incremental computations in your programs. The idea behind incremental computations is that if you do the same calculation multiple times but with slightly different inputs you don't have to recompute everything from scratch. Also you don't want to adopt special algorithms either, you would like to write your code in standard from scratch manner and get incrementality for free when it is possible. The plugin computes the digest of the proc bodies, recursively hashing all called procs as well . Such digest with the digest of the argument values gives a good "name" for the result. Terminology loosely follows paper "Incremental Computation with Names" link below. It works well if you have no side effects in your computations. If you have global state in your computations then you will need problem specific workarounds to represent global state in set of "names" . SideEffect tracking in Nim also useful in this topic. Classical examples: Dashboard with ticking data. New data arrives non stop and you would like to update the dashboard recomputing only changed outputs. Excel spreadsheet where user changes one cell and you would like to recompute all cells that are affected by the change, but do not want to recompute every cell in the spreadsheet.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/macros/tincremental.nim | 150 | ||||
-rw-r--r-- | tests/vm/tfile_rw.nim | 27 | ||||
-rw-r--r-- | tests/vm/tsignaturehash.nim | 20 |
3 files changed, 197 insertions, 0 deletions
diff --git a/tests/macros/tincremental.nim b/tests/macros/tincremental.nim new file mode 100644 index 000000000..401d6f3f8 --- /dev/null +++ b/tests/macros/tincremental.nim @@ -0,0 +1,150 @@ +discard """ + output: '''heavy_calc_impl is called +sub_calc1_impl is called +sub_calc2_impl is called +** no changes recompute effectively +** change one input and recompute effectively +heavy_calc_impl is called +sub_calc2_impl is called''' +""" + +# sample incremental + +import tables +import macros + +var inputs = initTable[string, float]() +var cache = initTable[string, float]() +var dep_tree {.compileTime.} = initTable[string, string]() + +macro symHash(s: typed{nkSym}): string = + result = newStrLitNode(symBodyHash(s)) + +####################################################################################### + +template graph_node(key: string) {.pragma.} + +proc tag(n: NimNode): NimNode = + ## returns graph node unique name of a function or nil if it is not a graph node + expectKind(n, {nnkProcDef, nnkFuncDef}) + for p in n.pragma: + if p.len > 0 and p[0] == bindSym"graph_node": + return p[1] + return nil + +macro graph_node_key(n: typed{nkSym}): untyped = + result = newStrLitNode(n.symBodyHash) + +macro graph_discovery(n: typed{nkSym}): untyped = + # discovers graph dependency tree and updated dep_tree global var + let mytag = newStrLitNode(n.symBodyHash) + var visited: seq[NimNode] + proc discover(n: NimNode) = + case n.kind: + of nnkNone..pred(nnkSym), succ(nnkSym)..nnkNilLit: discard + of nnkSym: + if n.symKind in {nskFunc, nskProc}: + if n notin visited: + visited.add n + let tag = n.getImpl.tag + if tag != nil: + dep_tree[tag.strVal] = mytag.strVal + else: + discover(n.getImpl.body) + else: + for child in n: + discover(child) + discover(n.getImpl.body) + result = newEmptyNode() + +####################################################################################### + +macro incremental_input(key: static[string], n: untyped{nkFuncDef}): untyped = + # mark leaf nodes of the graph + template getInput(key) {.dirty.} = + {.noSideEffect.}: + inputs[key] + result = n + result.pragma = nnkPragma.newTree(nnkCall.newTree(bindSym"graph_node", newStrLitNode(key))) + result.body = getAst(getInput(key)) + +macro incremental(n: untyped{nkFuncDef}): untyped = + ## incrementalize side effect free computation + ## wraps function into caching layer, mark caching function as a graph_node + ## injects dependency discovery between graph nodes + template cache_func_body(func_name, func_name_str, func_call) {.dirty.} = + {.noSideEffect.}: + graph_discovery(func_name) + let key = graph_node_key(func_name) + if key in cache: + result = cache[key] + else: + echo func_name_str & " is called" + result = func_call + cache[key] = result + + let func_name = n.name.strVal & "_impl" + let func_call = nnkCall.newTree(ident func_name) + for i in 1..<n.params.len: + func_call.add n.params[i][0] + let cache_func = n.copyNimTree + cache_func.body = getAst(cache_func_body(ident func_name, func_name, func_call)) + cache_func.pragma = nnkPragma.newTree(newCall(bindSym"graph_node", + newCall(bindSym"symHash", ident func_name))) + + n.name = ident(func_name) + result = nnkStmtList.newTree(n, cache_func) + +########################################################################### +### Example +########################################################################### + +func input1(): float {.incremental_input("a1").} + +func input2(): float {.incremental_input("a2").} + +func sub_calc1(a: float): float {.incremental.} = + a + input1() + +func sub_calc2(b: float): float {.incremental.} = + b + input2() + +func heavy_calc(a: float, b: float): float {.incremental.} = + sub_calc1(a) + sub_calc2(b) + +########################################################################### +## graph finalize and inputs +########################################################################### + +macro finalize_dep_tree(): untyped = + result = nnkTableConstr.newNimNode + for key, val in dep_tree: + result.add nnkExprColonExpr.newTree(newStrLitNode key, newStrLitNode val) + result = nnkCall.newTree(bindSym"toTable", result) + +const dep_tree_final = finalize_dep_tree() + +proc set_input(key: string, val: float) = + ## set input value + ## all affected nodes of graph are invalidated + inputs[key] = val + var k = key + while k != "": + k = dep_tree_final.getOrDefault(k , "") + cache.del(k) + +########################################################################### +## demo +########################################################################### + +set_input("a1", 5) +set_input("a2", 2) +discard heavy_calc(5.0, 10.0) + +echo "** no changes recompute effectively" +discard heavy_calc(5.0, 10.0) + +echo "** change one input and recompute effectively" + +set_input("a2", 10) +discard heavy_calc(5.0, 10.0) diff --git a/tests/vm/tfile_rw.nim b/tests/vm/tfile_rw.nim new file mode 100644 index 000000000..8d7a2ca95 --- /dev/null +++ b/tests/vm/tfile_rw.nim @@ -0,0 +1,27 @@ +discard """ + output: '''ok''' +""" + +# test file read write in vm + +import os, strutils + +const filename = splitFile(currentSourcePath).dir / "tfile_rw.txt" + +const mytext = "line1\nline2\nline3" +static: + writeFile(filename, mytext) +const myfile_str = staticRead(filename) +const myfile_str2 = readFile(filename) +const myfile_str_seq = readLines(filename, 3) + +static: + doAssert myfile_str == mytext + doAssert myfile_str2 == mytext + doAssert myfile_str_seq[0] == "line1" + doAssert myfile_str_seq[1] == "line2" + doAssert myfile_str_seq.join("\n") == mytext + + +removeFile(filename) +echo "ok" \ No newline at end of file diff --git a/tests/vm/tsignaturehash.nim b/tests/vm/tsignaturehash.nim new file mode 100644 index 000000000..42e0a1571 --- /dev/null +++ b/tests/vm/tsignaturehash.nim @@ -0,0 +1,20 @@ +# test sym digest is computable at compile time + +import macros, algorithm +import md5 + +macro testmacro(s: typed{nkSym}): string = + let s = getMD5(signaturehash(s) & " - " & symBodyHash(s)) + result = newStrLitNode(s) + +macro testmacro(s: typed{nkOpenSymChoice|nkClosedSymChoice}): string = + var str = "" + for sym in s: + str &= symBodyHash(sym) + result = newStrLitNode(getMD5(str)) + +# something recursive and/or generic +discard testmacro(testmacro) +discard testmacro(`[]`) +discard testmacro(binarySearch) +discard testmacro(sort) |