优化ByteArray副本

时间:2011-11-14 06:17:55

标签: flash actionscript-3 flex optimization bytearray

以下是flashlight-VNC库的代码片段,它解压缩(使用ByteArray.inflate)传入的字节流。

package com.flashlight.zlib
{

    import com.flashlight.utils.TimeTracker;

    import flash.utils.ByteArray;

    import mx.logging.ILogger;
    import mx.logging.Log;

    public class Inflater {
        private var lastDeflate:ByteArray;

        public function uncompress(compressedData:ByteArray):ByteArray {
            var uncompressedData:ByteArray = new ByteArray();
            var dataOffset:int = lastDeflate ? 0 : 2;

            TimeTracker.startTimer("TightEncoding[CopyCompresion]");
            if (lastDeflate) {
                var dictionarySize:int = lastDeflate.length > 32768 ? 32768 : lastDeflate.length;
                uncompressedData.writeByte(0x00);
                uncompressedData.writeByte(dictionarySize );
                uncompressedData.writeByte(dictionarySize >> 8);
                uncompressedData.writeByte(~dictionarySize);
                uncompressedData.writeByte((~dictionarySize) >> 8 );
                uncompressedData.writeBytes(lastDeflate,lastDeflate.length - dictionarySize, dictionarySize);
            }
            uncompressedData.writeBytes(compressedData,dataOffset,compressedData.length-dataOffset);
            TimeTracker.stopTimer("TightEncoding[CopyCompresion]");
            uncompressedData.writeByte(0x01);
            uncompressedData.writeUnsignedInt(0x0000FFFF);

            TimeTracker.startTimer("TightEncoding[Realdecompress]");
            uncompressedData.inflate(); 
            TimeTracker.stopTimer("TightEncoding[Realdecompress]");

            lastDeflate = uncompressedData;
            uncompressedData.position = dictionarySize;

            return uncompressedData;
        }

    }
}

来自服务器的压缩数据源源不断,Inflater类会逐块膨胀。 inflate()和writeBytes()方法一起占用了这个类的99%的时间。 inflate()已经是本机调用。

如何优化writeBytes调用?我们可以跳过它,以其他方式重新编写代码,还是有更好的方法来做ByteArray复制的事情?

1 个答案:

答案 0 :(得分:0)

不确定是否可以优化ByteArray,只要它是本机Flash类。我猜你可以尝试一些第三方库/框架,比如Fluorine或Alchemy来调用更快的C ++算法。