From 62e6c11e6e4e4d0cf5f4c380219fe10129bb2694 Mon Sep 17 00:00:00 2001 From: jneufeld Date: Tue, 24 Jul 2018 13:01:33 -0700 Subject: [PATCH] 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 --- .../glide/load/model/ModelLoaderRegistry.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/load/model/ModelLoaderRegistry.java b/library/src/main/java/com/bumptech/glide/load/model/ModelLoaderRegistry.java index ef11d7a211..0b52d387e0 100644 --- a/library/src/main/java/com/bumptech/glide/load/model/ModelLoaderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/load/model/ModelLoaderRegistry.java @@ -67,14 +67,19 @@ private void tearDown( } @NonNull - public synchronized List> getModelLoaders(@NonNull A model) { + public List> getModelLoaders(@NonNull A model) { List> modelLoaders = getModelLoadersForClass(getClass(model)); int size = modelLoaders.size(); - List> filteredLoaders = new ArrayList<>(size); + boolean isEmpty = true; + List> filteredLoaders = Collections.emptyList(); //noinspection ForLoopReplaceableByForEach to improve perf for (int i = 0; i < size; i++) { ModelLoader 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 synchronized List> getDataClasses(@NonNull Class modelClass) } @NonNull - private List> getModelLoadersForClass(@NonNull Class modelClass) { + private synchronized List> getModelLoadersForClass( + @NonNull Class modelClass) { List> loaders = cache.get(modelClass); if (loaders == null) { loaders = Collections.unmodifiableList(multiModelLoaderFactory.build(modelClass));