memoize — Create a MemoizedFunction handle object that caches function-handle results by input values and requested output count.
memoizedFcn = memoize(fh) returns a MemoizedFunction handle object for function handle fh. Calling the object reuses cached outputs when the input values and requested output count match a previous call. Repeated memoize calls for the same function handle share the same object state within the current runtime thread.
Syntax
memoizedFcn = memoize(fh)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fh | Any | Yes | — | Function handle to memoize. |
Returns
| Name | Type | Description |
|---|---|---|
memoizedFcn | Any | MemoizedFunction handle object. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:memoize:InvalidFunction | The input is not a function handle or closure. | memoize: input must be a function handle |
RunMat:memoize:GcFailure | The memoized object target cannot be allocated or accessed. | memoize: internal object storage failed |
How memoize works
fhmust be a function handle or anonymous-function closure.- The returned object is callable like the original function through normal function-call syntax or
feval. - Cache entries are keyed by the input values and the number of requested outputs.
- Numeric
NaNvalues compare equal for cache lookup. Functionstores the wrapped function handle and is read-only.Enableddefaults to true. When false, calls dispatch to the wrapped function without reading, writing, or updating cache statistics.CacheSizedefaults to 10 and must be a positive integer scalar. The oldest entries are evicted when the cache exceeds this size.clearCache(memoizedFcn)clears one shared cache and resets its counters.clearAllMemoizedCachesclears every memoized-function cache in the current runtime thread.stats(memoizedFcn)returns a structure withCache.Inputs,Cache.Nargout,Cache.Outputs,Cache.HitCount,Cache.TotalHits, andCache.TotalMisses.
Examples
Memoize an expensive function handle
f = memoize(@(x) sum(sin(x)));
y1 = f(1:1000);
y2 = f(1:1000);Expected output:
% The second call reuses the cached result.Inspect cache statistics
f = memoize(@sqrt);
f(4);
f(4);
S = stats(f)Expected output:
% S.Cache.TotalHits is 1 and TotalMisses is 1.Limit cache capacity
f = memoize(@cos);
f.CacheSize = 2;
f(1); f(2); f(3);Expected output:
% The oldest cached entry is evicted.Disable cache lookup temporarily
f = memoize(@rand);
f.Enabled = false;
a = f(1);
b = f(1);Expected output:
% The two calls dispatch normally and do not update cache statistics.Using memoize with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how memoize changes the result.
Run a small memoize example, explain the result, then change one input and compare the output.
FAQ
Does memoize(@f) return a function handle?⌄
No. It returns a MemoizedFunction handle object that can be invoked like the original function.
Do separate memoize(@f) calls share cache data?⌄
Yes, within the current runtime thread. The cache is associated with the wrapped function identity, so repeated memoization of the same function shares cache entries and properties.
Are multiple-output calls cached separately?⌄
Yes. The requested output count is part of the cache key.
Does this launch GPU kernels?⌄
No. memoize itself is host-side dispatch and bookkeeping. The wrapped function may still call GPU-aware operations.
Related Introspection functions
addprop · class · clear · clearAllMemoizedCaches · clearCache · clearvars · dbclear · dbstack · dbstatus · dbtype · evalc · findprop · getcallinfo · inputParser · isa · ischar · isdeployed · iskeyword · ismethod · isobject · isstring · isUnderlyingType · keyboard · matlab.metadata.DynamicProperty.delete · metaclass · mislocked · mlock · munlock · namelengthmax · onCleanup · stats · superclasses · underlyingType · verLessThan · version · which · who · whos
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how memoize is executed, line by line, in Rust.
- View the source for memoize in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.