Brian's Waste of Time

Tue, 20 Apr 2004

Too Much Magic

Leo Simmons charged me with using pooling from constructors. Well, why not. I don't advocate doing this at home though. Leo is a trained expert ;-)

We will be pooling that most vicious of resource-heavy classes, the HashSet. So, we build a basic HashSet pool (note we use a derivitive of HashSet so that I don't have to instrument the java runtime library).

package org.skife.pool;

import java.util.Stack;

public class HashPot {
    private Stack pool;
    public static final HashPot DEALER = new HashPot(5);

    public HashPot(int size) {
        pool = new Stack();
        for (int i = 0; i < size; ++i) {
            pool.push(new SpecialHashSet());
        }
    }

    public void release(SpecialHashSet set) {
        synchronized (set) {
            set.clear();
            pool.push(set);
        }
    }

    public SpecialHashSet obtain(){
        SpecialHashSet set = (SpecialHashSet) pool.pop();
        if (set == null) set = new SpecialHashSet();
        return set;
    }

    public int getFreeCount(){
        return pool.size();
    }
}

Nothin fancy there. The fun stuff comes in with the pooling...

package org.skife.pool;

public aspect HashPool
{
    pointcut create() : call(public SpecialHashSet.new())
                        && !within(HashPot);

    SpecialHashSet around() : create() {
        return HashPot.DEALER.obtain();
    }
}

Here we intercept the call to new SpecialHashSet() except within our pool (need to let the pool create them, after all).

Finally, we need to make sure it works:

package org.skife.pool;

import junit.framework.TestCase;

public class TestHash extends TestCase {

    public void testPullFromPool() {
        int start = HashPot.DEALER.getFreeCount();
        new SpecialHashSet();
        assertEquals(start - 1, HashPot.DEALER.getFreeCount());
    }

    public void returnToPool() {
        int start = HashPot.DEALER.getFreeCount();
        SpecialHashSet set = new SpecialHashSet();
        HashPot.DEALER.release(set);
        assertEquals(start, HashPot.DEALER.getFreeCount());
    }
}

And all these pass =) The SpecialHashSet is in fact pulled from the pool, rather than instantiated. Cool! Of course you still need to return the thing to the pool when you are finished with it. Java doesn't have a good way to detect "finished with it" but in Objective-C we could add a hook to the reference counter.

8 writebacks [/src/java] permanent link