Skip to content

AhmedGoudaa/go_memoize

Repository files navigation

go_memoize

Workflow Status

go_memoize package provides a set of functions to memoize the results of computations, allowing for efficient caching and retrieval of results based on input parameters. This can significantly improve performance for expensive or frequently called functions.

Features

  • Memoizes functions with TTL, supporting 0 to 7 comparable parameters. List of Memoize Functions
  • High performance, zero allocation, and zero dependencies.
  • Utilizes the FNV-1a hash algorithm for caching.
  • Thread-safe and concurrent-safe.

Installation

To install the package, use go get:

go get github.com/AhmedGoudaa/go_memoize

Usage

Basic Memoization

The Memoize function can be used to memoize a function with no parameters:

computeFn := func() int {
    // Expensive computation
    return 42
}

memoizedFn := Memoize(computeFn, 10*time.Second)
result := memoizedFn()

Memoization with Parameters

The package provides functions to memoize functions with up to 7 parameters. Here are some examples:

One Parameter

computeFn := func(a int) int {
    // Expensive computation
    return a * 2
}

memoizedFn := Memoize1(computeFn, 10*time.Second)
result := memoizedFn(5)

Two Parameters

computeFn := func(a int, b string) string {
    // Expensive computation
    return fmt.Sprintf("%d-%s", a, b)
}

memoizedFn := Memoize2(computeFn, 10*time.Second)
result := memoizedFn(5, "example")

Three Parameters

computeFn := func(a int, b string, c float64) string {
    // Expensive computation
    return fmt.Sprintf("%d-%s-%f", a, b, c)
}

memoizedFn := Memoize3(computeFn, 10*time.Second)
result := memoizedFn(5, "example", 3.14)

Cache Management

The Cache struct is used internally to manage the cached entries. It supports setting, getting, and deleting entries, as well as computing new values if they are not already cached or have expired.

Example

Here is a complete example of using the memoize package:

package main

import (
    "fmt"
    "time"
    m "github.com/AhmedGoudaa/go_memoize"
)

func main() {
    computeFn := func(a int, b string) string {
        // Simulate an expensive computation
        time.Sleep(2 * time.Second)
        return fmt.Sprintf("%d-%s", a, b)
    }

    memoizedFn := m.Memoize2(computeFn, 10*time.Second)

    // First call will compute the result
    result := memoizedFn(5, "example")
    fmt.Println(result) // Output: 5-example

    // Subsequent calls within 10 seconds will use the cached result
    result = memoizedFn(5, "example")
    fmt.Println(result) // Output: 5-example
}

Functions & Usage Examples

Function Description Example
Memoize Memoizes a function with no params

memoizedFn := Memoize(func() int { return 1 }, time.Minute)
result := memoizedFn()
      
Memoize1 Memoizes a function with 1 param

memoizedFn := Memoize1(func(a int) int { return a * 2 }, time.Minute)
result := memoizedFn(5)
      
Memoize2 Memoizes a function with 2 params

memoizedFn := Memoize2(func(a int, b string) string { return fmt.Sprintf("%d-%s", a, b) }, time.Minute)
result := memoizedFn(5, "example")
      
Memoize3 Memoizes a function with 3 params

memoizedFn := Memoize3(func(a int, b string, c float64) string { return fmt.Sprintf("%d-%s-%f", a, b, c) }, time.Minute)
result := memoizedFn(5, "example", 3.14)
      
Memoize4 Memoizes a function with 4 params

memoizedFn := Memoize4(func(a, b, c, d int) int { return a + b + c + d }, time.Minute)
result := memoizedFn(1, 2, 3, 4)
      
Memoize5 Memoizes a function with 5 params

memoizedFn := Memoize5(func(a, b, c, d, e int) int { return a + b + c + d + e }, time.Minute)
result := memoizedFn(1, 2, 3, 4, 5)
      
Memoize6 Memoizes a function with 6 params

memoizedFn := Memoize6(func(a, b, c, d, e, f int) int { return a + b + c + d + e + f }, time.Minute)
result := memoizedFn(1, 2, 3, 4, 5, 6)
      
Memoize7 Memoizes a function with 7 params

memoizedFn := Memoize7(func(a, b, c, d, e, f, g int) int { return a + b + c + d + e + f + g }, time.Minute)
result := memoizedFn(1, 2, 3, 4, 5, 6, 7)
      

Device "Apple M2 Pro"

goos: darwin
goarch: arm64
BenchmarkDo0Mem-10 | 811289566 | 14.77 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo1Mem-10 | 676579908 | 18.26 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo2Mem-10 | 578134332 | 20.99 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo3Mem-10 | 533455237 | 22.67 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo4Mem-10 | 487471639 | 24.73 ns/op | 0 B/op | 0 allocs/op

This project is licensed under the Apache License. See the LICENSE file for details.