提交 62e6c11e 编写于 作者: J jneufeld 提交者: Sam Judd

A couple tweaks to ModelLoaderRegistry to improve potential contention.

1) Synchronize only the inner method that accesses the cache, as the outer call
that loops and filters the loaders appears to not have threading issues.
2) Probably less of an impact since I imagine the loops are generally small,
but only allocate an ArrayList once you find a matching loader, and allocate it
with size - i which is the max possible remaining in the loop, rather than
always allocating it to size.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205877123
上级 229cb11e
......@@ -67,14 +67,19 @@ public class ModelLoaderRegistry {
}
@NonNull
public synchronized <A> List<ModelLoader<A, ?>> getModelLoaders(@NonNull A model) {
public <A> List<ModelLoader<A, ?>> getModelLoaders(@NonNull A model) {
List<ModelLoader<A, ?>> modelLoaders = getModelLoadersForClass(getClass(model));
int size = modelLoaders.size();
List<ModelLoader<A, ?>> filteredLoaders = new ArrayList<>(size);
boolean isEmpty = true;
List<ModelLoader<A, ?>> filteredLoaders = Collections.emptyList();
//noinspection ForLoopReplaceableByForEach to improve perf
for (int i = 0; i < size; i++) {
ModelLoader<A, ?> loader = modelLoaders.get(i);
if (loader.handles(model)) {
if (isEmpty) {
filteredLoaders = new ArrayList<>(size - i);
isEmpty = false;
}
filteredLoaders.add(loader);
}
}
......@@ -92,7 +97,8 @@ public class ModelLoaderRegistry {
}
@NonNull
private <A> List<ModelLoader<A, ?>> getModelLoadersForClass(@NonNull Class<A> modelClass) {
private synchronized <A> List<ModelLoader<A, ?>> getModelLoadersForClass(
@NonNull Class<A> modelClass) {
List<ModelLoader<A, ?>> loaders = cache.get(modelClass);
if (loaders == null) {
loaders = Collections.unmodifiableList(multiModelLoaderFactory.build(modelClass));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册