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

NameTypeRequiredDefaultDescription
fhAnyYesFunction handle to memoize.

Returns

NameTypeDescription
memoizedFcnAnyMemoizedFunction handle object.

Errors

IdentifierWhenMessage
RunMat:memoize:InvalidFunctionThe input is not a function handle or closure.memoize: input must be a function handle
RunMat:memoize:GcFailureThe memoized object target cannot be allocated or accessed.memoize: internal object storage failed

How memoize works

  • fh must 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 NaN values compare equal for cache lookup.
  • Function stores the wrapped function handle and is read-only.
  • Enabled defaults to true. When false, calls dispatch to the wrapped function without reading, writing, or updating cache statistics.
  • CacheSize defaults 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.
  • clearAllMemoizedCaches clears every memoized-function cache in the current runtime thread.
  • stats(memoizedFcn) returns a structure with Cache.Inputs, Cache.Nargout, Cache.Outputs, Cache.HitCount, Cache.TotalHits, and Cache.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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how memoize is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.