Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-17326 cache Factory.getSourceDirectoriesForLocale() #3474

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -306,7 +308,7 @@ public String toString() {
}

// private volatile CLDRFile result; // used in handleMake
private File sourceDirectories[];
private final File sourceDirectories[];
private Set<String> localeList = new TreeSet<>();
private Cache<CLDRCacheKey, CLDRFile> combinedCache = null;
// private Map<CLDRCacheKey,CLDRFile> combinedCache= null;
Expand All @@ -330,7 +332,9 @@ public String toString() {
// LockSupportMap<>();
private static Cache<SimpleFactoryLookupKey, SimpleFactoryCacheKey> factoryLookupMap = null;

private SimpleFactory() {}
private SimpleFactory() {
sourceDirectories = new File[0];
}

@Override
public DraftStatus getMinimalDraftStatus() {
Expand Down Expand Up @@ -775,33 +779,55 @@ public File[] getSourceDirectories() {
return sourceDirectories;
}

private LoadingCache<String, List<File>> sourceDirCache =
CacheBuilder.newBuilder()
.build(
new CacheLoader<>() {
@Override
public List<File> load(String localeName) throws Exception {
Builder<File> 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.<File>builder();
}
result.add(dir);
}
}
return result == null ? ImmutableList.of() : result.build();
}
});

@Override
public List<File> getSourceDirectoriesForLocale(String localeName) {
Builder<File> 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.<File>builder();
}
result.add(dir);
}
final List<File> cache = sourceDirCache.getUnchecked(localeName);
if (cache.isEmpty()) {
return null;
} else {
return cache;
}
return result == null ? null : result.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Loading