I have a binary file specification that describes a packetized data structure. Each data packet has a two-byte sync pattern, so scanning for the beginning of a packet is possible, using a BinaryReader and FileStream combination:
while(!reader.EndOfFile)
{
// Check for sync pattern.
if (reader.ReadUInt16() != 0xEB25)
{
// Move to next byte.
reader.BaseStream.Seek(-1, SeekOrigin.Current);
continue;
}
// If we got here, a sync pattern was found.
}
This process works perfectly fine in the forward direction, but similar code scanning in the reverse direction is at least two orders of magnitude slower:
while(!reader.BeginningOfFile)
{
// Check for sync pattern.
if (reader.ReadUInt16() != 0xEB25)
{
// Move to previous byte.
reader.BaseStream.Seek(-3, SeekOrigin.Current);
continue;
}
// If we got here, a sync pattern was found.
}
I’ve tried a few workarounds, like moving back by an arbitrary amount (currently 1 megabyte) and scanning forward, but it’s becoming clear that what I really need is a BinaryReader or FileStream that is modified to have adequate performance characteristics when reading in both forward and reverse directions.
I already have a FastFileStream which improves forward read performance by subclassing an ordinary FileStream and caching the Position and Length properties (it also provides the BeginningOfFile and EndOfFile properties). That’s what drives the reader variable in the code above.
Is there something similar I could do to improve reverse reading performance, perhaps by incorporating a MemoryStream as a buffer?
L.B mentioned in a comment to use a Memory Mapped file, you may be impressed with the performance.
Please try something like this: