Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[♻️Housekeeping] Remove BufferPool class and use .net implementation #2508

Merged
merged 11 commits into from
Jun 19, 2024
6 changes: 5 additions & 1 deletion LiteDB/Engine/Disk/DiskService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Threading;
Expand All @@ -25,6 +26,8 @@ internal class DiskService : IDisposable
private long _dataLength;
private long _logLength;

private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;

public DiskService(
EngineSettings settings,
EngineState state,
Expand Down Expand Up @@ -222,11 +225,12 @@ internal void MarkAsInvalidState()
{
using (var stream = _dataFactory.GetStream(true, true))
{
var buffer = new byte[PAGE_SIZE];
var buffer = bufferPool.Rent(PAGE_SIZE);
stream.Read(buffer, 0, PAGE_SIZE);
buffer[HeaderPage.P_INVALID_DATAFILE_STATE] = 1;
stream.Position = 0;
stream.Write(buffer, 0, PAGE_SIZE);
bufferPool.Return(buffer);
}
});
}
Expand Down
39 changes: 0 additions & 39 deletions LiteDB/Engine/Disk/Serializer/BufferPool.cs

This file was deleted.

26 changes: 14 additions & 12 deletions LiteDB/Engine/Disk/Serializer/BufferReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static LiteDB.Constants;

namespace LiteDB.Engine
Expand All @@ -20,6 +20,8 @@ internal class BufferReader : IDisposable

private bool _isEOF = false;

private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;

/// <summary>
/// Current global cursor position
/// </summary>
Expand Down Expand Up @@ -102,10 +104,10 @@ public int Read(byte[] buffer, int offset, int count)
// fill buffer
if (buffer != null)
{
Buffer.BlockCopy(_current.Array,
_current.Offset + _currentPosition,
buffer,
offset + bufferPosition,
Buffer.BlockCopy(_current.Array,
_current.Offset + _currentPosition,
buffer,
offset + bufferPosition,
bytesToCopy);
}

Expand Down Expand Up @@ -161,13 +163,13 @@ public string ReadString(int count)
else
{
// rent a buffer to be re-usable
var buffer = BufferPool.Rent(count);
var buffer = bufferPool.Rent(count);

this.Read(buffer, 0, count);

value = StringEncoding.UTF8.GetString(buffer, 0, count);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}

return value;
Expand Down Expand Up @@ -251,13 +253,13 @@ private T ReadNumber<T>(Func<byte[], int, T> convert, int size)
}
else
{
var buffer = BufferPool.Rent(size);
var buffer = bufferPool.Rent(size);

this.Read(buffer, 0, size);

value = convert(buffer, 0);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}

return value;
Expand Down Expand Up @@ -328,13 +330,13 @@ public ObjectId ReadObjectId()
}
else
{
var buffer = BufferPool.Rent(12);
var buffer = bufferPool.Rent(12);

this.Read(buffer, 0, 12);

value = new ObjectId(buffer, 0);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}

return value;
Expand Down Expand Up @@ -393,7 +395,7 @@ public BsonValue ReadIndexKey()
case BsonType.Int64: return this.ReadInt64();
case BsonType.Double: return this.ReadDouble();
case BsonType.Decimal: return this.ReadDecimal();

// Use +1 byte only for length
case BsonType.String: return this.ReadString(this.ReadByte());

Expand Down
25 changes: 14 additions & 11 deletions LiteDB/Engine/Disk/Serializer/BufferWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;
using static LiteDB.Constants;
Expand All @@ -18,6 +19,8 @@ internal class BufferWriter : IDisposable

private bool _isEOF = false;

private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;

/// <summary>
/// Current global cursor position
/// </summary>
Expand Down Expand Up @@ -98,10 +101,10 @@ public int Write(byte[] buffer, int offset, int count)
// fill buffer
if (buffer != null)
{
Buffer.BlockCopy(buffer,
Buffer.BlockCopy(buffer,
offset + bufferPosition,
_current.Array,
_current.Offset + _currentPosition,
_current.Offset + _currentPosition,
bytesToCopy);
}

Expand Down Expand Up @@ -133,7 +136,7 @@ public int Write(byte[] buffer, int offset, int count)
/// </summary>
public void Consume()
{
if(_source != null)
if (_source != null)
{
while (_source.MoveNext())
{
Expand Down Expand Up @@ -166,7 +169,7 @@ public void WriteCString(string value)
}
else
{
var buffer = BufferPool.Rent(bytesCount);
var buffer = bufferPool.Rent(bytesCount);

StringEncoding.UTF8.GetBytes(value, 0, value.Length, buffer, 0);

Expand All @@ -176,7 +179,7 @@ public void WriteCString(string value)

this.MoveForward(1);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}
}

Expand All @@ -202,13 +205,13 @@ public void WriteString(string value, bool specs)
else
{
// rent a buffer to be re-usable
var buffer = BufferPool.Rent(count);
var buffer = bufferPool.Rent(count);

StringEncoding.UTF8.GetBytes(value, 0, value.Length, buffer, 0);

this.Write(buffer, 0, count);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}

if (specs)
Expand All @@ -231,13 +234,13 @@ private void WriteNumber<T>(T value, Action<T, byte[], int> toBytes, int size)
}
else
{
var buffer = BufferPool.Rent(size);
var buffer = bufferPool.Rent(size);

toBytes(value, buffer, 0);

this.Write(buffer, 0, size);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}
}

Expand Down Expand Up @@ -293,13 +296,13 @@ public void Write(ObjectId value)
}
else
{
var buffer = BufferPool.Rent(12);
var buffer = bufferPool.Rent(12);

value.ToByteArray(buffer, 0);

this.Write(buffer, 0, 12);

BufferPool.Return(buffer);
bufferPool.Return(buffer);
}
}

Expand Down
15 changes: 12 additions & 3 deletions LiteDB/Engine/Disk/Streams/AesStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -24,6 +25,8 @@ public class AesStream : Stream

private static readonly byte[] _emptyContent = new byte[PAGE_SIZE - 1 - 16]; // 1 for aes indicator + 16 for salt

private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;

public byte[] Salt { get; }

public override bool CanRead => _stream.CanRead;
Expand Down Expand Up @@ -52,6 +55,9 @@ public AesStream(string password, Stream stream)
// start stream from zero position
_stream.Position = 0;

var checkBuffer = bufferPool.Rent(32);
var msBuffer = bufferPool.Rent(16);

try
{
// new file? create new salt
Expand Down Expand Up @@ -104,7 +110,6 @@ public AesStream(string password, Stream stream)
// set stream to password checking
_stream.Position = 32;

var checkBuffer = new byte[32];

if (!isNew)
{
Expand Down Expand Up @@ -140,8 +145,7 @@ public AesStream(string password, Stream stream)

_stream.Position = PAGE_SIZE;
_stream.FlushToDisk();

using (var ms = new MemoryStream(new byte[16]))
using (var ms = new MemoryStream(msBuffer))
using (var tempStream = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read))
{
tempStream.Read(_decryptedZeroes, 0, _decryptedZeroes.Length);
Expand All @@ -153,6 +157,11 @@ public AesStream(string password, Stream stream)

throw;
}
finally
{
bufferPool.Return(msBuffer);
bufferPool.Return(checkBuffer);
}
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions LiteDB/Engine/Engine/Upgrade.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -9,6 +10,9 @@ namespace LiteDB.Engine
{
public partial class LiteEngine
{

private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;

/// <summary>
/// If Upgrade=true, run this before open Disk service
/// </summary>
Expand All @@ -19,20 +23,21 @@ private void TryUpgrade()
// if file not exists, just exit
if (!File.Exists(filename)) return;

var buffer = bufferPool.Rent(1024);
using (var stream = new FileStream(
_settings.Filename,
FileMode.Open,
FileAccess.Read,
FileShare.Read, 1024))
{
var buffer = new byte[1024];


stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);

if (FileReaderV7.IsVersion(buffer) == false) return;
}

bufferPool.Return(buffer);
// run rebuild process
this.Recovery(_settings.Collation);
}
Expand Down
11 changes: 8 additions & 3 deletions LiteDB/Engine/FileReader/FileReaderV7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class FileReaderV7 : IFileReader

private byte[] _buffer = new byte[V7_PAGE_SIZE];
private bool _disposedValue;
private static readonly byte[] arrayByteEmpty = new byte[0];

public IDictionary<string, BsonValue> GetPragmas() => new Dictionary<string, BsonValue>()
{
Expand Down Expand Up @@ -387,7 +388,7 @@ private byte[] ReadExtendData(uint extendPageID)
{
var page = this.ReadPage(extendPageID);

if (page["pageType"].AsInt32 != 5) return new byte[0];
if (page["pageType"].AsInt32 != 5) return arrayByteEmpty;

buffer.Write(page["data"].AsBinary, 0, page["itemCount"].AsInt32);

Expand All @@ -403,10 +404,14 @@ private byte[] ReadExtendData(uint extendPageID)
/// </summary>
private HashSet<uint> VisitIndexPages(uint startPageID)
{
var toVisit = new HashSet<uint>(new uint[] { startPageID });
var toVisit = new HashSet<uint>
{
startPageID
};

var visited = new HashSet<uint>();

while(toVisit.Count > 0)
while (toVisit.Count > 0)
{
var indexPageID = toVisit.First();

Expand Down
Loading