Juan Hernandez has uploaded a new change for review. Change subject: core: Improve performance of GuidUtils.toByteArray ......................................................................
core: Improve performance of GuidUtils.toByteArray There is no need to use an intermediate stream to convert from an UUID to an array of bytes, directly generating the array is much faster. Change-Id: I3f7cf2f899cd9f0be56a79b06f25ad8381bdbfc1 Signed-off-by: Juan Hernandez <[email protected]> --- M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/GuidUtils.java 1 file changed, 10 insertions(+), 37 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/67/16467/1 diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/GuidUtils.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/GuidUtils.java index d910638..f035bed 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/GuidUtils.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/GuidUtils.java @@ -1,6 +1,5 @@ package org.ovirt.engine.core.utils; -import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -12,44 +11,18 @@ public class GuidUtils { public static byte[] toByteArray(UUID uuid) { - String guidStr = uuid.toString(); - - // Going to split the GUID to hexadecimal sequences. - // Each sequence like that contains hexadecimal numbers that should be - // converted to bytes. - // As GUID may vary in size , the bytes are written to a byte array - // output stream - // which is kept in - String[] guidParts = guidStr.split("-"); - if (guidParts == null || guidParts.length == 0) { - return null; + byte[] data = new byte[16]; + long msb = uuid.getMostSignificantBits(); + for (int i = 7; i >= 0; i--) { + data[i] = (byte) (msb & 0xff); + msb >>= 8; } - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - for (String guidPart : guidParts) { - writeGUIDPartToStream(guidPart, baos); - + long lsb = uuid.getLeastSignificantBits(); + for (int i = 15; i >= 8; i--) { + data[i] = (byte) (lsb & 0xff); + lsb >>= 8; } - - return baos.toByteArray(); - } - - private static void writeGUIDPartToStream(String guidPart, ByteArrayOutputStream baos) { - // GuidPart is composed from an even number of characters. - // Each two characters are a hexadecimal number - - char[] dst = new char[guidPart.length()]; - guidPart.getChars(0, guidPart.length(), dst, 0); - for (int counter = 0; counter < (guidPart.length()) / 2; counter++) { - // Build a string from two characters - StringBuilder numberStrSB = new StringBuilder(2); - numberStrSB.append(dst[counter * 2]); - numberStrSB.append(dst[(counter * 2) + 1]); - // Convert the string to byte and add write it to the stream - int number = Integer.parseInt(numberStrSB.toString(), 16); - baos.write(number); - } + return data; } private static final String SEPARATOR = ","; -- To view, visit http://gerrit.ovirt.org/16467 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I3f7cf2f899cd9f0be56a79b06f25ad8381bdbfc1 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
