fix(core): Fixes BufferWriter Seek and removes World.SaveBuffers (#359)

- [X] Fixes an issue in BufferWriter where if SeekOrigin.End is used, it will yield the wrong index.
- [X] Changes BinaryFileWriter to dispose pattern
- [X] Removes World.SaveBuffers. They were dangerous and should be done in-line while the world is loading, even if it is slower
- [X] Fixes BufferReader Seek too
- [X] Changes BufferWriter to use uninitialized arrays to speed up resizing
- [X] Changes World loading so it doesn't buffer the whole file, even if it makes things slower. It lowers allocations/fragmentation over all which is good.
- [X] Changes World Loading to use uninitialized arrays and directly saves them to the SaveBuffer which eliminates having to open/load the files _again_ which is dangerous. Also looping through items while the world is running is dangerous since async references can and will cause exceptions.

Bumps release version
This commit is contained in:
Kamron Batman 2020-12-24 14:21:17 -08:00 committed by GitHub
parent 4ddb3de026
commit c2143584bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 178 additions and 134 deletions

View file

@ -13,36 +13,38 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
namespace Server
{
public class BinaryFileWriter : BufferWriter
public class BinaryFileWriter : BufferWriter, IDisposable
{
private readonly Stream m_File;
private long m_Position;
private readonly Stream _file;
private long _position;
public BinaryFileWriter(string filename, bool prefixStr) : base(prefixStr) =>
m_File = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
public BinaryFileWriter(string filename, bool prefixStr) :
this(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None), prefixStr)
{}
public BinaryFileWriter(Stream stream, bool prefixStr) : base(prefixStr)
{
m_File = stream;
m_Position = m_File.Position;
_file = stream;
_position = _file.Position;
}
public override long Position => m_Position + Index;
public override long Position => _position + Index;
protected override int BufferSize => 512;
protected override int BufferSize => 81920;
public override void Flush()
{
if (Index > 0)
{
m_Position += Index;
_position += Index;
m_File.Write(Buffer, 0, (int)Index);
_file.Write(Buffer, 0, (int)Index);
Index = 0;
}
}
@ -54,14 +56,19 @@ namespace Server
Flush();
}
m_File.Close();
_file.Close();
}
public override long Seek(long offset, SeekOrigin origin)
{
Flush();
return m_Position = m_File.Seek(offset, origin);
return _position = _file.Seek(offset, origin);
}
public void Dispose()
{
Close();
}
}
}

View file

@ -16,6 +16,7 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
@ -27,7 +28,11 @@ namespace Server
{
private readonly Encoding _encoding;
private byte[] _buffer;
public int Position { get; private set; }
private int _position;
public int Position => _position;
public byte[] Buffer => _buffer;
public BufferReader(byte[] buffer)
{
@ -35,6 +40,13 @@ namespace Server
_encoding = Utility.UTF8;
}
public void SwapBuffers(byte[] newBuffer, out byte[] oldBuffer)
{
oldBuffer = _buffer;
_buffer = newBuffer;
_position = 0;
}
public string ReadString()
{
if (!ReadBool())
@ -49,7 +61,7 @@ namespace Server
}
var s = _encoding.GetString(_buffer.AsSpan(Position, length));
Position += length;
_position += length;
return s;
}
@ -64,64 +76,64 @@ namespace Server
public long ReadLong()
{
var v = BinaryPrimitives.ReadInt64LittleEndian(_buffer.AsSpan(Position, 8));
Position += 8;
_position += 8;
return v;
}
public ulong ReadULong()
{
var v = BinaryPrimitives.ReadUInt64LittleEndian(_buffer.AsSpan(Position, 8));
Position += 8;
_position += 8;
return v;
}
public int ReadInt()
{
var v = BinaryPrimitives.ReadInt32LittleEndian(_buffer.AsSpan(Position, 4));
Position += 4;
_position += 4;
return v;
}
public uint ReadUInt()
{
var v = BinaryPrimitives.ReadUInt32LittleEndian(_buffer.AsSpan(Position, 4));
Position += 4;
_position += 4;
return v;
}
public short ReadShort()
{
var v = BinaryPrimitives.ReadInt16LittleEndian(_buffer.AsSpan(Position, 2));
Position += 2;
_position += 2;
return v;
}
public ushort ReadUShort()
{
var v = BinaryPrimitives.ReadUInt16LittleEndian(_buffer.AsSpan(Position, 2));
Position += 2;
_position += 2;
return v;
}
public double ReadDouble()
{
var v = BinaryPrimitives.ReadDoubleLittleEndian(_buffer.AsSpan(Position, 8));
Position += 8;
_position += 8;
return v;
}
public float ReadFloat()
{
var v = BinaryPrimitives.ReadSingleLittleEndian(_buffer.AsSpan(Position, 4));
Position += 4;
_position += 4;
return v;
}
public byte ReadByte() => _buffer[Position++];
public byte ReadByte() => _buffer[_position++];
public sbyte ReadSByte() => (sbyte)_buffer[Position++];
public sbyte ReadSByte() => (sbyte)_buffer[_position++];
public bool ReadBool() => _buffer[Position++] != 0;
public bool ReadBool() => _buffer[_position++] != 0;
public int ReadEncodedInt()
{
@ -217,18 +229,31 @@ namespace Server
}
_buffer.AsSpan(Position, length).CopyTo(buffer);
Position += length;
_position += length;
return length;
}
public virtual int Seek(int offset, SeekOrigin origin)
{
return origin switch
Debug.Assert(
origin != SeekOrigin.End || offset <= 0 && offset > -_buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.End"
);
Debug.Assert(
origin != SeekOrigin.Begin || offset >= 0 && offset < _buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.Begin"
);
Debug.Assert(
origin != SeekOrigin.Current || _position + offset >= 0 && _position + offset < _buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.Current"
);
return _position = Math.Max(0, origin switch
{
SeekOrigin.Current => Position += offset,
SeekOrigin.End => Position = _buffer.Length - offset,
_ => Position = offset // Begin
};
SeekOrigin.Current => _position + offset,
SeekOrigin.End => _buffer.Length + offset,
_ => offset // Begin
});
}
}
}

View file

@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
@ -26,8 +27,30 @@ namespace Server
{
private readonly Encoding m_Encoding;
private readonly bool m_PrefixStrings;
private long _bytesWritten;
private long _index;
protected long Index
{
get => _index;
set
{
if (value < 0 || value > _buffer.Length)
{
// If you are receiving this exception and your value is too large, you may need to use `Resize`
// If you are receiving this exception and your value is negative, you probably used Seek incorrectly.
throw new ArgumentOutOfRangeException(nameof(value));
}
_index = value;
if (value > _bytesWritten)
{
_bytesWritten = value;
}
}
}
protected long Index { get; set; }
private byte[] _buffer;
public BufferWriter(byte[] buffer, bool prefixStr)
@ -37,18 +60,15 @@ namespace Server
_buffer = buffer;
}
public BufferWriter(bool prefixStr)
public BufferWriter(bool prefixStr) : this(0, prefixStr)
{
m_PrefixStrings = prefixStr;
m_Encoding = Utility.UTF8;
_buffer = GC.AllocateUninitializedArray<byte>(BufferSize);
}
public BufferWriter(int count, bool prefixStr)
{
m_PrefixStrings = prefixStr;
m_Encoding = Utility.UTF8;
_buffer = GC.AllocateUninitializedArray<byte>(count);
_buffer = GC.AllocateUninitializedArray<byte>(count < 1 ? BufferSize : count);
}
public virtual long Position => Index;
@ -70,7 +90,16 @@ namespace Server
size = BufferSize;
}
Array.Resize(ref _buffer, size);
if (size < _buffer.Length)
{
_bytesWritten = size;
}
var newBuffer = GC.AllocateUninitializedArray<byte>(size);
_buffer.AsSpan(0, Math.Min(size, _buffer.Length)).CopyTo(newBuffer);
_buffer = newBuffer;
// Array.Resize(ref _buffer, size);
}
public virtual void Flush()
@ -82,15 +111,12 @@ namespace Server
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool FlushIfNeeded(int amount)
private void FlushIfNeeded(int amount)
{
if (Index + amount > _buffer.Length)
{
Flush();
return true;
}
return false;
}
public void Write(ReadOnlySpan<byte> bytes)
@ -113,12 +139,25 @@ namespace Server
public virtual long Seek(long offset, SeekOrigin origin)
{
return origin switch
Debug.Assert(
origin != SeekOrigin.End || offset <= 0 && offset > -_buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.End"
);
Debug.Assert(
origin != SeekOrigin.Begin || offset >= 0 && offset < _buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.Begin"
);
Debug.Assert(
origin != SeekOrigin.Current || Index + offset >= 0 && Index + offset < _buffer.Length,
"Attempting to seek to an invalid position using SeekOrigin.Current"
);
return Index = Math.Max(0, origin switch
{
SeekOrigin.Current => Index += offset,
SeekOrigin.End => Index = _buffer.Length - offset,
_ => Index = offset // Begin
};
SeekOrigin.Current => Index + offset,
SeekOrigin.End => _bytesWritten + offset,
_ => offset // Begin
});
}
public void WriteEncodedInt(int value)