cache option - Maple Help

cache Option

 Calling Sequence option cache   or   options cache option cache(n)   or   options cache(n)

Parameters

 n - integer: the size of the cache table

Description

 • A procedure with option cache automatically uses a cache table as a remember table.
 • Using a cache table as a remember table is similar to using a standard remember table.  At the end of each invocation of the procedure, an entry is added to the cache table.  When the procedure is called, this table is checked to see if the result already exists in the table.  If it does the result is returned without evaluating the function.  For more information on remember tables, see remember.
 The difference between a cache table and a standard remember table is that cache tables have a maximum size.  As new elements are added old ones may be removed.  Therefore, the cache table does not grow continuously as values are added.
 • A cache remember table is an implementation of a Least Recently Used (LRU) cache.
 • Permanent entries can be inserted into a cache table by using the Cache:-AddPermanent command.  This allows cache remember tables to still be used for base cases in recursive functions and for storing commonly used values.
 • When using option cache(n), the cache table can store at least n elements.  This is the same as passing n into the Cache command.

Details

 • Cache remember tables attempt to solve the problems of standard remember tables and option system remember tables.  The advantage over standard remember tables is that a cache table has a maximum size.  This means that a cache table does not act as a memory trap, storing a large number of values that cannot be reclaimed by garbage collection.  As cache tables allow permanent elements to be added, they can be used in procedures that cannot use option system remember tables.
 • The Cache command can also be used to create a procedure with a cache remember table, but without option cache.  This behaves much like a procedure with a remember table, but no option remember.  The table is checked when the procedure is invoked, but values are not automatically added to the table when the procedure returns.
 • Option cache is incompatible with option remember and option system.  An error occurs if they are used together.

Examples

 > cacheFunc := proc() option cache; print(args); args; end proc;
 ${\mathrm{cacheFunc}}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{cache}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({\mathrm{args}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{args}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $\mathrm{cacheFunc}\left(1\right)$
 ${1}$
 ${1}$ (2)
 > $\mathrm{cacheFunc}\left(2\right)$
 ${2}$
 ${2}$ (3)
 > $\mathrm{cacheFunc}\left(1\right)$
 ${1}$ (4)
 > $\mathrm{cacheFunc}\left(2\right)$
 ${2}$ (5)
 > $\mathrm{cacheFunc}\left(3\right)≔4$
 ${\mathrm{cacheFunc}}{}\left({3}\right){≔}{4}$ (6)
 > $\mathrm{cacheFunc}\left(3\right)$
 ${4}$ (7)
 > $\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{cacheFunc}\right)\right)$
 ${\mathrm{Cache}}{}\left({512}{,}'{\mathrm{temporary}}'{=}\left[{1}{=}{1}{,}{2}{=}{2}{,}{3}{=}{4}\right]\right)$ (8)
 > $\mathrm{Cache}:-\mathrm{AddPermanent}\left(\mathrm{cacheFunc},\left[5\right],10\right)$
 > $\mathrm{cacheFunc}\left(5\right)$
 ${10}$ (9)
 > $\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{cacheFunc}\right)\right)$
 ${\mathrm{Cache}}{}\left({512}{,}'{\mathrm{temporary}}'{=}\left[{1}{=}{1}{,}{2}{=}{2}{,}{3}{=}{4}\right]{,}'{\mathrm{permanent}}'{=}\left[{5}{=}{10}\right]\right)$ (10)
 > cacheFunc := proc() option cache(16); args; end proc;
 ${\mathrm{cacheFunc}}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{cache}}{}\left({16}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{args}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (11)
 > remFunc := proc() option remember; args; end proc;
 ${\mathrm{remFunc}}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{remember}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{args}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (12)
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{10}^{5}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{cacheFunc}\left(i\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{remFunc}\left(i\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$$\mathrm{length}\left(\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{cacheFunc}\right)\right)\right)$
 ${132}$ (13)
 > $\mathrm{length}\left(\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{remFunc}\right)\right)\right)$
 ${753102}$ (14)