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

Cache binding lookups for single bean providers #45

Merged
merged 2 commits into from
Jan 24, 2022
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 @@ -155,9 +155,16 @@ <V> Provider<V> firstOf( final Key<V> key )
final Provider<Iterable<? extends BeanEntry<Annotation, V>>> beanEntries = beanEntriesOf( key );
return new Provider<V>()
{
private volatile Iterable<? extends BeanEntry<?, V>> cachedLookup;

public V get()
{
return firstOf( beanEntries.get() );
if ( null == cachedLookup )
{
cachedLookup = beanEntries.get();
}
final Iterator<? extends BeanEntry<?, V>> itr = cachedLookup.iterator();
return itr.hasNext() ? itr.next().getProvider().get() : null;
}
};
}
Expand All @@ -167,15 +174,6 @@ public V get()
*/
public <V> Provider<V> placeholderOf( final Key<V> key )
{
return new PlaceholderBeanProvider<V>( locator, key );
}

/**
* Selects first bean from the sequence; or null if none is available.
*/
public static <V> V firstOf( final Iterable<? extends Entry<?, V>> entries )
{
final Iterator<? extends Entry<?, V>> itr = entries.iterator();
return itr.hasNext() ? itr.next().getValue() : null;
return new PlaceholderBeanProvider<V>( this, key );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,17 +50,19 @@ final class PlaceholderBeanProvider<V>
@Inject
private TypeConverterCache converterCache;

private final Provider<BeanLocator> locator;
private final BeanProviders beans;

private final Key<V> placeholderKey;

private volatile Entry<Key<?>, Provider<?>> cachedLookup;

// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------

PlaceholderBeanProvider( final Provider<BeanLocator> locator, final Key<V> key )
PlaceholderBeanProvider( final BeanProviders beans, final Key<V> key )
{
this.locator = locator;
this.beans = beans;
placeholderKey = key;
}

Expand Down Expand Up @@ -117,9 +120,16 @@ public V get()
// Implementation methods
// ----------------------------------------------------------------------

@SuppressWarnings( { "unchecked", "rawtypes" } )
private <T> T lookup( final Key<T> key )
{
return BeanProviders.firstOf( locator.get().locate( key ) );
Entry<Key<?>, 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 )
Expand Down