#16353: A cached_function with selective memory
-------------------------+-------------------------------------------------
Reporter: | Owner:
ncohen | Status: needs_review
Type: | Milestone: sage-6.3
enhancement | Resolution:
Priority: major | Merged in:
Component: | Reviewers:
performance | Work issues:
Keywords: | Commit:
Authors: | e06b947ab12f9f754363847c996b251f5682c125
Nathann Cohen | Stopgaps:
Report Upstream: N/A |
Branch: |
u/ncohen/16353 |
Dependencies: |
-------------------------+-------------------------------------------------
Comment (by vbraun):
Thats still crazy, decorators are a useful metaprogramming tool but you
are essentially creating two function bodies that don't / can't talk to
each other. If you really want to have a decorator to help with managing
the cache then make the decision to cache available inside the function
body. E.g.
{{{
#!python
@sometimes_cached_function
def bar(x, y, z):
w = do_long_computation(x, y, z)
if want_to_cache(x, y, z, w):
bar.set_cache((x, y, z), w)
# or some variation thereof that uses the original arguments
return w
}}}
Though really the advantage over
{{{
#!python
bar_cache = dict()
def bar(x, y, z):
try:
return bar_cache[(x,y,z)]
except KeyError:
w = do_long_computation(x, y, z)
if want_to_cache(x,y,z,w):
bar_cache[(x,y,z)] = w
return w
}}}
is pretty slim. You save maybe 2 lines at the expense of making it harder
to understand to somebody who doesn't know about the memoization
machinery.
--
Ticket URL: <http://trac.sagemath.org/ticket/16353#comment:11>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.