Skip to content

Commit 6650158

Browse files
author
BRUNER Patrick
committed
LogBufferPool
1 parent 37d7a49 commit 6650158

3 files changed

Lines changed: 131 additions & 26 deletions

File tree

src/LogExpert.Core/Classes/Log/LogBuffer.cs

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Buffers;
2+
13
using ColumnizerLib;
24

35
using NLog;
@@ -15,7 +17,10 @@ public class LogBuffer
1517
private readonly List<long> _filePositions; // file position for every line
1618
#endif
1719

18-
private readonly List<LogLine> _lineList;
20+
private LogLine[] _lineArray;
21+
private int _lineArrayLength; // capacity of the rented array
22+
23+
//private readonly List<LogLine> _lineList;
1924

2025
private int MAX_LINES = 500;
2126

@@ -31,7 +36,10 @@ public LogBuffer (ILogFileInfo fileInfo, int maxLines)
3136
{
3237
FileInfo = fileInfo;
3338
MAX_LINES = maxLines;
34-
_lineList = new(MAX_LINES);
39+
//_lineList = new(MAX_LINES);
40+
41+
_lineArray = ArrayPool<LogLine>.Shared.Rent(maxLines);
42+
_lineArrayLength = _lineArray.Length;
3543
#if DEBUG
3644
_filePositions = new(MAX_LINES);
3745
#endif
@@ -81,23 +89,67 @@ public long Size
8189

8290
public void AddLine (LogLine lineMemory, long filePos)
8391
{
84-
_lineList.Add(lineMemory);
92+
//_lineList.Add(lineMemory);
93+
94+
if (LineCount < _lineArrayLength)
95+
{
96+
_lineArray[LineCount] = lineMemory;
97+
LineCount++;
98+
}
99+
#if DEBUG
100+
else
101+
{
102+
_logger.Error("AddLine overflow: LineCount={0} >= _lineArrayLength={1}", LineCount, _lineArrayLength);
103+
}
104+
#endif
105+
85106
#if DEBUG
86107
_filePositions.Add(filePos);
87108
#endif
88-
LineCount++;
89109
IsDisposed = false;
90110
}
91111

92112
public void ClearLines ()
93113
{
94-
_lineList.Clear();
114+
Array.Clear(_lineArray, 0, LineCount);
115+
//_lineList.Clear();
116+
LineCount = 0;
117+
}
118+
119+
/// <summary>
120+
/// Prepares the buffer for reuse from the pool.
121+
/// </summary>
122+
public void Reinitialise (ILogFileInfo fileInfo, int maxLines)
123+
{
124+
FileInfo = fileInfo;
125+
MAX_LINES = maxLines;
126+
StartLine = 0;
127+
StartPos = 0;
128+
Size = 0;
95129
LineCount = 0;
130+
DroppedLinesCount = 0;
131+
PrevBuffersDroppedLinesSum = 0;
132+
IsDisposed = false;
133+
_lineArray = ArrayPool<LogLine>.Shared.Rent(maxLines);
134+
_lineArrayLength = _lineArray.Length;
135+
#if DEBUG
136+
_filePositions.Clear();
137+
DisposeCount = 0;
138+
#endif
96139
}
97140

98141
public void DisposeContent ()
99142
{
100-
_lineList.Clear();
143+
//_lineList.Clear();
144+
145+
if (_lineArray != null)
146+
{
147+
Array.Clear(_lineArray, 0, LineCount);
148+
ArrayPool<LogLine>.Shared.Return(_lineArray);
149+
_lineArray = null;
150+
LineCount = 0;
151+
}
152+
101153
IsDisposed = true;
102154
#if DEBUG
103155
DisposeCount++;
@@ -106,9 +158,12 @@ public void DisposeContent ()
106158

107159
public LogLine? GetLineMemoryOfBlock (int num)
108160
{
109-
return num < _lineList.Count && num >= 0
110-
? _lineList[num]
111-
: null;
161+
return num < LineCount && num >= 0
162+
? _lineArray[num]
163+
: null;
164+
//return num < _lineList.Count && num >= 0
165+
// ? _lineList[num]
166+
// : null;
112167
}
113168

114169
/// <summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Concurrent;
2+
3+
using ColumnizerLib;
4+
5+
namespace LogExpert.Core.Classes.Log;
6+
7+
public sealed class LogBufferPool (int maxSize)
8+
{
9+
private readonly ConcurrentBag<LogBuffer> _pool = [];
10+
private readonly int _maxSize = maxSize;
11+
12+
public LogBuffer Rent (ILogFileInfo fileInfo, int maxLines)
13+
{
14+
if (_pool.TryTake(out var buffer))
15+
{
16+
buffer.Reinitialise(fileInfo, maxLines);
17+
return buffer;
18+
}
19+
20+
return new LogBuffer(fileInfo, maxLines);
21+
}
22+
23+
public void Return (LogBuffer buffer)
24+
{
25+
ArgumentNullException.ThrowIfNull(buffer);
26+
27+
buffer.DisposeContent();
28+
if (_pool.Count < _maxSize)
29+
{
30+
_pool.Add(buffer);
31+
}
32+
}
33+
}

src/LogExpert.Core/Classes/Log/LogfileReader.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public partial class LogfileReader : IAutoLogLineMemoryColumnizerCallback, IDisp
3030
private readonly ReaderType _readerType;
3131
private readonly int _maximumLineLength;
3232

33+
private readonly LogBufferPool _bufferPool;
3334
private readonly Lock _logBufferLock = new();
3435
private readonly ReaderWriterLockSlim _bufferListLock = new(LockRecursionPolicy.SupportsRecursion);
3536

@@ -101,6 +102,8 @@ private LogfileReader (string[] fileNames, EncodingOptions encodingOptions, bool
101102
_pluginRegistry = pluginRegistry;
102103
_disposed = false;
103104

105+
_bufferPool = new LogBufferPool(_max_buffers * 2);
106+
104107
InitLruBuffers();
105108

106109
ILogFileInfo fileInfo = null;
@@ -1300,11 +1303,15 @@ private void ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start
13001303
{
13011304
if (_bufferList.Count == 0)
13021305
{
1303-
logBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1304-
{
1305-
StartLine = startLine,
1306-
StartPos = filePos
1307-
};
1306+
logBuffer = _bufferPool.Rent(logFileInfo, _maxLinesPerBuffer);
1307+
logBuffer.StartLine = startLine;
1308+
logBuffer.StartPos = filePos;
1309+
// logBuffer.PrevBuffersDroppedLinesSum = droppedLines;
1310+
// logBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1311+
// {
1312+
// StartLine = startLine,
1313+
// StartPos = filePos
1314+
// };
13081315

13091316
UpgradeBufferlistLockToWriterLock();
13101317

@@ -1323,11 +1330,15 @@ private void ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start
13231330

13241331
if (!logBuffer.FileInfo.FullName.Equals(logFileInfo.FullName, StringComparison.Ordinal))
13251332
{
1326-
logBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1327-
{
1328-
StartLine = startLine,
1329-
StartPos = filePos
1330-
};
1333+
logBuffer = _bufferPool.Rent(logFileInfo, _maxLinesPerBuffer);
1334+
logBuffer.StartLine = startLine;
1335+
logBuffer.StartPos = filePos;
1336+
// logBuffer.PrevBuffersDroppedLinesSum = droppedLines;
1337+
// logBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1338+
// {
1339+
// StartLine = startLine,
1340+
// StartPos = filePos
1341+
// };
13311342

13321343
UpgradeBufferlistLockToWriterLock();
13331344

@@ -1408,12 +1419,17 @@ private void ReadToBufferList (ILogFileInfo logFileInfo, long filePos, int start
14081419
Monitor.Exit(logBuffer);
14091420
try
14101421
{
1411-
var newBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1412-
{
1413-
StartLine = lineNum,
1414-
StartPos = filePos,
1415-
PrevBuffersDroppedLinesSum = droppedLines
1416-
};
1422+
var newBuffer = _bufferPool.Rent(logFileInfo, _maxLinesPerBuffer);
1423+
newBuffer.StartLine = lineNum;
1424+
newBuffer.StartPos = filePos;
1425+
newBuffer.PrevBuffersDroppedLinesSum = droppedLines;
1426+
1427+
//var newBuffer = new LogBuffer(logFileInfo, _maxLinesPerBuffer)
1428+
//{
1429+
// StartLine = lineNum,
1430+
// StartPos = filePos,
1431+
// PrevBuffersDroppedLinesSum = droppedLines
1432+
//};
14171433

14181434
AcquireBufferListWriterLock();
14191435

@@ -1564,7 +1580,8 @@ private void GarbageCollectLruCache ()
15641580
try
15651581
{
15661582
removed.LogBuffer.AcquireContentLock(ref lockTaken);
1567-
removed.LogBuffer.DisposeContent();
1583+
//removed.LogBuffer.DisposeContent();
1584+
_bufferPool.Return(removed.LogBuffer);
15681585
}
15691586
finally
15701587
{

0 commit comments

Comments
 (0)