-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto memoization, "Share" button on wasm/online (#86)
* Auto memoization * fix, need to actually mutate to set cache key * add memoization to readme * Add fib(50) test * add test for fib(50) result * add ResetCache() for fair-er benchmarks * don't cache if max args is exceeded * use fib(35) as that seems to be the standard for some benchmarks * new object.Hashable() to exclude all non hashble types; ie array and maps in addition to functions * added test of what we want/expect for print vs log * memoziation includes print output * Add Share button and decode of c=
- Loading branch information
Showing
10 changed files
with
202 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package eval | ||
|
||
import ( | ||
"grol.io/grol/object" | ||
) | ||
|
||
const MaxArgs = 4 | ||
|
||
type CacheKey struct { | ||
Fn string | ||
Args [MaxArgs]object.Object | ||
} | ||
|
||
type CacheValue struct { | ||
Result object.Object | ||
Output []byte | ||
} | ||
|
||
type Cache map[CacheKey]CacheValue | ||
|
||
func NewCache() Cache { | ||
return make(Cache) | ||
} | ||
|
||
func (c Cache) Get(fn string, args []object.Object) (object.Object, []byte, bool) { | ||
if len(args) > MaxArgs { | ||
return nil, nil, false | ||
} | ||
key := CacheKey{Fn: fn} | ||
for i, v := range args { | ||
// Can't hash functions, arrays, maps arguments (yet). | ||
if !object.Hashable(v) { | ||
return nil, nil, false | ||
} | ||
key.Args[i] = v | ||
} | ||
result, ok := c[key] | ||
return result.Result, result.Output, ok | ||
} | ||
|
||
func (c Cache) Set(fn string, args []object.Object, result object.Object, output []byte) { | ||
if len(args) > MaxArgs { | ||
return | ||
} | ||
key := CacheKey{Fn: fn} | ||
for i, v := range args { | ||
// Can't hash functions arguments (yet). | ||
if !object.Hashable(v) { | ||
return | ||
} | ||
key.Args[i] = v | ||
} | ||
c[key] = CacheValue{Result: result, Output: output} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
fib = func(x) { | ||
if x <= 0 { | ||
return 0 | ||
} | ||
if x == 1 { | ||
return 1 | ||
} | ||
fib(x - 1) + fib(x - 2) | ||
} | ||
r = fib(35) | ||
log("fib(35) =", r) | ||
r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters