From 5d4740e52fb7374df503df46f3f5c0489be94292 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Wed, 22 May 2019 10:59:56 +0200 Subject: Do lazy initialization of default linguistics provider --- .../yahoo/language/provider/DefaultLinguisticsProvider.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'container-core/src') diff --git a/container-core/src/main/java/com/yahoo/language/provider/DefaultLinguisticsProvider.java b/container-core/src/main/java/com/yahoo/language/provider/DefaultLinguisticsProvider.java index 8f08e33a777..d9af9bf88b2 100644 --- a/container-core/src/main/java/com/yahoo/language/provider/DefaultLinguisticsProvider.java +++ b/container-core/src/main/java/com/yahoo/language/provider/DefaultLinguisticsProvider.java @@ -1,10 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.language.provider; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.inject.Inject; -import com.yahoo.language.opennlp.OpenNlpLinguistics; import com.yahoo.container.di.componentgraph.Provider; import com.yahoo.language.Linguistics; +import com.yahoo.language.opennlp.OpenNlpLinguistics; /** * Provides the default linguistics implementation if no linguistics component has been explicitly configured @@ -14,14 +16,15 @@ import com.yahoo.language.Linguistics; */ public class DefaultLinguisticsProvider implements Provider { - private final Linguistics linguistics; + // Use lazy initialization to avoid expensive (memory-wise) instantiation f + private volatile Supplier linguisticsSupplier = Suppliers.memoize(OpenNlpLinguistics::new); @SuppressWarnings("deprecation") @Inject - public DefaultLinguisticsProvider() { linguistics = new OpenNlpLinguistics(); } + public DefaultLinguisticsProvider() { } @Override - public Linguistics get() { return linguistics; } + public Linguistics get() { return linguisticsSupplier.get(); } @Override public void deconstruct() {} -- cgit v1.2.3