# Copyright 2005 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ pragma.enable("easy-return") pragma.disable("explicit-result-guard") # XXX todo: add support for cheap memoization of thunks def memoizeAuthor(DeepFrozenStamp) { def _memoize implements DeepFrozen { to run(base :DeepFrozen, options :Map ? options.size().isZero()) { # XXX add support for limited cache size def cache := [].asMap().diverge() def memoized implements DeepFrozenStamp { to __printOn(out :TextWriter) { out.write("") } match msg :DeepFrozen { cache.fetch(msg, thunk { # xxx optimization: we could cache thrown errors as well. def result := E.callWithPair(base, msg) if (result =~ dpbc :DeepPassByCopy) { # If DeepPassByCopy coerces (I haven't determined whether it should), then we return the coercion, since doing so is safe (no non-DPBC objects escape) cache[msg] := dpbc } else { result } }) } match msg { E.callWithPair(base, msg) } } return memoized } } # must be separate to avoid cycles, for simple DeepFrozen auditing /** Return a memoizing, yet DeepFrozen, version of a DeepFrozen function or other object. */ def memoize implements DeepFrozen { /** Memoize 'base' with default options. */ to run(base) { return _memoize(base, [].asMap()) } /** Memoize 'base' with options specified. There are no options defined yet. */ to run(base, options) { return _memoize(base, options) } } return memoize }