activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [1/2] activemq-artemis git commit: ARTEMIS-1357 TypedProperties encode/decode improvement
Date Wed, 23 Aug 2017 02:17:58 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master a9606c9b0 -> cfdd9fb5b


ARTEMIS-1357 TypedProperties encode/decode improvement


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/bdf10610
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/bdf10610
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/bdf10610

Branch: refs/heads/master
Commit: bdf1061058423ebf4d9c08d69ac9836c0c3e1085
Parents: a9606c9
Author: Francesco Nigro <nigro.fra@gmail.com>
Authored: Mon Aug 21 16:29:22 2017 +0200
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Tue Aug 22 22:17:39 2017 -0400

----------------------------------------------------------------------
 .../utils/collections/TypedProperties.java      | 81 +++++++++++++-------
 1 file changed, 54 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/bdf10610/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java
----------------------------------------------------------------------
diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java
index 5b653ab..b17156e 100644
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java
+++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java
@@ -89,17 +89,17 @@ public class TypedProperties {
 
    public void putBooleanProperty(final SimpleString key, final boolean value) {
       checkCreateProperties();
-      doPutValue(key, new BooleanValue(value));
+      doPutValue(key, BooleanValue.of(value));
    }
 
    public void putByteProperty(final SimpleString key, final byte value) {
       checkCreateProperties();
-      doPutValue(key, new ByteValue(value));
+      doPutValue(key, ByteValue.valueOf(value));
    }
 
    public void putBytesProperty(final SimpleString key, final byte[] value) {
       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) {
@@ -129,12 +129,12 @@ public class TypedProperties {
 
    public void putSimpleStringProperty(final SimpleString key, final SimpleString value)
{
       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) {
       checkCreateProperties();
-      doPutValue(key, new NullValue());
+      doPutValue(key, NullValue.INSTANCE);
    }
 
    public void putCharProperty(final SimpleString key, final char value) {
@@ -337,7 +337,8 @@ public class TypedProperties {
       } else {
          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;
 
          for (int i = 0; i < numHeaders; i++) {
@@ -352,7 +353,7 @@ public class TypedProperties {
 
             switch (type) {
                case NULL: {
-                  val = new NullValue();
+                  val = NullValue.INSTANCE;
                   doPutValue(key, val);
                   break;
                }
@@ -362,12 +363,12 @@ public class TypedProperties {
                   break;
                }
                case BOOLEAN: {
-                  val = new BooleanValue(buffer);
+                  val = BooleanValue.of(buffer.readBoolean());
                   doPutValue(key, val);
                   break;
                }
                case BYTE: {
-                  val = new ByteValue(buffer);
+                  val = ByteValue.valueOf(buffer.readByte());
                   doPutValue(key, val);
                   break;
                }
@@ -422,14 +423,13 @@ public class TypedProperties {
 
          buffer.writeInt(properties.size());
 
-         for (Map.Entry<SimpleString, PropertyValue> entry : properties.entrySet())
{
-            SimpleString s = entry.getKey();
-            byte[] data = s.getData();
+         //uses internal iteration to allow inlining/loop unrolling
+         properties.forEach((key, value) -> {
+            final byte[] data = key.getData();
             buffer.writeInt(data.length);
             buffer.writeBytes(data);
-
-            entry.getValue().write(buffer);
-         }
+            value.write(buffer);
+         });
       }
    }
 
@@ -567,6 +567,8 @@ public class TypedProperties {
 
    private static final class NullValue extends PropertyValue {
 
+      private static final NullValue INSTANCE = new NullValue();
+
       private NullValue() {
       }
 
@@ -589,19 +591,29 @@ public class TypedProperties {
 
    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) {
          this.val = val;
+         this.objVal = val;
       }
 
-      private BooleanValue(final ByteBuf buffer) {
-         val = buffer.readBoolean();
+      private static BooleanValue of(final boolean val) {
+         if (val) {
+            return TRUE;
+         } else {
+            return FALSE;
+         }
       }
 
       @Override
       public Object getValue() {
-         return val;
+         return objVal;
       }
 
       @Override
@@ -612,26 +624,41 @@ public class TypedProperties {
 
       @Override
       public int encodeSize() {
-         return DataConstants.SIZE_BYTE + DataConstants.SIZE_BOOLEAN;
+         return ENCODE_SIZE;
       }
 
    }
 
    private static final class ByteValue extends PropertyValue {
 
-      final byte val;
+      private static final int ENCODE_SIZE = DataConstants.SIZE_BYTE + DataConstants.SIZE_BYTE;
 
-      private ByteValue(final byte val) {
-         this.val = val;
+      //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 ByteValue(final ByteBuf buffer) {
-         val = buffer.readByte();
+      private final byte val;
+      private final Byte objectVal;
+
+      private ByteValue(final byte val) {
+         this.val = val;
+         this.objectVal = val;
       }
 
       @Override
       public Object getValue() {
-         return val;
+         return objectVal;
       }
 
       @Override
@@ -642,7 +669,7 @@ public class TypedProperties {
 
       @Override
       public int encodeSize() {
-         return DataConstants.SIZE_BYTE + DataConstants.SIZE_BYTE;
+         return ENCODE_SIZE;
       }
    }
 


Mime
View raw message