Last active
December 16, 2016 21:28
-
-
Save travisdowns/c981c55c013064ea643fe83ba545233c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import java.util.function.Supplier; | |
import org.apache.commons.lang3.concurrent.ConcurrentException; | |
import org.apache.commons.lang3.concurrent.LazyInitializer; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.CompilerControl; | |
import org.openjdk.jmh.annotations.CompilerControl.Mode; | |
import org.openjdk.jmh.annotations.Level; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
@State(Scope.Thread) | |
public class LazyInitBench { | |
private static final Integer INTEGER = new Integer(42); | |
private Supplier<Integer> gamboaField; | |
private Supplier<Integer> holgerField; | |
private Supplier<Integer> stuartField; | |
private LazyDragon<Integer> dragonField; | |
private LazyBee<Integer> beeField; | |
private LazyBeeInteger beeIntegerField; | |
@Setup(Level.Iteration) | |
public void init() { | |
holgerField = lazilyH(() -> holgerField=value(expensiveInit())); | |
stuartField = lazilyS(this::expensiveInit); | |
dragonField = new LazyDragon<>(this::expensiveInit); | |
beeField = new LazyBee<>(this::expensiveInit); | |
beeIntegerField = new LazyBeeInteger(this::expensiveInit); | |
gamboaField = () -> { | |
Integer val = expensiveInit(); | |
gamboaField = () -> val; | |
return val; | |
}; | |
} | |
@Benchmark | |
public Integer dummyReturn() { | |
return INTEGER; | |
} | |
@Benchmark | |
public Integer lazyGamboa() { | |
return gamboaField.get(); | |
} | |
@Benchmark | |
public Integer lazyHolger() { | |
return holgerField.get(); | |
} | |
@Benchmark | |
public Integer lazyStuart() { | |
return stuartField.get(); | |
} | |
@Benchmark | |
public Integer lazyDragon() throws ConcurrentException { | |
return dragonField.get(); | |
} | |
@Benchmark | |
public Integer lazyBeeT() { | |
return beeField.get(); | |
} | |
@Benchmark | |
public Integer lazyBeeInteger() { | |
return beeIntegerField.get(); | |
} | |
/////////// HOLGER //////////// | |
interface LazyHolger<T> extends Supplier<T> { | |
Supplier<T> init(); | |
@Override | |
public default T get() { return init().get(); } | |
} | |
static <U> Supplier<U> lazilyH(LazyHolger<U> lazy) { return lazy; } | |
static <T> Supplier<T> value(T value) { return ()->value; } | |
///////// STUART MARKS ///////////// | |
static <Z> Supplier<Z> lazilyS(Supplier<Z> supplier) { | |
return new Supplier<Z>() { | |
Z value; // = null | |
@Override public Z get() { | |
if (value == null) | |
value = supplier.get(); | |
return value; | |
} | |
}; | |
} | |
/////////// HIDDEN DRAGON ///////////// | |
static class LazyDragon<T> extends LazyInitializer<T> { | |
private Supplier<T> builder; | |
public LazyDragon(Supplier<T> builder) { | |
if (builder == null) throw new IllegalArgumentException(); | |
this.builder = builder; | |
} | |
@Override | |
protected T initialize() throws ConcurrentException { | |
return builder.get(); | |
} | |
} | |
///////////// BEEONROPE //////////////////// | |
public static class LazyBee<T> implements Supplier<T> { | |
private T value; | |
private final Supplier<? extends T> factory; | |
public LazyBee(Supplier<? extends T> factory) { | |
this.factory = factory; | |
} | |
@Override | |
public final T get() { | |
if (value == null) { | |
init(); | |
} | |
return value; | |
} | |
private final void init() { | |
value = factory.get(); | |
} | |
} | |
public static class LazyBeeInteger implements Supplier<Integer> { | |
private Integer value; | |
private final Supplier<? extends Integer> factory; | |
public LazyBeeInteger(Supplier<? extends Integer> factory) { | |
this.factory = factory; | |
} | |
// @Override | |
// public final Integer get() { | |
// if (value == null) { | |
// init(); | |
// } | |
// return value; | |
// } | |
// | |
// | |
// private final void init() { | |
// value = factory.get(); | |
// } | |
@Override | |
public Integer get() { | |
Integer v = value; | |
return v != null ? v : (value = factory.get()); | |
} | |
} | |
@CompilerControl(Mode.DONT_INLINE) | |
private Integer expensiveInit() { | |
return Integer.valueOf(42); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment