|
2 | 2 | Memof |
3 | 3 | ---- |
4 | 4 |
|
5 | | -> port of memof. |
| 5 | +> A memoization library in calcit, port from [Cumulo/memof](https://github.com/Cumulo/memof). |
6 | 6 |
|
7 | 7 | ### Usages |
8 | 8 |
|
| 9 | +The model behind memof is memoizationm but with cache invalidations. `new-loop!` is the API to tell memof new loop happened. If stored record is old enough, it will be removed. |
| 10 | + |
| 11 | +```cirru |
| 12 | +(ns demo.main (:require ([] memof.core :as memof))) |
| 13 | +
|
| 14 | +; pass GC options |
| 15 | +defatom *states $ memof.core/new-caches ({}) |
| 16 | +
|
| 17 | +defn f1 (x y) (* x y) |
| 18 | +
|
| 19 | +memof.core/write-record! *states f1 ([] 1 2) 3 |
| 20 | +
|
| 21 | +memof.core/access-record *states f1 ([] 1 2) |
| 22 | +; returns 3 |
| 23 | +
|
| 24 | +memof.core/new-loop! *states |
| 25 | +; when loop is large enough, it will trigger GC |
| 26 | +``` |
| 27 | + |
| 28 | +A short hand for using it: |
| 29 | + |
| 30 | +```cirru |
| 31 | +memof.alias/memof-call f ([] 1 2) 3 |
| 32 | +
|
| 33 | +; handle this at first on reload! |
| 34 | +memof.alias/reset-memof-caches! |
| 35 | +``` |
| 36 | + |
| 37 | +States structure: |
| 38 | + |
| 39 | +```edn |
| 40 | +{ |
| 41 | + :loop 0 ; counter |
| 42 | + :entries { ; where entries are stored |
| 43 | + f1 { |
| 44 | + :hit-times 1, :missed-times 1 |
| 45 | + :records { |
| 46 | + [p1 p2] {:value 1, :hit-times 1, :last-hit-loop 1, :initial-loop 1} |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + :gc { ; configurations |
| 51 | + :trigger-loop 100, ; trigger GC every N loops |
| 52 | + :elapse-loop 50 ; entries are considered unuseful after not used for N loops |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Methods: |
| 58 | + |
| 59 | +* `(new-states)` creates states holding all entries |
| 60 | +* `(show-summary @*states)` list entries after formatted |
| 61 | +* `(write-record! *states f params value)` write to cache, `params` supposed to be a collection |
| 62 | +* `(access-record *states f params)` access and return value(or `nil`) |
| 63 | +* `(new-loop! *states)` loop and trigger actions |
| 64 | +* `(perform-gc! *states)` remove entries that are probably no longer useful |
| 65 | +* `(reset-entries! *states)` clear entries |
| 66 | +* `(modify-gc-options! *states ({}))` modify GC options |
| 67 | + |
| 68 | +Set `memofVerbose=true` in environment to enable verbose mode to print debug logs. |
| 69 | +Alternatively, you can access and overwrite `memof.core/*verbose?` to enabled it. |
| 70 | + |
| 71 | +### Develop |
| 72 | + |
9 | 73 | Install [calcit-runner](https://github.com/Cirru/calcit-runner.nim) to run demo: |
10 | 74 |
|
11 | 75 | ```bash |
|
0 commit comments