Skip to content

Commit

Permalink
Reduce the jar size by 400kb (codegen)
Browse files Browse the repository at this point in the history
When introducing code generation for the entry and cache, it was overly
optimistic and aggressive. That resulted in a larger binary and slow
byte code verification. The type hierarchy was optimized and the least
useful cases were promoted back to declared references.

The CacheLoader reference is another memory optimization that can be
rolled back. It should be very commonly used, as we strongly recommend
using a self populating cache instead of a manual one. The difference
is 14k lines generated (400kb), which is a lot less byte code that
might be verified.
  • Loading branch information
ben-manes committed Feb 19, 2016
1 parent 0337c68 commit 610d90e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public enum Feature {
MAXIMUM_WEIGHT,

LISTENING,
LOADING,
STATS;

public static String makeEnumName(Iterable<Feature> features) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import javax.lang.model.element.Modifier;

import com.github.benmanes.caffeine.cache.local.AddCacheLoader;
import com.github.benmanes.caffeine.cache.local.AddConstructor;
import com.github.benmanes.caffeine.cache.local.AddDeques;
import com.github.benmanes.caffeine.cache.local.AddExpirationTicker;
Expand Down Expand Up @@ -71,14 +70,14 @@
* @author [email protected] (Ben Manes)
*/
public final class LocalCacheFactoryGenerator {
final Feature[] featureByIndex = new Feature[] {null, null, Feature.LOADING, Feature.LISTENING,
final Feature[] featureByIndex = new Feature[] {null, null, Feature.LISTENING,
Feature.STATS, Feature.MAXIMUM_SIZE, Feature.MAXIMUM_WEIGHT, Feature.EXPIRE_ACCESS,
Feature.EXPIRE_WRITE, Feature.REFRESH_WRITE};
final List<LocalCacheRule> rules = ImmutableList.of(new AddSubtype(), new AddConstructor(),
new AddKeyValueStrength(), new AddCacheLoader(), new AddRemovalListener(),
new AddStats(), new AddExpirationTicker(), new AddMaximum(), new AddFastPath(),
new AddDeques(), new AddExpireAfterAccess(), new AddExpireAfterWrite(),
new AddRefreshAfterWrite(), new AddWriteQueue(), new Finalize());
new AddKeyValueStrength(), new AddRemovalListener(), new AddStats(),
new AddExpirationTicker(), new AddMaximum(), new AddFastPath(), new AddDeques(),
new AddExpireAfterAccess(), new AddExpireAfterWrite(), new AddRefreshAfterWrite(),
new AddWriteQueue(), new Finalize());
final NavigableMap<String, ImmutableSet<Feature>> classNameToFeatures;
final Path directory;

Expand Down Expand Up @@ -204,7 +203,6 @@ private static String encode(String className) {
.replaceFirst("WEAK_KEYS", "W")
.replaceFirst("_STRONG_VALUES", "S")
.replaceFirst("_INFIRM_VALUES", "I")
.replaceFirst("_LOADING", "Lo")
.replaceFirst("_LISTENING", "Li")
.replaceFirst("_STATS", "S")
.replaceFirst("_MAXIMUM", "M")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ private LocalCacheSelectorCode values() {
return this;
}

private LocalCacheSelectorCode cacheLoader() {
block.beginControlFlow("if (cacheLoader != null)")
.addStatement("sb.append(\"Lo\")")
.endControlFlow();
return this;
}

private LocalCacheSelectorCode removalListener() {
block.beginControlFlow("if (builder.removalListener != null)")
.addStatement("sb.append(\"Li\")")
Expand Down Expand Up @@ -115,7 +108,6 @@ public static CodeBlock get(Set<String> classNames) {
return new LocalCacheSelectorCode()
.keys()
.values()
.cacheLoader()
.removalListener()
.stats()
.maximum()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ protected void execute() {
.addParameter(BUILDER_PARAM)
.addParameter(CACHE_LOADER_PARAM)
.addParameter(boolean.class, "async")
.addStatement(context.parentFeatures.isEmpty()
? "super(builder, async)"
: "super(builder, cacheLoader, async)");
.addStatement("super(builder, (CacheLoader<K, V>) cacheLoader, async)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ abstract class BoundedLocalCache<K, V> extends BLCHeader.DrainStatusRef<K, V>

final ConcurrentHashMap<Object, Node<K, V>> data;
final Consumer<Node<K, V>> accessPolicy;
final CacheLoader<K, V> cacheLoader;
final Buffer<Node<K, V>> readBuffer;
final Runnable drainBuffersTask;
final CacheWriter<K, V> writer;
Expand All @@ -147,8 +148,10 @@ abstract class BoundedLocalCache<K, V> extends BLCHeader.DrainStatusRef<K, V>
transient Set<Entry<K, V>> entrySet;

/** Creates an instance based on the builder's configuration. */
protected BoundedLocalCache(Caffeine<K, V> builder, boolean isAsync) {
protected BoundedLocalCache(Caffeine<K, V> builder,
@Nullable CacheLoader<K, V> cacheLoader, boolean isAsync) {
this.isAsync = isAsync;
this.cacheLoader = cacheLoader;
executor = builder.getExecutor();
writer = builder.getCacheWriter();
weigher = builder.getWeigher(isAsync);
Expand Down Expand Up @@ -207,10 +210,6 @@ protected Queue<Runnable> writeQueue() {
throw new UnsupportedOperationException();
}

protected CacheLoader<? super K, V> cacheLoader() {
throw new UnsupportedOperationException();
}

@Override
public final Executor executor() {
return executor;
Expand Down Expand Up @@ -762,7 +761,7 @@ void refreshIfNeeded(Node<K, V> node, long now) {
return v;
}
try {
return cacheLoader().reload(k, v);
return cacheLoader.reload(k, v);
} catch (Exception e) {
node.setWriteTime(writeTime);
return LocalCache.throwUnchecked(e);
Expand Down Expand Up @@ -2635,7 +2634,7 @@ static final class BoundedLocalLoadingCache<K, V> extends BoundedLocalManualCach

@Override
public CacheLoader<? super K, V> cacheLoader() {
return cache.cacheLoader();
return cache.cacheLoader;
}

@Override
Expand All @@ -2659,7 +2658,7 @@ Object writeReplace() {
if (cache.refreshAfterWrite()) {
proxy.refreshAfterWriteNanos = cache.refreshAfterWriteNanos();
}
proxy.loader = cache.cacheLoader();
proxy.loader = cache.cacheLoader;
return proxy;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public void asyncLoadAll() throws Throwable {
throw e.getCause();
}
}

@Test
public void asyncReload() throws Exception {
CacheLoader<Integer, Integer> loader = key -> -key;
CompletableFuture<?> future = loader.asyncReload(1, 2, MoreExecutors.directExecutor());
assertThat(future.get(), is(-1));
}

/* ---------------- getIfPresent -------------- */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static <K, V> void checkBoundedLocalManualCache(
private static <K, V> void checkBoundedLocalLoadingCache(
BoundedLocalCache.BoundedLocalLoadingCache<K, V> original,
BoundedLocalCache.BoundedLocalLoadingCache<K, V> copy, DescriptionBuilder desc) {
desc.expectThat("same cacheLoader", copy.cache.cacheLoader(), is(original.cache.cacheLoader()));
desc.expectThat("same cacheLoader", copy.cache.cacheLoader, is(original.cache.cacheLoader));
}

private static <K, V> void checkBoundedAsyncLocalLoadingCache(
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ ext {
osgi_runtime: [
"org.ops4j.pax.exam:pax-exam-container-native:${test_versions.pax_exam}",
"org.ops4j.pax.exam:pax-exam-link-mvn:${test_versions.pax_exam}",
"org.ops4j.pax.url:pax-url-aether:2.4.5",
"org.ops4j.pax.url:pax-url-aether:2.4.6",
],
testng: [
dependencies.create("org.testng:testng:${test_versions.testng}") {
Expand Down

0 comments on commit 610d90e

Please sign in to comment.