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

@@ -1,6 +1,7 @@
package org.bukkit.metadata;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.TestPlugin;
@@ -10,21 +11,32 @@ public class FixedMetadataValueTest {
private Plugin plugin = new TestPlugin("X");
private FixedMetadataValue subject;
private void valueEquals(Object value) {
subject = new FixedMetadataValue(plugin, value);
assertEquals(value, subject.value());
@Test
public void testBasic() {
subject = new FixedMetadataValue(plugin, new Integer(50));
assertSame(plugin, subject.getOwningPlugin());
assertEquals(new Integer(50), subject.value());
}
@Test
public void testTypes() {
valueEquals(10);
valueEquals(0.1);
valueEquals("TEN");
valueEquals(true);
valueEquals(null);
valueEquals((float) 10.5);
valueEquals((long) 10);
valueEquals((short) 10);
valueEquals((byte) 10);
public void testNumberTypes() {
subject = new FixedMetadataValue(plugin, new Integer(5));
assertEquals(new Integer(5), subject.value());
assertEquals(5, subject.asInt());
assertEquals(true, subject.asBoolean());
assertEquals(5, subject.asByte());
assertEquals(5.0, subject.asFloat(), 0.1e-8);
assertEquals(5.0D, subject.asDouble(), 0.1e-8D);
assertEquals(5L, subject.asLong());
assertEquals(5, subject.asShort());
assertEquals("5", subject.asString());
}
@Test
public void testInvalidateDoesNothing() {
Object o = new Object();
subject = new FixedMetadataValue(plugin, o);
subject.invalidate();
assertSame(o, subject.value());
}
}