Classic Singleton vs. Lazy with Java 8 Performance

I recently read the article " " Lazy With Java 8 ", which introduced a way to create lazy objects (objects that will create their internal state on first access).

public final class Lazy<T> {

    private volatile T value;

    public T getOrCompute(Supplier<T> supplier){
        final T result = value;
        return result == null ? maybeCompute(supplier) : result;
    }

    private synchronized T maybeCompute(Supplier<T> supplier) {
        if (value == null){
            value = Objects.requireNonNull(supplier.get());
        }
        return value;
    }
}

I found that this pattern is very similar to the well-known singleton pattern, with the exception of generics:

public class PropertiesSingleton {

    public static Properties getProperties(){
        return Helper.INSTANCE;
    }

    private final static class Helper{
        private final static Properties INSTANCE = computeWithClassLoaderLock();


        private static Properties computeWithClassLoaderLock(){
            return new Properties();
        }
    }
}

Lazy volatile , ( , ). , , getOrCompute Lazy (- ), Singleton , L1 L2. JMH, CentOS 6 Intel (R) Core i5-3470 @3.20 GHz. Git: https://github.com/maximkir/LazyObjectVsSingletonPerformance

:

Benchmark                                   Mode      Cnt   Score   Error  Units
LazyVsSingletonPerformance.testLazy       sample  1101716  33.793 ± 0.148  ns/op
LazyVsSingletonPerformance.testSingleton  sample   622603  33.993 ± 0.179  ns/op

, , . , . ? ? ? ?

:

@State(Scope.Thread)
public class LazyVsSingletonPerformance {

    Blackhole bh = new Blackhole();
    Lazy<Properties> lazyProperties = new Lazy<>();

    public static void main(String... args) throws Exception{
        Options opts = new OptionsBuilder()
                .include(LazyVsSingletonPerformance.class.getSimpleName())
                .warmupIterations(3)
                .forks(2)
                .measurementIterations(3)
                .mode(Mode.SampleTime)
                .measurementTime(TimeValue.seconds(10))
                .timeUnit(TimeUnit.NANOSECONDS)
                .build();

        new Runner(opts).run();
    }


    @Benchmark
    public void testLazy(){
        bh.consume(lazyProperties.getOrCompute(() -> new Properties()));
    }


    @Benchmark
    public void testSingleton(){
        bh.consume(PropertiesSingleton.getProperties());
    }
+4
1

concurrency, , . Scope.Thread. , Lazy, concurrency.

Lazy ( apache commons LazyInitializer), .

   org.sample;

import java.util.Properties;

public class Eager {
    private final Properties value = new Properties();

    public Properties get(){
        return value;
    }
}

   org.sample;

import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;

import java.util.Properties;

public class Lazy extends LazyInitializer<Properties> {
    @Override
    protected Properties initialize() throws ConcurrentException {
        return new Properties();
    }
}

Singleton .

Benchmark    org.sample;

import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

import java.util.Properties;

@State(Scope.Benchmark)
public class MyBenchmark {
    private Lazy lazyProperties = new Lazy();
    private Eager eagerProperties = new Eager();

    @Benchmark
    public Properties testEager(){
        return eagerProperties.get();
    }

    @Benchmark
    public Properties testLazy() throws ConcurrentException {
        return lazyProperties.get();
    }    

    @Benchmark
    public Properties testSingleton(){
        return PropertiesSingleton.getProperties();
    }
}

Benchmark                   Mode  Cnt         Score         Error  Units
MyBenchmark.testEager      thrpt   20  90980753,160 ± 4075331,777  ops/s
MyBenchmark.testLazy       thrpt   20  83876826,598 ± 3445507,139  ops/s
MyBenchmark.testSingleton  thrpt   20  82260350,608 ± 3524764,266  ops/s
0

All Articles