Skip to content

Commit 7051b8d

Browse files
committed
update
1 parent 33fa73e commit 7051b8d

4 files changed

Lines changed: 221 additions & 107 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace LogExpert.Core.Classes.Log;
1010

11-
public class PositionAwareStreamReaderPipeline : LogStreamReaderBase, ILogStreamReaderSpan
11+
public class PositionAwareStreamReaderPipeline : LogStreamReaderBase, ILogStreamReaderMemory
1212
{
1313
private const int DEFAULT_BYTE_BUFFER_SIZE = 64 * 1024; // 64 KB
1414
private const int MINIMUM_READ_AHEAD_SIZE = 4 * 1024; // 4 KB
@@ -416,7 +416,8 @@ private void EnqueueLine (LineSegment segment)
416416
var remaining = localCharsInBuffer - searchIndex;
417417
if (remaining > 0 && searchIndex > 0)
418418
{
419-
Array.Copy(charBuffer, searchIndex, charBuffer, 0, remaining);
419+
charBuffer.AsSpan(searchIndex, remaining).CopyTo(charBuffer.AsSpan(0, remaining));
420+
//Array.Copy(charBuffer, searchIndex, charBuffer, 0, remaining);
420421
}
421422

422423
return (remaining, localByteOffset);
@@ -446,7 +447,8 @@ private void EnqueueLine (LineSegment segment)
446447
var remaining = charsInBuffer - searchIndex;
447448
if (remaining > 0 && searchIndex > 0)
448449
{
449-
Array.Copy(charBuffer, searchIndex, charBuffer, 0, remaining);
450+
charBuffer.AsSpan(searchIndex, remaining).CopyTo(charBuffer.AsSpan(0, remaining));
451+
//Array.Copy(charBuffer, searchIndex, charBuffer, 0, remaining);
450452
}
451453

452454
charsInBuffer = remaining;
@@ -580,7 +582,8 @@ private LineSegment CreateSegment (
580582
// Copy line content (excluding newline)
581583
if (logicalLength > 0)
582584
{
583-
Array.Copy(source, start, buffer, 0, logicalLength);
585+
source.AsSpan(start, logicalLength).CopyTo(buffer.AsSpan(0, logicalLength));
586+
//Array.Copy(source, start, buffer, 0, logicalLength);
584587
}
585588

586589
return new LineSegment(buffer, logicalLength, byteOffset, byteLength, truncated, false);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LogExpert.Core.Interface;
2+
3+
public interface ILogStreamReaderMemory : ILogStreamReader
4+
{
5+
6+
/// <summary>
7+
/// Attempts to read the next line from the stream.
8+
/// </summary>
9+
/// <param name="lineSpan">
10+
/// When this method returns <c>true</c>, contains a <see cref="ReadOnlyMemory{Char}"/> representing the next line read from the stream.
11+
/// The memory is only valid until the next call to <see cref="TryReadLine"/> or until <see cref="ReturnMemory"/> is called.
12+
/// </param>
13+
/// <returns>
14+
/// <c>true</c> if a line was successfully read; <c>false</c> if the end of the stream has been reached or no more lines are available.
15+
/// </returns>
16+
/// <remarks>
17+
/// The returned memory is only valid until the next call to <see cref="TryReadLine"/> or until <see cref="ReturnMemory"/> is called.
18+
/// This method is not guaranteed to be thread-safe; concurrent access should be synchronized externally.
19+
/// </remarks>
20+
bool TryReadLine (out ReadOnlyMemory<char> lineSpan);
21+
22+
/// <summary>
23+
/// Returns the memory buffer previously obtained from <see cref="TryReadLine"/> to the underlying pool or resource manager.
24+
/// </summary>
25+
/// <param name="memory">
26+
/// The <see cref="ReadOnlyMemory{Char}"/> instance previously obtained from <see cref="TryReadLine"/>.
27+
/// </param>
28+
/// <remarks>
29+
/// Call this method when you are done processing the memory returned by <see cref="TryReadLine"/> to avoid memory leaks or resource retention.
30+
/// Failing to call this method may result in increased memory usage.
31+
/// It is safe to call this method multiple times for the same memory, but only the first call will have an effect.
32+
/// </remarks>
33+
void ReturnMemory (ReadOnlyMemory<char> memory);
34+
}

src/LogExpert.Core/Interface/ILogStreamReaderSpan.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public interface ILogStreamReaderSpan : ILogStreamReader
66
/// <summary>
77
/// Attempts to read the next line from the stream.
88
/// </summary>
9-
/// <param name="lineMemory">
10-
/// When this method returns <c>true</c>, contains a <see cref="ReadOnlyMemory{Char}"/> representing the next line read from the stream.
9+
/// <param name="lineSpan">
10+
/// When this method returns <c>true</c>, contains a <see cref="ReadOnlySpan{Char}"/> representing the next line read from the stream.
1111
/// The memory is only valid until the next call to <see cref="TryReadLine"/> or until <see cref="ReturnMemory"/> is called.
1212
/// </param>
1313
/// <returns>
@@ -17,18 +17,18 @@ public interface ILogStreamReaderSpan : ILogStreamReader
1717
/// The returned memory is only valid until the next call to <see cref="TryReadLine"/> or until <see cref="ReturnMemory"/> is called.
1818
/// This method is not guaranteed to be thread-safe; concurrent access should be synchronized externally.
1919
/// </remarks>
20-
bool TryReadLine (out ReadOnlyMemory<char> lineMemory);
20+
bool TryReadLine (out ReadOnlySpan<char> lineSpan);
2121

2222
/// <summary>
2323
/// Returns the memory buffer previously obtained from <see cref="TryReadLine"/> to the underlying pool or resource manager.
2424
/// </summary>
2525
/// <param name="memory">
26-
/// The <see cref="ReadOnlyMemory{Char}"/> instance previously obtained from <see cref="TryReadLine"/>.
26+
/// The <see cref="ReadOnlySpan{Char}"/> instance previously obtained from <see cref="TryReadLine"/>.
2727
/// </param>
2828
/// <remarks>
2929
/// Call this method when you are done processing the memory returned by <see cref="TryReadLine"/> to avoid memory leaks or resource retention.
3030
/// Failing to call this method may result in increased memory usage.
3131
/// It is safe to call this method multiple times for the same memory, but only the first call will have an effect.
3232
/// </remarks>
33-
void ReturnMemory (ReadOnlyMemory<char> memory);
33+
void ReturnMemory (ReadOnlySpan<char> memory);
3434
}

0 commit comments

Comments
 (0)