Skip to content

Commit 552d00d

Browse files
committed
Remove BytePool
1 parent a3ee675 commit 552d00d

File tree

3 files changed

+49
-150
lines changed

3 files changed

+49
-150
lines changed

IPBanCore/Core/Utility/BytePool.cs

-104
This file was deleted.

IPBanCore/Core/Utility/ExtensionMethods.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,8 @@ public static IPAddress ToIPAddress(this UInt128 value)
701701
public static unsafe IPAddress ToIPAddressRaw(this UInt128 value)
702702
{
703703
byte* bytes = (byte*)&value;
704-
using var managedBytes = BytePool.Rent(16);
705-
for (int i = 0; i < 16; i++)
706-
{
707-
managedBytes[i] = bytes[i];
708-
}
709-
return new IPAddress(managedBytes.AsSpan());
704+
ReadOnlySpan<byte> spanBytes = new(bytes, 16);
705+
return new IPAddress(spanBytes);
710706
}
711707

712708
/// <summary>

IPBanCore/Core/Utility/LogFileScanner.cs

+47-40
Original file line numberDiff line numberDiff line change
@@ -504,57 +504,64 @@ private void ProcessFile(WatchedFile file, FileStream fs)
504504
fs.Position = file.LastPosition;
505505

506506
// fill up to 64K bytes
507-
using var bytes = BytePool.Rent(ushort.MaxValue);
508-
int read = fs.Read(bytes, 0, ushort.MaxValue);
507+
var bytes = ArrayPool<byte>.Shared.Rent(ushort.MaxValue);
508+
try
509+
{
510+
int read = fs.Read(bytes, 0, ushort.MaxValue);
509511

510-
// setup state
511-
int bytesEnd;
512-
bool foundNewLine = false;
512+
// setup state
513+
int bytesEnd;
514+
bool foundNewLine = false;
513515

514-
// find the last newline char
515-
for (bytesEnd = read - 1; bytesEnd >= 0; bytesEnd--)
516-
{
517-
if (bytes[bytesEnd] == '\n')
516+
// find the last newline char
517+
for (bytesEnd = read - 1; bytesEnd >= 0; bytesEnd--)
518518
{
519-
// take bytes up to and including the last newline char
520-
bytesEnd++;
521-
foundNewLine = true;
522-
break;
519+
if (bytes[bytesEnd] == '\n')
520+
{
521+
// take bytes up to and including the last newline char
522+
bytesEnd++;
523+
foundNewLine = true;
524+
break;
525+
}
523526
}
524-
}
525527

526-
// check for binary file
527-
if (!foundNewLine)
528-
{
529-
if (read > maxLineLength)
528+
// check for binary file
529+
if (!foundNewLine)
530530
{
531-
// max line length bytes without a new line
532-
file.IsBinaryFile = true;
533-
Logger.Warn($"Aborting parsing log file {file.FileName}, file may be a binary file");
531+
if (read > maxLineLength)
532+
{
533+
// max line length bytes without a new line
534+
file.IsBinaryFile = true;
535+
Logger.Warn($"Aborting parsing log file {file.FileName}, file may be a binary file");
536+
}
537+
// reset position try again on next cycle
538+
fs.Position = file.LastPosition;
539+
return;
534540
}
535-
// reset position try again on next cycle
536-
fs.Position = file.LastPosition;
537-
return;
538-
}
539541

540-
// if we found a newline, process all the text up until that newline
541-
if (foundNewLine)
542-
{
543-
try
544-
{
545-
// strip out all carriage returns and ensure string starts/ends with newlines
546-
string foundText = encoding.GetString(bytes, 0, bytesEnd).Trim().Replace("\r", string.Empty);
547-
string processText = "\n" + foundText + "\n";
548-
OnProcessText(processText);
549-
ProcessText?.Invoke(processText);
550-
}
551-
finally
542+
// if we found a newline, process all the text up until that newline
543+
if (foundNewLine)
552544
{
553-
// set file position for next processing
554-
fs.Position = file.LastPosition + bytesEnd;
555-
file.LastPosition = fs.Position;
545+
try
546+
{
547+
// strip out all carriage returns and ensure string starts/ends with newlines
548+
string foundText = encoding.GetString(bytes, 0, bytesEnd).Trim().Replace("\r", string.Empty);
549+
string processText = "\n" + foundText + "\n";
550+
OnProcessText(processText);
551+
ProcessText?.Invoke(processText);
552+
}
553+
finally
554+
{
555+
// set file position for next processing
556+
fs.Position = file.LastPosition + bytesEnd;
557+
file.LastPosition = fs.Position;
558+
}
556559
}
557560
}
561+
finally
562+
{
563+
ArrayPool<byte>.Shared.Return(bytes);
564+
}
558565
}
559566
}
560567
}

0 commit comments

Comments
 (0)