Skip to content
Open
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 @@ -21,6 +21,8 @@
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -1232,10 +1234,80 @@ Object putIfAbsent(int res, Object item, int size) {
});
return result[0];
}

synchronized void deduplicateTableArrays() {
Map<CharBuffer, char[]> charMap = new HashMap<>();
Map<IntBuffer, int[]> intMap = new HashMap<>();
Map<String, String> stringMap = new HashMap<>();
for (Map.Entry<Integer, Object> entry : map.entrySet()) {
entry.setValue(deduplicateItem(entry.getValue(), charMap, intMap, stringMap));
}
}

@SuppressWarnings("unchecked")
private static Object deduplicateItem(
Object value,
Map<CharBuffer, char[]> charMap,
Map<IntBuffer, int[]> intMap,
Map<String, String> stringMap) {
Object item = value;
if (item instanceof SoftReference) {
item = ((SoftReference<?>) item).get();
}
if (item instanceof Table) {
Table table = (Table) item;
if (table.keyOffsets != null) {
CharBuffer cb = CharBuffer.wrap(table.keyOffsets);
char[] existing = charMap.putIfAbsent(cb, table.keyOffsets);
if (existing != null) {
table.keyOffsets = existing;
}
}
if (table.key32Offsets != null) {
IntBuffer ib = IntBuffer.wrap(table.key32Offsets);
int[] existing = intMap.putIfAbsent(ib, table.key32Offsets);
if (existing != null) {
table.key32Offsets = existing;
}
}
} else if (item instanceof String) {
String s = (String) item;
String existing = stringMap.putIfAbsent(s, s);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can Map<String, String> stringMap be a Set<String> stringSet instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a method in Set to do so in Java unfortunately.

For performance, there is no harm to use HashMap and Map here, because HashSet in Java is just a wrapper of HashMap.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, thank you.

if (existing != null) {
item = existing;
}
if (value instanceof SoftReference) {
return new SoftReference<>(item);
} else {
return item;
}
}
return value;
}
}

private static final String ICU_RESOURCE_SUFFIX = ".res";

/** Deduplicates the char[] and int[] arrays of Table objects in the cache. */
public static void deduplicateTableArrays() {
for (Object mapValue : CACHE.getMap().values()) {
ICUResourceBundleReader reader = null;
if (mapValue instanceof CacheValue) {
@SuppressWarnings("unchecked")
CacheValue<ICUResourceBundleReader> cv =
(CacheValue<ICUResourceBundleReader>) mapValue;
if (!cv.isNull()) {
reader = cv.get();
}
} else if (mapValue instanceof ICUResourceBundleReader) {
reader = (ICUResourceBundleReader) mapValue;
}
if (reader != null && reader.resourceCache != null) {
reader.resourceCache.deduplicateTableArrays();
}
}
}

/** Gets the full name of the resource with suffix. */
public static String getFullName(String baseName, String localeName) {
if (baseName == null || baseName.length() == 0) {
Expand Down
7 changes: 7 additions & 0 deletions icu4j/main/core/src/main/java/com/ibm/icu/impl/SoftCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
public abstract class SoftCache<K, V, D> extends CacheBase<K, V, D> {
private ConcurrentHashMap<K, Object> map = new ConcurrentHashMap<K, Object>();

/**
* @return the cache contents.
*/
protected ConcurrentHashMap<K, Object> getMap() {
return map;
}

@SuppressWarnings("unchecked")
@Override
public final V getInstance(K key, D data) {
Expand Down
Loading