[Performance] instance level length var access adds a small speedup over requesting byteLength from the ArrayBuffer
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/f791f02c Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/f791f02c Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/f791f02c Branch: refs/heads/develop Commit: f791f02cf2f2cf4dcd25c6cae924923da20f0df1 Parents: 6288c2d Author: greg-dove <[email protected]> Authored: Mon Jul 25 18:14:17 2016 +1200 Committer: greg-dove <[email protected]> Committed: Mon Jul 25 18:14:17 2016 +1200 ---------------------------------------------------------------------- .../flex/org/apache/flex/utils/BinaryData.as | 23 ++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/f791f02c/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as index 769b006..d44ac0e 100644 --- a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as +++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as @@ -56,6 +56,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput public function BinaryData(bytes:ArrayBuffer = null) { ba = bytes ? bytes : new ArrayBuffer(0); + _len = ba.byteLength; } /** @@ -481,9 +482,9 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput { //do we need to check offset and length and sanitize or throw an error? - if (length == 0) length = ba.byteLength - _position ; + if (length == 0) length = _len - _position ; //extend the destination length if necessary - var extra:int = offset + length - destination.ba.byteLength; + var extra:int = offset + length - destination._len; if (extra > 0) destination.growBuffer(extra); var src:Uint8Array = new Uint8Array(ba, _position,length); @@ -546,7 +547,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput } COMPILE::JS { - if (idx >= ba.byteLength) { + if (idx >= _len) { setBufferSize(idx+1); } getTypedArray()[idx] = byte; @@ -713,6 +714,9 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput } } + COMPILE::JS + private var _len:uint; + public function get length():int { @@ -722,7 +726,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput } COMPILE::JS { - return ba.byteLength; + return _len;; } } @@ -743,7 +747,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput COMPILE::JS protected function setBufferSize(newSize):void { - var n:uint = ba.byteLength; + var n:uint = _len; if (n != newSize) { //note: ArrayBuffer.slice could be better for buffer size reduction //looks like it is only IE11+, so not using it here @@ -754,6 +758,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput ba = newView.buffer; if (_position > newSize) _position = newSize; _typedArray = newView; + _len = newSize; } } /** @@ -772,7 +777,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput } COMPILE::JS { - return ba.byteLength - _position; + return _len - _position; } } @@ -837,13 +842,13 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput COMPILE::JS { - setBufferSize(ba.byteLength + extra); + setBufferSize(_len + extra); } } COMPILE::JS protected function ensureWritableBytes(len:uint):void{ - if (_position + len > ba.byteLength) { + if (_position + len > _len) { setBufferSize( _position + len ); } } @@ -998,7 +1003,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput private function mergeInToArrayBuffer(offset:uint, newBytes:Uint8Array):uint { var newContentLength:uint = newBytes.length; var dest:Uint8Array; - if (offset + newContentLength > ba.byteLength) { + if (offset + newContentLength > _len) { dest = new Uint8Array(offset + newContentLength); dest.set(new Uint8Array(ba, 0, offset)); dest.set(newBytes, offset);
