Skip to content

Commit

Permalink
use arrayPool where is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Jun 12, 2024
1 parent 80bf882 commit b90b106
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
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
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

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
{

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
4 changes: 3 additions & 1 deletion LiteDB/Engine/Structures/IndexNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal class IndexNode
private readonly IndexPage _page;
private readonly BufferSlice _segment;

private static readonly byte[] arrayByteEmpty = new byte[0];

/// <summary>
/// Position of this node inside a IndexPage (not persist)
/// </summary>
Expand Down Expand Up @@ -164,7 +166,7 @@ public IndexNode(IndexPage page, byte index, BufferSlice segment, byte slot, byt
public IndexNode(BsonDocument doc)
{
_page = null;
_segment = new BufferSlice(new byte[0], 0, 0);
_segment = new BufferSlice(arrayByteEmpty, 0, 0);

this.Position = new PageAddress(0, 0);
this.Slot = 0;
Expand Down

0 comments on commit b90b106

Please sign in to comment.