The sprintabout

The sprintabout is a join work between Rémi Forax Etienne Duris and Gilles Roussel.

Rationnal

The sprintabout is a really fast implementation of the visitor pattern concept. It's a 100% pure Java library that add multi-dispatch (with a single parameter) to Java without changing the compiler or the Java Virtual Machine.
The sprintabout makes dispatch code more readable, understandable and less error prone by avoiding cascade of instanceof on argument type.
The semantics of the sprintabout is greatly inspired of its little cousin of the runabout.

The sprintabout is freely available on license LGPL.

In case of multi-dispatch with more than one argument, you can use an older framework. Java MultiMethod Framework.

Example

import fr.umlv.sprintabout.VisitorGenerator;

//argument types hierarchy
interface A {}
class A0 implements A {}
class A1 implements A {}
class A2 implements A {}

public abstract class SumSprintabout {
  public int value(A0 a) { return 0; }
  public int value(A1 a) { return 1; }
  public int value(A2 a) { return 2; }
  public abstract int valueAppropriate(A a);
  public static int sum(A[] array) {
    int sum = 0;
    VisitorGenerator vg = new VisitorGenerator();
    SumSprintabout visitor = vg.createVisitor(SumSprintabout.class);
    for (int i=0; i<array.length; i++) {
      sum += visitor.valueAppropriate(array[i]);
    }
    return sum;
  }
  public static void main(String[] args) throws Exception {
    A[] array = {new A0(), new A1(), new A2() };
    System.out.println(sum(array)); 
  }
}
     

Performance

The sprintabout is really, really fast. In server mode (using c2 compiler), the sprintabout is faster than the visitor pattern and even faster than a simple dispatch using dedicated method.
In client mode, the sprintabout is 30% to 100% faster than the runabout depending on the class hierachy.

Comparison table

Little comparison between the runabout and the sprintabout :
RunaboutSprintabout
LicenseGPLLGPL
Inheritancefixedfree
Creationfreefixed,
need a default constructor
Return typenoyes
Method: modifierspublicall modifiers but private
Method: staticnoyes
ThreadSafeyesyes
JDK compatibility1.21.5
Share Type statesnewly type states are thread specificall states between all threads
Techniquegeneric code +
dedicated method call
dedicated code
(no generic part, avoid some cast)

Download

Current source of the sprintabout implementation : sprintabout-src.zip
In order to work, the sprintabout relies on version 3.0 of ASM framework. ASM is tiny an efficient bytecode manipulation framework. You can download it here.

If you want more information, you can contact me by email.

Rémi Forax 2004-07 Université de Marne la Vallée