-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce ScopedBuffer #415
Changes from 6 commits
b0a5fe8
215881d
8ad19dc
e76716a
a436552
52c9b1f
337d235
50d44e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using System.Buffers; | ||
using System.Buffers.Binary; | ||
|
||
namespace MetadataExtractor.IO; | ||
|
@@ -178,6 +177,9 @@ public readonly double GetDouble64(int index) | |
|
||
public readonly string GetString(int index, int bytesRequested, Encoding encoding) | ||
{ | ||
if (bytesRequested < 0) | ||
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater"); | ||
iamcarbon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// This check is important on .NET Framework | ||
if (bytesRequested is 0) | ||
{ | ||
|
@@ -193,17 +195,11 @@ public readonly string GetString(int index, int bytesRequested, Encoding encodin | |
} | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the else block any more? Looks like the size check could be moved into a ternary conditional and the two blocks collapsed into one, as you've done elsewhere in the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unified the logic across all the readers. much tidier. |
||
{ | ||
byte[] bytes = ArrayPool<byte>.Shared.Rent(bytesRequested); | ||
|
||
Span<byte> span = bytes.AsSpan(0, bytesRequested); | ||
|
||
GetBytes(index, span); | ||
|
||
var s = encoding.GetString(span); | ||
using var buffer = new ScopedBuffer(bytesRequested); | ||
|
||
ArrayPool<byte>.Shared.Return(bytes); | ||
GetBytes(index, buffer); | ||
|
||
return s; | ||
return encoding.GetString(buffer); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using System.Buffers; | ||
|
||
namespace MetadataExtractor.IO; | ||
|
||
internal ref struct ScopedBuffer | ||
{ | ||
private byte[]? _rentedBuffer; | ||
private readonly Span<byte> _span; | ||
|
||
public ScopedBuffer(int size) | ||
{ | ||
_rentedBuffer = ArrayPool<byte>.Shared.Rent(size); | ||
_span = _rentedBuffer.AsSpan(0, size); | ||
} | ||
|
||
public ScopedBuffer(Span<byte> span) | ||
{ | ||
_span = span; | ||
} | ||
|
||
public readonly Span<byte> Span => _span; | ||
|
||
public static implicit operator Span<byte>(ScopedBuffer bufferScope) | ||
{ | ||
return bufferScope._span; | ||
} | ||
|
||
public static implicit operator ReadOnlySpan<byte>(ScopedBuffer bufferScope) | ||
{ | ||
return bufferScope._span; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_rentedBuffer is null) return; | ||
|
||
ArrayPool<byte>.Shared.Return(_rentedBuffer); | ||
|
||
_rentedBuffer = null; | ||
iamcarbon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ public static bool IsValidTime(int hours, int minutes, int seconds) | |
minutes is >= 0 and < 60 | ||
&& seconds is >= 0 and < 60; | ||
|
||
#if NET8_0 || NETSTANDARD2_1 | ||
#if NET8_0_OR_GREATER || NETSTANDARD2_1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing this. I searched and couldn't find any other instances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, also searched -- and only one I found! |
||
private static readonly DateTime _unixEpoch = DateTime.UnixEpoch; | ||
#else | ||
private static readonly DateTime _unixEpoch = new(1970, 1, 1, 0, 0, 0); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we convert the comment into a
Debug.Assert(count <= 255)
instead? It provides a little more safety and is clearer as far as documenting intent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the comment to be clearer (count is an unsigned byte, with a safe range) -- no need for an assert. The comment saves a second when auditing the stackalloc call sites.