From 64e7016821f147aa83d5f7835fbb6b8b6bd32072 Mon Sep 17 00:00:00 2001 From: Stuart McCulloch Date: Sun, 23 Jan 2022 21:19:39 +0000 Subject: [PATCH 1/2] Cache and re-use binding lookups in PlaceholderBeanProvider --- .../org/eclipse/sisu/wire/BeanProviders.java | 2 +- .../sisu/wire/PlaceholderBeanProvider.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java index f7402aba..718238db 100644 --- a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java +++ b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java @@ -167,7 +167,7 @@ public V get() */ public Provider placeholderOf( final Key key ) { - return new PlaceholderBeanProvider( locator, key ); + return new PlaceholderBeanProvider( this, key ); } /** diff --git a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/PlaceholderBeanProvider.java b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/PlaceholderBeanProvider.java index f73e8871..6ec2e9dd 100644 --- a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/PlaceholderBeanProvider.java +++ b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/PlaceholderBeanProvider.java @@ -10,12 +10,13 @@ *******************************************************************************/ package org.eclipse.sisu.wire; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map; +import java.util.Map.Entry; import javax.inject.Inject; import org.eclipse.sisu.Parameters; -import org.eclipse.sisu.inject.BeanLocator; import com.google.inject.Key; import com.google.inject.Provider; @@ -49,17 +50,19 @@ final class PlaceholderBeanProvider @Inject private TypeConverterCache converterCache; - private final Provider locator; + private final BeanProviders beans; private final Key placeholderKey; + private volatile Entry, Provider> cachedLookup; + // ---------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------- - PlaceholderBeanProvider( final Provider locator, final Key key ) + PlaceholderBeanProvider( final BeanProviders beans, final Key key ) { - this.locator = locator; + this.beans = beans; placeholderKey = key; } @@ -117,9 +120,16 @@ public V get() // Implementation methods // ---------------------------------------------------------------------- + @SuppressWarnings( { "unchecked", "rawtypes" } ) private T lookup( final Key key ) { - return BeanProviders.firstOf( locator.get().locate( key ) ); + Entry, Provider> lookup = cachedLookup; + if ( null == lookup || !key.equals( lookup.getKey() ) ) + { + lookup = new SimpleImmutableEntry( key, beans.firstOf( key ) ); + cachedLookup = lookup; + } + return (T) lookup.getValue().get(); } private static String nullify( final String value ) From 3696fdbc5b0450e136d3679a0c7048375cfb996d Mon Sep 17 00:00:00 2001 From: Stuart McCulloch Date: Sun, 23 Jan 2022 22:08:09 +0000 Subject: [PATCH 2/2] Cache and re-use binding lookups in BeanProviders.firstOf --- .../org/eclipse/sisu/wire/BeanProviders.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java index 718238db..a2b2ca52 100644 --- a/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java +++ b/org.eclipse.sisu.inject/src/org/eclipse/sisu/wire/BeanProviders.java @@ -155,9 +155,16 @@ Provider firstOf( final Key key ) final Provider>> beanEntries = beanEntriesOf( key ); return new Provider() { + private volatile Iterable> cachedLookup; + public V get() { - return firstOf( beanEntries.get() ); + if ( null == cachedLookup ) + { + cachedLookup = beanEntries.get(); + } + final Iterator> itr = cachedLookup.iterator(); + return itr.hasNext() ? itr.next().getProvider().get() : null; } }; } @@ -169,13 +176,4 @@ public Provider placeholderOf( final Key key ) { return new PlaceholderBeanProvider( this, key ); } - - /** - * Selects first bean from the sequence; or null if none is available. - */ - public static V firstOf( final Iterable> entries ) - { - final Iterator> itr = entries.iterator(); - return itr.hasNext() ? itr.next().getValue() : null; - } }