-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bc::memoize: support caching more than one result at a time #23
Comments
Thanks for the feedback! You may find it interesting to dig into how
That's right! When the memoized function is invoked cold the function is re-defined, and the backing call's output is cached directly in the function body! If that doesn't disgust you I suggest seeking professional help 😉 I implemented this somewhat on a whim while trying to think about how I could avoid
Could you elaborate on your constraints, just so I understand them? I certainly appreciate that I/O overhead is undesirable, but maybe it's not as bad as you think. In particular, if you're able to point bash-cache at a
Yes and no. It's certainly possible to have an in-memory multi-keyed cache, (e.g. via an associative array or possibly by extending the dynamic-redeclaration pattern in A significant issue (which I actually need to call out expressly in the documentation) is that data cached in-process can't escape its subshell. So if you call a in-process-cached function in a command substitution or other subshell nothing "actually" gets cached. I'm not aware of any (reasonable) way to propagate data back to a parent shell without writing to the file system. An additional concern is the loss of fidelity. Bash does a surprisingly poor job of handling data losslessly (see There are probably additional reasons I'd remember if I started redesigning |
Thanks for the detailed reply!
I may need pro help, I don't find it disgusting. In other languages where static function variables exist, a first approximation memoization implementation would also store the function output in the function itself:
Using cache files just feels dirty and avoiding them whenever possible is optimal in my book; they need careful design to avoid race conditions, they need to be cleaned up, they increase the attackable surface area of a program, and so on; you may already have correctly guessed I'm no fan of python's
I see; enlightening! Files make sense in that context, I can't figure out a more elegant way to transport variables back to a parent process. One way I can get around the current “one result” limitation of the current
|
Yeah but that's not redefining the function itself on every cold invocation :)
Tell me about it....
I had actually meant to suggest currying for memoized functions with a finite set of inputs, thanks for calling that out. We definitely could consider something like that in a generalized solution, but if we did so under the covers the behavior might be surprising since the curried function names would presumably be lossy, and we'd need to come up with an invalidation scheme. I'd also need to investigate exactly how much memory overhead this pattern incurs; one benefit(?) of |
Hi,
I was a bit saddened when I realized that
bc::memoize
would only keep the freshest invocation in memory:It appears
bc::cache
supports the above usage pattern, but OTOH polluting the filesystem with files (and I/O) is not very appealing for my use case.I'm curious, is there some fundamental reason that would make supporting the above usage pattern in
bc::memoize
a headache?Thanks for your library, it really fills one of the many bash gaps — I can almost do away with function static variables thanks to you :)
The text was updated successfully, but these errors were encountered: