Giter Site home page Giter Site logo

cacheme's Introduction

cacheMe please

A very basic caching helper for procedures:

import cacheme

type
  Bar = object
    name: string
    a, b: int

proc foo(a, b: int): Bar {.cacheMe: "/tmp/test_cache".} =
  result = Bar(a: a, b: b, name: "Hehe")

echo foo(1, 5)
echo foo(5, 3)
echo foo(1, 5)
echo foo(8, 12)

It caches function calls of foo with the arguments a, b over multiple runs of the same program. It arose from a need in my code where I have some function that needs to perform a lengthy calculation, but caching it is fine. Instead of writing similar caching code in each case, I decided to just write a short library to take care of it generally. Note that the cache also works well for multiple processes (e.g. via cligen’s procpool) accessing the same file by trying to merge changes between different processes writing potentially different data (see more below).

The idea is we construct an HDF5 file with the given prefix path given to the pragma. It effectively wraps the procedure body in the following skeleton:

template injectOldBody(args, retTyp, path, body: untyped): untyped {.dirty.} =
  var theCache {.global.} = initCache[typeof args, retTyp](path)
  if args.inCache(theCache, path):
    result = args.getFromCache(theCache)
  else:
    body
    result.addToCache(args, theCache, path)

That means we construct a global (!) Table[K, V] to store the arguments and return values at runtime. That table is serialized to disk to an HDF5 file between accesses using nimhdf5.

Feel free to write the files to /dev/shm if you’re on linux and don’t want to actually suffer the IO costs of writing to disk.

One word of caution: If your procedure contains an explicit return statement, caching won’t work correctly! And for obvious reasons do not use this for functions that depend on some runtime state either!

Finally, not every possible Nim type is supported, but the serialization of nimhdf5 supports some pretty complex objects. You may have to / want to write a custom serialization hook for specific types, like here.

Dependencies

The only dependency is nimhdf5, because the caching is done by serializing the arguments and return values to an HDF5 file. Other backends could be added trivially (as long as they have serialization support).

nimble install nimhdf5

On multiprocessing

Multiple threads is a different matter obviously and theoretical races are obviously possible. However, if the procedure is a pure function in the sense of same input ⇒ same output and if you are fine if sometimes a value is computed twice before the cache is synchronized again, it works well in practice!

Similar libraries

  • memo by @andreaferretti performs memoization of functions in a similar manner, however only persistent between a single program run. On the other hand it can also handle recursive functions. Different use cases require different solutions!

cacheme's People

Contributors

vindaar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.