Memoization is a Swift compiler plugin that provides a @memoized
macro for automatically memoizing function results. By leveraging Swift's powerful macro system, @memoized
simplifies caching the results of expensive function calls, enhancing performance without boilerplate code.
- Automatic Caching: Automatically caches function results based on input parameters.
- Thread-Safe Storage: Utilizes a thread-safe storage mechanism to handle concurrent access.
- Easy Integration: Seamlessly integrates with your Swift projects using Swift's macro system.
- Reset Capability: Provides functions to reset the cache for specific
memoized
functions.
To integrate MemoizationMacros into your Swift project, follow these steps:
- Add the Package
Add Memoization
to your project's dependencies in your Package.swift:
dependencies: [
.package(url: "https://github.com/brunogama/Memoization.git", from: "0.0.1"),
],
- Import the Module
import Memoization
Annotate your functions with the @memoized
macro to enable memoization
.
import Memoization
class Calculator {
@memoized
func fibonacci(_ n: Int) -> Int {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
}
In this example:
The @memoized
macro automatically caches the results of the fibonacci function.
Subsequent calls with the same parameter and retrieve the result from the cache, avoiding redundant computations.
Generated code:
import Memoization
class Calculator {
func fibonacci(_ n: Int) -> Int {
if n <= 1 {
return n
}
return fibonacci(n - 1) + fibonacci(n - 2)
}
private var memoizedFibonacciStorage: MemoizeStorage<Int>? = .init()
func memoizedFibonacci(_ n: Int) -> Int {
let key = CacheKey(n)
if let cachedResult = memoizedFibonacciStorage?.getValue(for: key) {
return cachedResult
}
let result = fibonacci(n)
memoizedFibonacciStorage?[key] = CacheResult(result)
return result
}
func resetCacheFibonacci() {
memoizedFibonacciStorage?.clear()
}
}
In the future I will add policies for automatic cacche invalidation. Such as notification of low memory or stall data, timeouts and so on. Suggestions are welcome.
The @memoized
macro processes annotated functions and generates additional code to handle caching:
- Function Analysis: Extracts the function's name, return type, and parameters.
- Cache Key Generation: Creates a unique key based on the function's parameters.
- Storage Creation: Initializes a storage mechanism (MemoizeStorage) to hold cached results.
- Memoized Function: Generates a memoized version of the function that:
- Checks if the result exists in the cache.
- Returns the cached result if available.
- Computes, caches, and returns the result if not.
- Cache Reset Function: Generates a function to clear the cache for the specific memoized function.
Swift Version: Requires Swift 5.9 or later.
Platform Support: Compatible with all platforms supported by Swift.
Swift Compiler: Utilizes Swift's macro system; ensure your compiler supports macros.
Contributions are welcome! Please follow these steps:
Fork the Repository Create a Feature Branch
git checkout -b feature/YourFeature
# Commit Your Changes
git commit -m "Add YourFeature"
# Push to the Branch
git push origin feature/YourFeature
This project is licensed under the MIT License.