Skip to content

Commit 2bb5493

Browse files
committed
implement memof-call; add CI scripts
1 parent 3bee3c5 commit 2bb5493

File tree

5 files changed

+310
-110
lines changed

5 files changed

+310
-110
lines changed

.github/workflows/tests.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request: {}
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: wget
15+
run: mkdir ci-bin/ && wget -O ci-bin/cr_once http://repo.calcit-lang.org/binaries/linux/cr_once
16+
- name: "permission"
17+
run: chmod +x ci-bin/cr_once
18+
19+
- name: "prepare modules"
20+
run: >
21+
mkdir -p ~/.config/calcit/modules/
22+
&& cd ~/.config/calcit/modules/
23+
&& git clone https://github.com/calcit-lang/calcit-test
24+
&& git clone https://github.com/calcit-lang/lilac
25+
26+
- name: "test"
27+
run: env=ci ./ci-bin/cr_once

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
.compact-inc.cirru
3+
.calcit-error.cirru

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,74 @@
22
Memof
33
----
44

5-
> port of memof.
5+
> A memoization library in calcit, port from [Cumulo/memof](https://github.com/Cumulo/memof).
66
77
### Usages
88

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+
973
Install [calcit-runner](https://github.com/Cirru/calcit-runner.nim) to run demo:
1074

1175
```bash

0 commit comments

Comments
 (0)