Improve speed and memory use of FixedMetadataValue. Fixes BUKKIT-1460

FixedMetadataValue currently just extends LazyMetadataValue with a value
that never changes. While this works it is a lot of unneeded overhead
that causes FixedMetadataValue to be a lot slower and use a lot more
memory than one would expect. To correct this we store the value directly
in FixedMetadataValue and override the the appropriate methods to use it.

Ideally we would modify FixedMetadataValue to no longer extend
LazyMetadataValue as this would give a very large memory savings. However,
this is not currently done for backwards compatibility reasons.

By: crast <contact@jamescrasta.com>
This commit is contained in:
Bukkit/Spigot
2013-02-16 17:34:52 -07:00
parent 79f657b1a7
commit 1ed361e0cc
3 changed files with 51 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ import org.bukkit.plugin.Plugin;
public class LazyMetadataValue extends MetadataValueAdapter implements MetadataValue {
private Callable<Object> lazyValue;
private CacheStrategy cacheStrategy;
private SoftReference<Object> internalValue = new SoftReference<Object>(null);
private SoftReference<Object> internalValue;
private static final Object ACTUALLY_NULL = new Object();
/**
@@ -40,11 +40,16 @@ public class LazyMetadataValue extends MetadataValueAdapter implements MetadataV
super(owningPlugin);
Validate.notNull(cacheStrategy, "cacheStrategy cannot be null");
Validate.notNull(lazyValue, "lazyValue cannot be null");
this.internalValue = new SoftReference<Object>(null);
this.lazyValue = lazyValue;
this.cacheStrategy = cacheStrategy;
}
/** Protected special constructor used by FixedMetadataValue to bypass standard setup. */
protected LazyMetadataValue(Plugin owningPlugin) {
super(owningPlugin);
}
public Object value() {
eval();
Object value = internalValue.get();