我面临着从FileStream读取的优化问题,我想构建一个可以读取结构头的函数,然后一旦完成就读取它遇到的所有引用。这是我可以用回调做的事情吗?
目前,我将引用查找项存储到队列中,并将该队列与找到的每个新项连接(如果是单个项,则使用队列函数)。我返回此方法的结果并将其传递给另一个方法,该方法读取所有引用对象的标头并将它们分配给对象。这对于perf来说目前非常糟糕,但仍然比我之前的随机读数更好。
所有参考对象在FileStream中以树形结构的线性顺序存在
public override Queue<BlamPointer> ReadFields(BinaryReader binaryReader)
{
var blamPointers = new Queue<BlamPointer>(base.ReadFields(binaryReader));
type = (Type) binaryReader.ReadInt16();
format = (Format) binaryReader.ReadInt16();
usage = (Usage) binaryReader.ReadInt16();
flags = (Flags) binaryReader.ReadInt16();
detailFadeFactor01 = binaryReader.ReadSingle();
sharpenAmount01 = binaryReader.ReadSingle();
bumpHeightRepeats = binaryReader.ReadSingle();
spriteSize = (SpriteSize) binaryReader.ReadInt16();
[....]
meleeAimAssist = new MeleeAimAssistStructBlock();
blamPointers = new Queue<BlamPointer>(blamPointers.Concat(meleeAimAssist.ReadFields(binaryReader)));
blamPointers.Enqueue(ReadBlockArrayPointer<BitmapGroupSequenceBlock>(binaryReader));
blamPointers.Enqueue(ReadBlockArrayPointer<BitmapDataBlock>(binaryReader));
return blamPointers;
}
这非常糟糕,因为它会产生大量的GC足迹。
public virtual void Read(BinaryReader binaryReader)
{
var pointers = ReadFields(binaryReader);
ReadPointers(binaryReader, pointers);
}
public override void ReadPointers(BinaryReader binaryReader, Queue<BlamPointer> blamPointers)
{
base.ReadPointers(binaryReader, blamPointers);
meleeAimAssist.ReadPointers(binaryReader, blamPointers);
sequences = ReadBlockArrayData<BitmapGroupSequenceBlock>(binaryReader, blamPointers.Dequeue());
bitmaps = ReadBlockArrayData<BitmapDataBlock>(binaryReader, blamPointers.Dequeue());
}
我想知道是否有办法产生参考&#39; blamPointer&#39;函数来自读取&#39;指针&#39;的方法,然后返回它以完成读取参考对象 - 使用回调函数或其他东西?