Monday, November 29, 2010

Cache::LRU (a handy and fast in-memory cache module in pure-perl)

Last week, after not being able to find a handy and fast cache module, I decided to write one by myself, and the outcome is Cache::LRU.

Cache::LRU is an in-memory cache module written in pure-perl. It has no dependencies, and the code is less than 100 lines long. Yet it is faster than other modules as the result of a primitive benchmark shows (note: Cache::FastMmap is a shared-memory cache and the overhead of inter-process communication needs to be taken into consideration).

$ perl -Ilib benchmark/simple.pl
cache_hit:
                          Rate Cache::Ref::LRU (List) Cache::FastMmap Cache::Ref::LRU (Array) Cache::FastMmap (raw) Tie::Cache::LRU Cache::LRU
Cache::Ref::LRU (List)  29.1/s                     --            -14%                    -31%                  -37%            -72%       -80%
Cache::FastMmap         33.9/s                    17%              --                    -20%                  -27%            -68%       -77%
Cache::Ref::LRU (Array) 42.3/s                    45%             25%                      --                   -9%            -60%       -71%
Cache::FastMmap (raw)   46.4/s                    60%             37%                     10%                    --            -56%       -69%
Tie::Cache::LRU          105/s                   260%            209%                    147%                  125%              --       -29%
Cache::LRU               148/s                   408%            336%                    249%                  218%             41%         --

cache_set:
            (warning: too few iterations for a reliable count)
            (warning: too few iterations for a reliable count)
            (warning: too few iterations for a reliable count)
                       s/iter Cache::Ref::LRU (List) Tie::Cache::LRU  Cache::LRU
Cache::Ref::LRU (List)   3.02                     --            -43%        -70%
Tie::Cache::LRU          1.71                    77%              --        -47%
Cache::LRU              0.910                   232%             88%          --
$ uname -a
Darwin ******** 10.5.0 Darwin Kernel Version 10.5.0: Fri Nov  5 23:20:39 PDT 2010; root:xnu-1504.9.17~1/RELEASE_I386 i386