MemoizedSerialization.jl is a Julia package that provides macros for memoizing the results of function calls using Serialization.jl. This is particularly useful for expensive computations with non-hashable arguments, as it allows the user to define custom keys for memoization. This package allows you to persist the results of function calls and retrieve them from disk if they have been previously computed, saving time for repeated evaluations.
- Memoize expensive computations with user-defined keys.
- Serialize and deserialize results using Serialization.jl.
- Flexibility to manage paths either implicitly or explicitly.
julia> ] add MemoizedSerialization
The following example demonstrates memoization where the path for serialization is implicitly managed:
using MemoizedSerialization
function sum(a, b)
println("Computing sum($a, $b)")
return a + b
end
# first call with (1, 2) - computation is performed and result is serialized
a, b = 1, 2
result = @memoized_serialization "sum-$a-$b" sum(a, b)
# second call with (2, 2) - computation is performed and result is serialized
a, b = 2, 2
result = @memoized_serialization "sum-$a-$b" sum(a, b)
# third call with (1, 2) - result is loaded from cache (deserialized)
a, b = 1, 2
result = @memoized_serialization "sum-$a-$b" sum(a, b)
You can also specify an explicit path to store the serialized results:
using MemoizedSerialization
function sum(a, b)
println("Computing sum($a, $b)")
return a + b
end
# define a directory for storage
path = mktempdir()
# first call with (1, 2) - computation is performed and result is serialized at the specified path
a, b = 1, 2
result = @memoized_serialization path "sum-$a-$b" sum(a, b)
# second call with (2, 2) - computation is performed and result is serialized at the specified path
a, b = 2, 2
result = @memoized_serialization path "sum-$a-$b" sum(a, b)
# third call with (1, 2) - result is loaded from cache (deserialized) from the specified path
a, b = 1, 2
result = @memoized_serialization path "sum-$a-$b" sum(a, b)
Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.