Make routines faster by trading space for time
Other
Switch branches/tags
Nothing to show
Latest commit a6d570d Jan 10, 2017 @azawawi Switch to zef in travis ci
Permalink
Failed to load latest commit information.
examples No need for this script anymore Mar 2, 2016
lib More documentation and 'is pure' implicit option Mar 4, 2016
logotype Add rocket logo icon :) Mar 2, 2016
t Author test META.info using Test::META Apr 26, 2016
.gitignore Initial project structure Mar 1, 2016
.travis.yml Switch to zef in travis ci Jan 10, 2017
LICENSE
META6.json Rename to META6.json Jan 10, 2017
README.md Simpler CI badges Apr 26, 2016
appveyor.yml

README.md

Memoize

Build Status Build status

This make a Perl 6 routine faster by caching its results. This means it trades more memory space used to get less execution time on cache hits. This means it is faster on routines that return a result such the following:

  • An expensive calculation (CPU)
  • A slow database query (I/O)

This is a totally-experimental-at-the-moment module to create a subroutine trait similar to the currently experimental is cached.

Plan

  • Add None to strategy to disable cache eviction and cache size limitation
  • Determine tunable cache size statistics
  • Add a pluggable architecture to cache expiry.

perlpilot: it would be interesting if you could pass the thing that handles the caching as a parameter, but perhaps only as an academic exercise.

Example

use v6;
use Memoize;

sub get-slowed-result(Int $n where $_ >= 0) is memoized {
  sleep $n / 10;
  return 1 if $n <= 1;
  return get-slowed-result($n - 1) * $n;
}

say sprintf("get-slowed-result(%d) is %d", $_, get-slowed-result($_)) for 0..10;

Memoize vs is-cached

Here is an example for is cached for the sake of completeness:

#!/usr/bin/env perl6

use v6;
use experimental :cached;

sub get-slowed-result(Int $n where $_ >= 0) is cached {
  sleep $n / 10;
  return 1 if $n <= 1;
  return get-slowed-result($n - 1) * $n;
}

say sprintf("get-slowed-result(%d) is %d", $_, get-slowed-result($_)) for 0..10;

See Also

Author

Ahmad M. Zawawi, azawawi on #perl6, https://github.com/azawawi/

License

MIT License