Skip to content

Commit

Permalink
Remove BufferPool class and use .net implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Jun 12, 2024
1 parent e9cff85 commit 2e720ac
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 60 deletions.
39 changes: 0 additions & 39 deletions LiteDB/Engine/Disk/Serializer/BufferPool.cs

This file was deleted.

15 changes: 9 additions & 6 deletions LiteDB/Engine/Disk/Serializer/BufferReader.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.Text;
Expand All @@ -20,6 +21,8 @@ internal class BufferReader : IDisposable

private bool _isEOF = false;

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

/// <summary>
/// Current global cursor position
/// </summary>
Expand Down Expand Up @@ -161,13 +164,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 +254,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 +331,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
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;

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
7 changes: 5 additions & 2 deletions LiteDB/Engine/Sort/SortContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -25,6 +26,8 @@ internal class SortContainer : IDisposable
private int _readPosition = 0;

private BufferReader _reader = null;

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

/// <summary>
/// Returns if current container has no more items to read
Expand Down Expand Up @@ -119,7 +122,7 @@ public bool MoveNext()
/// </summary>
private IEnumerable<BufferSlice> GetSourceFromStream(Stream stream)
{
var bytes = BufferPool.Rent(PAGE_SIZE);
var bytes = bufferPool.Rent(PAGE_SIZE);
var buffer = new BufferSlice(bytes, 0, PAGE_SIZE);

while (_readPosition < _size)
Expand All @@ -133,7 +136,7 @@ private IEnumerable<BufferSlice> GetSourceFromStream(Stream stream)
yield return buffer;
}

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

public void Dispose()
Expand Down
7 changes: 5 additions & 2 deletions LiteDB/Engine/Sort/SortService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -29,6 +30,8 @@ internal class SortService : IDisposable
private readonly EnginePragmas _pragmas;
private readonly BufferSlice _buffer;
private readonly Lazy<Stream> _reader;

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

/// <summary>
/// Get how many documents was inserted by Insert method
Expand All @@ -49,7 +52,7 @@ public SortService(SortDisk disk, int order, EnginePragmas pragmas)

_reader = new Lazy<Stream>(() => _disk.GetReader());

var bytes = BufferPool.Rent(disk.ContainerSize);
var bytes = bufferPool.Rent(disk.ContainerSize);

_buffer = new BufferSlice(bytes, 0, _containerSize);
}
Expand All @@ -69,7 +72,7 @@ public void Dispose()
}

// return array buffer into pool
BufferPool.Return(_buffer.Array);
bufferPool.Return(_buffer.Array);

// return open strem into disk
if (_reader.IsValueCreated)
Expand Down
4 changes: 4 additions & 0 deletions LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>

<!-- End References -->

Expand Down

0 comments on commit 2e720ac

Please sign in to comment.