[concurrency-interest] Lock free caches

Tim Peierls tim at peierls.net
Wed Apr 11 10:45:53 EDT 2007

On 4/11/07, Ernst, Matthias <matthias.ernst at coremedia.com> wrote:
> a technique for memoizing small amounts with a minimum of synchronization
> I'm sometimes using is copy-on-write on a plain hashmap: [...]
> Synchronization cost: one volatile read and I guess the code path is a
> little shorter than with a CHM.

The synch cost of MemoizedFunction.apply(arg) for a previously cached value
of arg is a CHM.get() plus a FutureTask.get() on a completed task -- usually
a small number of volatile reads, I believe.

It's worth getting memoization right once, and encapsulating it. Once you
have it, all you need to do is something like this:

    private static final Function<String, Pattern> PATTERN_COMPILE =
        new MemoizedFunction<String, Pattern>(new Function<String,
Pattern>() {
            public Pattern apply(String s) { return Pattern.compile(s); }

Then you can safely write:

    Pattern pat = PATTERN_COMPILE.apply("truth(?:iness)?");

This really ought to be standardized, or at least made available via some
quasi-standard resource. I had hoped that http://jcip.net would become such
a resource, but alas, everyone seems to be too busy to manage this.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070411/d1c4ce9e/attachment.html 

More information about the Concurrency-interest mailing list