Juan Hernandez has uploaded a new change for review.

Change subject: core: Improve performance of GUID constructor
......................................................................

core: Improve performance of GUID constructor

The GUID constructor that takes an array of bytes as parameter creates
an intermediate string representation that is then immediately
discarded. This patch changes the constructor to build the GUID without
the intermediate string, borrowing the algorithm from a private
constructor of the UUID class. In my environment this reduces the time
to run the constructor from approx 1500ns to approx 75ns. It should also
have a positive effect in garbage collection, as it doesn't generate the
intermediate string. The overall effect in performance is probably not
that big, but every bit helps.

Change-Id: Ia76c3d9e7ee7594d6d925551320635a20399f11f
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M 
backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Guid.java
1 file changed, 25 insertions(+), 60 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/21/16421/1

diff --git 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Guid.java
 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Guid.java
index 76da99b..b0dd1b7 100644
--- 
a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Guid.java
+++ 
b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/Guid.java
@@ -34,8 +34,31 @@
     }
 
     public Guid(byte[] guid, boolean keepByteOrder) {
-        String guidAsStr = getStrRepresentationOfGuid(guid, keepByteOrder);
-        uuid = UUID.fromString(guidAsStr);
+        // Note that the indexes are computed modulo the length of the input
+        // array because this is how they used to be calculated in the past,
+        // and some components (the REST API, for example) build GUIDs from
+        // array of bytes created from arbitrary strings, for example, in the
+        // BackendCapabilitiesResource class a GUID is built from the version
+        // string with this code:
+        //
+        //    public String generateId(Version v) {
+        //      Guid guid = new 
Guid((v.getMajor()+"."+v.getMinor()).getBytes(),true);
+        //      return guid.toString();
+        //    }
+        //
+        // This may result in an array of bytes shorter than the 16 bytes
+        // needed to build a GUID, thus the modulo operation is required.
+        byte[] indexes = keepByteOrder? KEEP_BYTE_ORDER_INDICES: 
CHANGE_BYTE_ORDER_INDICES;
+        long msb = 0;
+        long lsb = 0;
+        int length = guid.length;
+        for (int i = 0; i <= 7; i++) {
+            msb = (msb << 8) | (guid[indexes[i] % length] & 0xff);
+        }
+        for (int i = 8; i <= 15; i++) {
+            lsb = (lsb << 8) | (guid[indexes[i] % length] & 0xff);
+        }
+        uuid = new UUID(msb, lsb);
     }
 
     public Guid(String candidate) {
@@ -99,63 +122,5 @@
     @Override
     public String toString() {
         return uuid.toString();
-    }
-
-    /**
-     * Gets a string representation of GUID
-     *
-     * @param inguid
-     *            byte array containing the GUID data.
-     * @param keepByteOrder
-     *            determines if to keep the byte order in the string 
representation or not. For some systems as MSSQL
-     *            the bytes order should be swapped before converting to 
String, and for other systems (such as
-     *            ActiveDirectory) it should be kept.
-     * @return String representation of GUID
-     */
-    private static String getStrRepresentationOfGuid(byte[] inguid,
-            boolean keepByteOrder) {
-
-        StringBuilder strGUID = new StringBuilder();
-
-        byte[] byteOrderIndices = null;
-
-        if (keepByteOrder) {
-            byteOrderIndices = KEEP_BYTE_ORDER_INDICES;
-        } else {
-            byteOrderIndices = CHANGE_BYTE_ORDER_INDICES;
-        }
-
-        int length = inguid.length;
-        // A GUID format looks like xxxx-xx-xx-xx-xxxxxx where each "x"
-        // represents a byte in hexadecimal format
-
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[0 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[1 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[2 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[3 % length]] & 
0xFF));
-        strGUID.append('-');
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[4 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[5 % length]] & 
0xFF));
-        strGUID.append('-');
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[6 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[7 % length]] & 
0xFF));
-        strGUID.append('-');
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[8 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[9 % length]] & 
0xFF));
-        strGUID.append('-');
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[10 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[11 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[12 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[13 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[14 % length]] & 
0xFF));
-        strGUID.append(addLeadingZero(inguid[byteOrderIndices[15 % length]] & 
0xFF));
-
-        return strGUID.toString();
-
-    }
-
-    private static String addLeadingZero(int k) {
-        return (k <= 0xF) ? '0' + Integer.toHexString(k) : Integer
-                .toHexString(k);
     }
 }


-- 
To view, visit http://gerrit.ovirt.org/16421
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia76c3d9e7ee7594d6d925551320635a20399f11f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to