下面是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复制的事情吗?
发布于 2011-11-15 16:49:33
不确定ByteArray是否可以优化,只要它是一个原生闪存类。我猜你可以尝试一些第三方库/框架,比如Fluorine或Alchemy来调用可能更快的C++算法。
https://stackoverflow.com/questions/8117989
复制相似问题