(ArcViewStream): restored bufferization.

apparently performance is improved compared to unbuffered mapped file
view reads.
This commit is contained in:
morkt 2017-04-02 14:41:25 +04:00
parent 89de867c2d
commit 1f45de7d35

View File

@ -28,6 +28,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using GameRes.Utility;
namespace GameRes namespace GameRes
{ {
@ -37,6 +38,11 @@ namespace GameRes
private readonly long m_start; private readonly long m_start;
private readonly long m_size; private readonly long m_size;
private long m_position; private long m_position;
private byte[] m_buffer;
private int m_buffer_pos; // read position within buffer
private int m_buffer_len; // length of bytes read in buffer
private const int DefaultBufferSize = 0x1000;
public string Name { get; set; } public string Name { get; set; }
public uint Signature { get { return ReadSignature(); } } public uint Signature { get { return ReadSignature(); } }
@ -48,11 +54,20 @@ namespace GameRes
public override long Length { get { return m_size; } } public override long Length { get { return m_size; } }
public override long Position public override long Position
{ {
get { return m_position; } get { return m_position + (m_buffer_pos - m_buffer_len); }
set { set {
if (value < 0) if (value < 0)
throw new ArgumentOutOfRangeException ("value", "Stream position is out of range."); throw new ArgumentOutOfRangeException ("value", "Stream position is out of range.");
m_position = value; var buffer_start = m_position - m_buffer_len;
if (m_buffer_pos != m_buffer_len && value >= buffer_start && value < m_position)
{
m_buffer_pos = (int)(value - buffer_start);
}
else
{
m_position = value;
m_buffer_pos = m_buffer_len = 0;
}
} }
} }
@ -117,24 +132,61 @@ namespace GameRes
return new CowArray<byte> (m_header, 0, size); return new CowArray<byte> (m_header, 0, size);
} }
private void RefillBuffer ()
{
if (null == m_buffer)
m_buffer = new byte[DefaultBufferSize];
uint length = (uint)Math.Min (m_size - m_position, m_buffer.Length);
m_buffer_len = m_view.Read (m_start + m_position, m_buffer, 0, length);
m_position += m_buffer_len;
m_buffer_pos = 0;
}
private void FlushBuffer ()
{
if (m_buffer_len != 0)
{
m_position += m_buffer_pos - m_buffer_len;
m_buffer_pos = m_buffer_len = 0;
}
}
private void EnsureAvailable (int length) private void EnsureAvailable (int length)
{ {
if (m_position + length > m_size) if (m_buffer_pos + length > m_buffer_len)
throw new EndOfStreamException(); {
FlushBuffer();
if (m_position + length > m_size)
throw new EndOfStreamException();
RefillBuffer();
}
}
private int ReadFromBuffer (byte[] array, int offset, int count)
{
int available = Math.Min (m_buffer_len - m_buffer_pos, count);
if (available > 0)
{
Buffer.BlockCopy (m_buffer, m_buffer_pos, array, offset, available);
m_buffer_pos += available;
}
return available;
} }
public int PeekByte () public int PeekByte ()
{ {
if (m_position >= m_size) if (m_buffer_pos == m_buffer_len)
RefillBuffer();
if (m_buffer_pos == m_buffer_len)
return -1; return -1;
return m_view.ReadByte (m_start+m_position); return m_buffer[m_buffer_pos];
} }
public override int ReadByte () public override int ReadByte ()
{ {
int b = PeekByte(); int b = PeekByte();
if (-1 != b) if (-1 != b)
++m_position; ++m_buffer_pos;
return b; return b;
} }
@ -154,8 +206,8 @@ namespace GameRes
public short ReadInt16 () public short ReadInt16 ()
{ {
EnsureAvailable (2); EnsureAvailable (2);
var v = m_view.ReadInt16 (m_start+m_position); var v = m_buffer.ToInt16 (m_buffer_pos);
m_position += 2; m_buffer_pos += 2;
return v; return v;
} }
@ -167,17 +219,16 @@ namespace GameRes
public int ReadInt24 () public int ReadInt24 ()
{ {
EnsureAvailable (3); EnsureAvailable (3);
int v = m_view.ReadUInt16 (m_start+m_position); int v = m_buffer.ToInt24 (m_buffer_pos);
v |= m_view.ReadByte (m_start+m_position+2) << 16; m_buffer_pos += 3;
m_position += 3;
return v; return v;
} }
public int ReadInt32 () public int ReadInt32 ()
{ {
EnsureAvailable (4); EnsureAvailable (4);
var v = m_view.ReadInt32 (m_start+m_position); int v = m_buffer.ToInt32 (m_buffer_pos);
m_position += 4; m_buffer_pos += 4;
return v; return v;
} }
@ -189,8 +240,8 @@ namespace GameRes
public long ReadInt64 () public long ReadInt64 ()
{ {
EnsureAvailable (8); EnsureAvailable (8);
var v = m_view.ReadInt64 (m_start+m_position); var v = m_buffer.ToInt64 (m_buffer_pos);
m_position += 8; m_buffer_pos += 8;
return v; return v;
} }
@ -206,10 +257,37 @@ namespace GameRes
public string ReadCString (int length, Encoding enc) public string ReadCString (int length, Encoding enc)
{ {
uint string_length = (uint)Math.Min (length, m_size-m_position); if (m_buffer_pos == m_buffer_len && length <= DefaultBufferSize)
var str = m_view.ReadString (m_position, (uint)string_length, enc); RefillBuffer();
m_position += string_length; if (m_buffer_pos + length <= m_buffer_len)
return str; {
// whole string fit into buffer
var str = Binary.GetCString (m_buffer, m_buffer_pos, length, enc);
m_buffer_pos += length;
return str;
}
else if (length > DefaultBufferSize)
{
// requested string length is larger than internal buffer size
var string_buffer = ReadBytes (length);
return Binary.GetCString (string_buffer, 0, string_buffer.Length, enc);
}
else
{
int available = m_buffer_len - m_buffer_pos;
if (available > 0 && m_buffer_pos != 0)
Buffer.BlockCopy (m_buffer, m_buffer_pos, m_buffer, 0, available);
int count = (int)Math.Min (m_buffer.Length - available, m_size - m_position);
if (count > 0)
{
int read = m_view.Read (m_start + m_position, m_buffer, available, (uint)count);
m_position += read;
available += read;
}
m_buffer_len = available;
m_buffer_pos = Math.Min (length, m_buffer_len);
return Binary.GetCString (m_buffer, 0, m_buffer_pos, enc);
}
} }
public string ReadCString () public string ReadCString ()
@ -219,12 +297,27 @@ namespace GameRes
public string ReadCString (Encoding enc) public string ReadCString (Encoding enc)
{ {
// underlying view includes rest of the stream if (m_buffer_pos == m_buffer_len)
if (m_view.Offset <= m_position && m_view.Offset + m_view.Reserved >= m_start + m_size) RefillBuffer();
return ReadCStringUnsafe (enc); int available = m_buffer_len - m_buffer_pos;
if (0 == available)
return string.Empty;
var string_buf = new byte[0x20]; int zero = Array.IndexOf<byte> (m_buffer, 0, m_buffer_pos, available);
int size = 0; if (zero != -1)
{
// null byte found within buffer
var str = enc.GetString (m_buffer, m_buffer_pos, zero - m_buffer_pos);
m_buffer_pos = zero+1;
return str;
}
// underlying view includes whole stream
if (m_view.Offset <= m_start && m_view.Offset + m_view.Reserved >= m_start + m_size)
return ReadCStringUnsafe (enc, available);
var string_buf = new byte[Math.Max (0x20, available * 2)];
ReadFromBuffer (string_buf, 0, available);
int size = available;
for (;;) for (;;)
{ {
int b = ReadByte(); int b = ReadByte();
@ -242,6 +335,7 @@ namespace GameRes
private unsafe string ReadCStringUnsafe (Encoding enc, int skip_bytes = 0) private unsafe string ReadCStringUnsafe (Encoding enc, int skip_bytes = 0)
{ {
Debug.Assert (m_view.Offset + m_view.Reserved >= m_start + m_size); Debug.Assert (m_view.Offset + m_view.Reserved >= m_start + m_size);
FlushBuffer();
using (var ptr = m_view.GetPointer()) using (var ptr = m_view.GetPointer())
{ {
byte* s = ptr.Value + (m_start - m_view.Offset + m_position); byte* s = ptr.Value + (m_start - m_view.Offset + m_position);
@ -260,27 +354,35 @@ namespace GameRes
public byte[] ReadBytes (int count) public byte[] ReadBytes (int count)
{ {
if (0 == count || m_position >= m_size) if (m_buffer_pos + count <= m_buffer_len && m_buffer_len != 0)
{
var data = new CowArray<byte> (m_buffer, m_buffer_pos, count).ToArray();
m_buffer_pos += count;
return data;
}
var current_pos = Position;
if (0 == count || current_pos >= m_size)
return new byte[0]; return new byte[0];
var bytes = m_view.ReadBytes (m_start+m_position, (uint)Math.Min (count, m_size - m_position)); var bytes = m_view.ReadBytes (m_start+current_pos, (uint)Math.Min (count, m_size - current_pos));
m_position += bytes.Length; Position = current_pos + bytes.Length;
return bytes; return bytes;
} }
#region System.IO.Stream methods #region System.IO.Stream methods
public override void Flush() public override void Flush()
{ {
FlushBuffer();
} }
public override long Seek (long offset, SeekOrigin origin) public override long Seek (long offset, SeekOrigin origin)
{ {
switch (origin) switch (origin)
{ {
case SeekOrigin.Current: offset += m_position; break; case SeekOrigin.Current: offset += Position; break;
case SeekOrigin.End: offset += m_size; break; case SeekOrigin.End: offset += m_size; break;
} }
Position = offset; Position = offset;
return m_position; return offset;
} }
public override void SetLength (long length) public override void SetLength (long length)
@ -290,12 +392,26 @@ namespace GameRes
public override int Read (byte[] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
int read_from_buffer = ReadFromBuffer (buffer, offset, count);
offset += read_from_buffer;
count -= read_from_buffer;
if (0 == count || m_position >= m_size) if (0 == count || m_position >= m_size)
return 0; return read_from_buffer;
count = (int)Math.Min (count, m_size - m_position); if (count < DefaultBufferSize)
int read = m_view.Read (m_start + m_position, buffer, offset, (uint)count); {
m_position += read; RefillBuffer();
return read; count = Math.Min (count, m_buffer_len);
Buffer.BlockCopy (m_buffer, m_buffer_pos, buffer, offset, count);
m_buffer_pos += count;
return read_from_buffer + count;
}
else
{
uint view_count = (uint)Math.Min (count, m_size - m_position);
int read_from_view = m_view.Read (m_start + m_position, buffer, offset, view_count);
m_position += read_from_view;
return read_from_buffer + read_from_view;
}
} }
public override void Write (byte[] buffer, int offset, int count) public override void Write (byte[] buffer, int offset, int count)