ARTEMIS-1357 TypedProperties encode/decode improvement

This commit is contained in:
Francesco Nigro 2017-08-21 16:29:22 +02:00 committed by Clebert Suconic
parent a9606c9b0e
commit bdf1061058
1 changed files with 54 additions and 27 deletions

View File

@ -89,17 +89,17 @@ public class TypedProperties {
public void putBooleanProperty(final SimpleString key, final boolean value) { public void putBooleanProperty(final SimpleString key, final boolean value) {
checkCreateProperties(); checkCreateProperties();
doPutValue(key, new BooleanValue(value)); doPutValue(key, BooleanValue.of(value));
} }
public void putByteProperty(final SimpleString key, final byte value) { public void putByteProperty(final SimpleString key, final byte value) {
checkCreateProperties(); checkCreateProperties();
doPutValue(key, new ByteValue(value)); doPutValue(key, ByteValue.valueOf(value));
} }
public void putBytesProperty(final SimpleString key, final byte[] value) { public void putBytesProperty(final SimpleString key, final byte[] value) {
checkCreateProperties(); checkCreateProperties();
doPutValue(key, value == null ? new NullValue() : new BytesValue(value)); doPutValue(key, value == null ? NullValue.INSTANCE : new BytesValue(value));
} }
public void putShortProperty(final SimpleString key, final short value) { public void putShortProperty(final SimpleString key, final short value) {
@ -129,12 +129,12 @@ public class TypedProperties {
public void putSimpleStringProperty(final SimpleString key, final SimpleString value) { public void putSimpleStringProperty(final SimpleString key, final SimpleString value) {
checkCreateProperties(); checkCreateProperties();
doPutValue(key, value == null ? new NullValue() : new StringValue(value)); doPutValue(key, value == null ? NullValue.INSTANCE : new StringValue(value));
} }
public void putNullValue(final SimpleString key) { public void putNullValue(final SimpleString key) {
checkCreateProperties(); checkCreateProperties();
doPutValue(key, new NullValue()); doPutValue(key, NullValue.INSTANCE);
} }
public void putCharProperty(final SimpleString key, final char value) { public void putCharProperty(final SimpleString key, final char value) {
@ -337,7 +337,8 @@ public class TypedProperties {
} else { } else {
int numHeaders = buffer.readInt(); int numHeaders = buffer.readInt();
properties = new HashMap<>(numHeaders); //optimize the case of no collisions to avoid any resize (it doubles the map size!!!) when load factor is reached
properties = new HashMap<>(numHeaders, 1.0f);
size = 0; size = 0;
for (int i = 0; i < numHeaders; i++) { for (int i = 0; i < numHeaders; i++) {
@ -352,7 +353,7 @@ public class TypedProperties {
switch (type) { switch (type) {
case NULL: { case NULL: {
val = new NullValue(); val = NullValue.INSTANCE;
doPutValue(key, val); doPutValue(key, val);
break; break;
} }
@ -362,12 +363,12 @@ public class TypedProperties {
break; break;
} }
case BOOLEAN: { case BOOLEAN: {
val = new BooleanValue(buffer); val = BooleanValue.of(buffer.readBoolean());
doPutValue(key, val); doPutValue(key, val);
break; break;
} }
case BYTE: { case BYTE: {
val = new ByteValue(buffer); val = ByteValue.valueOf(buffer.readByte());
doPutValue(key, val); doPutValue(key, val);
break; break;
} }
@ -422,14 +423,13 @@ public class TypedProperties {
buffer.writeInt(properties.size()); buffer.writeInt(properties.size());
for (Map.Entry<SimpleString, PropertyValue> entry : properties.entrySet()) { //uses internal iteration to allow inlining/loop unrolling
SimpleString s = entry.getKey(); properties.forEach((key, value) -> {
byte[] data = s.getData(); final byte[] data = key.getData();
buffer.writeInt(data.length); buffer.writeInt(data.length);
buffer.writeBytes(data); buffer.writeBytes(data);
value.write(buffer);
entry.getValue().write(buffer); });
}
} }
} }
@ -567,6 +567,8 @@ public class TypedProperties {
private static final class NullValue extends PropertyValue { private static final class NullValue extends PropertyValue {
private static final NullValue INSTANCE = new NullValue();
private NullValue() { private NullValue() {
} }
@ -589,19 +591,29 @@ public class TypedProperties {
private static final class BooleanValue extends PropertyValue { private static final class BooleanValue extends PropertyValue {
final boolean val; private static final int ENCODE_SIZE = DataConstants.SIZE_BYTE + DataConstants.SIZE_BOOLEAN;
private static final BooleanValue TRUE = new BooleanValue(true);
private static final BooleanValue FALSE = new BooleanValue(false);
private final boolean val;
private final Boolean objVal;
private BooleanValue(final boolean val) { private BooleanValue(final boolean val) {
this.val = val; this.val = val;
this.objVal = val;
} }
private BooleanValue(final ByteBuf buffer) { private static BooleanValue of(final boolean val) {
val = buffer.readBoolean(); if (val) {
return TRUE;
} else {
return FALSE;
}
} }
@Override @Override
public Object getValue() { public Object getValue() {
return val; return objVal;
} }
@Override @Override
@ -612,26 +624,41 @@ public class TypedProperties {
@Override @Override
public int encodeSize() { public int encodeSize() {
return DataConstants.SIZE_BYTE + DataConstants.SIZE_BOOLEAN; return ENCODE_SIZE;
} }
} }
private static final class ByteValue extends PropertyValue { private static final class ByteValue extends PropertyValue {
final byte val; private static final int ENCODE_SIZE = DataConstants.SIZE_BYTE + DataConstants.SIZE_BYTE;
//LAZY CACHE that uses a benign race condition to avoid too many allocations of ByteValue if contended and to allocate upfront unneeded instances.
//the Java spec doesn't allow tearing while reading/writing from arrays of references
private static final ByteValue[] VALUES = new ByteValue[-(-128) + 127 + 1];
private static ByteValue valueOf(byte b) {
final int offset = 128;
final int index = (int) b + offset;
ByteValue value = VALUES[index];
if (value == null) {
return VALUES[index] = new ByteValue(b);
} else {
return value;
}
}
private final byte val;
private final Byte objectVal;
private ByteValue(final byte val) { private ByteValue(final byte val) {
this.val = val; this.val = val;
} this.objectVal = val;
private ByteValue(final ByteBuf buffer) {
val = buffer.readByte();
} }
@Override @Override
public Object getValue() { public Object getValue() {
return val; return objectVal;
} }
@Override @Override
@ -642,7 +669,7 @@ public class TypedProperties {
@Override @Override
public int encodeSize() { public int encodeSize() {
return DataConstants.SIZE_BYTE + DataConstants.SIZE_BYTE; return ENCODE_SIZE;
} }
} }