diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/SimpleFactory.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/SimpleFactory.java index b194c0fa3b7..60a7d8ddf28 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/SimpleFactory.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/SimpleFactory.java @@ -2,6 +2,8 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; import com.google.common.collect.ImmutableSet; @@ -306,7 +308,7 @@ public String toString() { } // private volatile CLDRFile result; // used in handleMake - private File sourceDirectories[]; + private final File sourceDirectories[]; private Set localeList = new TreeSet<>(); private Cache combinedCache = null; // private Map combinedCache= null; @@ -330,7 +332,9 @@ public String toString() { // LockSupportMap<>(); private static Cache factoryLookupMap = null; - private SimpleFactory() {} + private SimpleFactory() { + sourceDirectories = new File[0]; + } @Override public DraftStatus getMinimalDraftStatus() { @@ -775,33 +779,55 @@ public File[] getSourceDirectories() { return sourceDirectories; } + private LoadingCache> sourceDirCache = + CacheBuilder.newBuilder() + .build( + new CacheLoader<>() { + @Override + public List load(String localeName) throws Exception { + Builder result = null; + boolean isSupplemental = + CLDRFile.isSupplementalName(localeName); + for (File sourceDirectory : sourceDirectories) { + if (isSupplemental) { + sourceDirectory = + new File( + sourceDirectory + .getAbsolutePath() + .replace( + "incoming" + + File.separator + + "vetted" + + File + .separator, + "common" + + File + .separator)); + } + final File dir = + isSupplemental + ? new File( + sourceDirectory, "../supplemental") + : sourceDirectory; + final File xmlFile = makeFileName(localeName, dir); + if (xmlFile.canRead()) { + if (result == null) { + result = ImmutableList.builder(); + } + result.add(dir); + } + } + return result == null ? ImmutableList.of() : result.build(); + } + }); + @Override public List getSourceDirectoriesForLocale(String localeName) { - Builder result = null; - boolean isSupplemental = CLDRFile.isSupplementalName(localeName); - for (File sourceDirectory : this.sourceDirectories) { - if (isSupplemental) { - sourceDirectory = - new File( - sourceDirectory - .getAbsolutePath() - .replace( - "incoming" - + File.separator - + "vetted" - + File.separator, - "common" + File.separator)); - } - final File dir = - isSupplemental ? new File(sourceDirectory, "../supplemental") : sourceDirectory; - final File xmlFile = makeFileName(localeName, dir); - if (xmlFile.canRead()) { - if (result == null) { - result = ImmutableList.builder(); - } - result.add(dir); - } + final List cache = sourceDirCache.getUnchecked(localeName); + if (cache.isEmpty()) { + return null; + } else { + return cache; } - return result == null ? null : result.build(); } } diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestCLDRFile.java b/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestCLDRFile.java index d30a5bcbd18..0749bc10a3e 100644 --- a/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestCLDRFile.java +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestCLDRFile.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Set; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; @@ -24,6 +25,7 @@ import org.unicode.cldr.tool.PathInfo; import org.unicode.cldr.util.CLDRFile.DraftStatus; import org.unicode.cldr.util.LocaleInheritanceInfo.Reason; +import org.unicode.cldr.util.SimpleFactory.NoSourceDirectoryException; /** * This contains additional tests in JUnit. @@ -372,4 +374,19 @@ public void TestInternedPaths() { } } } + + @Test + @Disabled + /** enable manually - for testing performance */ + public void TestSimpleFactoryPerf() { + for (int i = 0; i < 10000; i++) { + Factory baselineFactory = + CLDRConfig.getInstance().getCommonAndSeedAndMainAndAnnotationsFactory(); + baselineFactory.make("en_BE".toString(), true); + assertThrows( + NoSourceDirectoryException.class, + () -> baselineFactory.make("de_RU".toString(), true)); + baselineFactory.make("es_MX".toString(), true); + } + } }