FIXED - FLEX-33852 Port UIDUtil optimizations to RPCUIDUtil
Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/246600d1 Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/246600d1 Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/246600d1 Branch: refs/heads/develop Commit: 246600d16d2ff0c55929a46a6bc7fff1323fc691 Parents: 2fd4aad Author: mamsellem <maurice.amsel...@systar.com> Authored: Thu Oct 31 09:10:03 2013 +0100 Committer: mamsellem <maurice.amsel...@systar.com> Committed: Thu Oct 31 09:10:03 2013 +0100 ---------------------------------------------------------------------- .../projects/rpc/src/mx/utils/RPCUIDUtil.as | 57 +++++++++----------- 1 file changed, 25 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/246600d1/frameworks/projects/rpc/src/mx/utils/RPCUIDUtil.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/rpc/src/mx/utils/RPCUIDUtil.as b/frameworks/projects/rpc/src/mx/utils/RPCUIDUtil.as index eda9caa..84b4620 100644 --- a/frameworks/projects/rpc/src/mx/utils/RPCUIDUtil.as +++ b/frameworks/projects/rpc/src/mx/utils/RPCUIDUtil.as @@ -45,8 +45,11 @@ public class RPCUIDUtil * @private * Char codes for 0123456789ABCDEF */ - private static const ALPHA_CHAR_CODES:Array = [48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 65, 66, 67, 68, 69, 70]; + private static const ALPHA_CHAR_CODES:Array = [48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 65, 66, 67, 68, 69, 70]; + + private static const DASH:int = 45; // dash ascii + private static const UIDBuffer:ByteArray = new ByteArray(); // static ByteArray used for UID generation to save memory allocation cost //-------------------------------------------------------------------------- // @@ -74,50 +77,40 @@ public class RPCUIDUtil */ public static function createUID():String { - var uid:Array = new Array(36); - var index:int = 0; - + UIDBuffer.position = 0; + var i:int; var j:int; - + for (i = 0; i < 8; i++) { - uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)]; + UIDBuffer.writeByte(ALPHA_CHAR_CODES[int(Math.random() * 16)]); } for (i = 0; i < 3; i++) { - uid[index++] = 45; // charCode for "-" - + UIDBuffer.writeByte(DASH); for (j = 0; j < 4; j++) { - uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)]; + UIDBuffer.writeByte(ALPHA_CHAR_CODES[int(Math.random() * 16)]); } } - - uid[index++] = 45; // charCode for "-" - var time:Number = new Date().getTime(); - // Note: time is the number of milliseconds since 1970, - // which is currently more than one trillion. - // We use the low 8 hex digits of this number in the UID. - // Just in case the system clock has been reset to - // Jan 1-4, 1970 (in which case this number could have only - // 1-7 hex digits), we pad on the left with 7 zeros - // before taking the low digits. - var timeString:String = ("0000000" + time.toString(16).toUpperCase()).substr(-8); - - for (i = 0; i < 8; i++) - { - uid[index++] = timeString.charCodeAt(i); - } - + UIDBuffer.writeByte(DASH); + + var time:uint = new Date().getTime(); // extract last 8 digits + var timeString:String = time.toString(16).toUpperCase(); + // 0xFFFFFFFF milliseconds ~= 3 days, so timeString may have between 1 and 8 digits, hence we need to pad with 0s to 8 digits + for (i = 8; i > timeString.length; i--) + UIDBuffer.writeByte(48); + UIDBuffer.writeUTFBytes(timeString); + for (i = 0; i < 4; i++) { - uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)]; + UIDBuffer.writeByte(ALPHA_CHAR_CODES[int(Math.random() * 16)]); } - - return String.fromCharCode.apply(null, uid); + + return UIDBuffer.toString(); } /** @@ -144,7 +137,7 @@ public class RPCUIDUtil for (var i:uint = 0; i < 16; i++) { if (i == 4 || i == 6 || i == 8 || i == 10) - chars[index++] = 45; // Hyphen char code + chars[index++] = DASH; // Hyphen char code var b:int = ba.readByte(); chars[index++] = ALPHA_CHAR_CODES[(b & 0xF0) >>> 4]; @@ -183,7 +176,7 @@ public class RPCUIDUtil // Check for correctly placed hyphens if (i == 8 || i == 13 || i == 18 || i == 23) { - if (c != 45) + if (c != DASH) { return false; }