Home

memoizing

Memoizing, or memoization, is a programming optimization that stores the results of expensive function calls and returns the cached result when the same inputs occur again. It is a specialization of caching applied to a function's outputs within a program, often implemented with a data structure such as a dictionary or map that associates input arguments with their computed results. Memoization is commonly described as a form of dynamic programming, recording subproblem results to avoid recomputing them.

How it works: when a function is called, the implementation checks the cache for a key representing

Requirements and trade-offs: memoization requires that the function be deterministic and side-effect free so cached results

Applications and variants: memoization is widely used in recursive algorithms, such as top-down dynamic programming, and

the
input
arguments.
If
a
cached
result
exists,
it
is
returned
immediately;
otherwise
the
function
executes,
the
result
is
stored
in
the
cache
with
the
corresponding
key,
and
the
result
is
returned.
Keys
must
capture
all
relevant
inputs;
for
multiple
arguments
a
composite
key,
a
tuple,
or
serialized
representation
is
often
used.
Some
implementations
limit
memory
with
eviction
policies
like
LRU.
remain
valid.
It
can
greatly
speed
up
repeated
calls,
especially
for
expensive
or
recursive
computations.
The
downsides
include
increased
memory
usage
and
the
need
to
manage
cache
invalidation
in
response
to
changes
in
inputs
or
program
state.
In
concurrent
programs,
synchronization
may
be
needed
to
avoid
race
conditions.
as
a
language
feature
or
library
facility
(for
example,
decorators
or
wrappers
that
add
caching).
It
is
related
to,
but
distinct
from,
system-level
caching
and
can
be
language-specific
in
how
caches
are
implemented
and
purged.