Skip to content
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

Merged
merged 8 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions MetadataExtractor/Formats/Apple/BplistReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +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;

namespace MetadataExtractor.Formats.Apple;

/// <summary>
Expand Down Expand Up @@ -123,12 +121,9 @@ static object HandleInt(ref BufferReader reader, byte marker)

static Dictionary<byte, byte> HandleDict(ref BufferReader reader, byte count)
{
var keyRefs = ArrayPool<byte>.Shared.Rent(count);
Span<byte> keyRefs = stackalloc byte[count]; // <= 255
Copy link
Owner

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.

Copy link
Collaborator Author

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.


for (int j = 0; j < count; j++)
{
keyRefs[j] = reader.GetByte();
}
reader.GetBytes(keyRefs);

Dictionary<byte, byte> map = [];

Expand All @@ -137,8 +132,6 @@ static Dictionary<byte, byte> HandleDict(ref BufferReader reader, byte count)
map.Add(keyRefs[j], reader.GetByte());
}

ArrayPool<byte>.Shared.Return(keyRefs);

return map;
}

Expand Down
16 changes: 6 additions & 10 deletions MetadataExtractor/IO/BufferReader.Indexed.cs
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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -193,17 +195,11 @@ public readonly string GetString(int index, int bytesRequested, Encoding encodin
}
else
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
}

Expand Down
14 changes: 8 additions & 6 deletions MetadataExtractor/IO/BufferReader.Sequential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,19 @@ public ulong GetUInt64()

public string GetString(int bytesRequested, Encoding encoding)
{
// This check is important on .NET Framework
iamcarbon marked this conversation as resolved.
Show resolved Hide resolved
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

if (bytesRequested is 0)
return "";

Span<byte> bytes = bytesRequested <= 256
? stackalloc byte[bytesRequested]
: new byte[bytesRequested];
using var buffer = bytesRequested <= 256
iamcarbon marked this conversation as resolved.
Show resolved Hide resolved
? new ScopedBuffer(stackalloc byte[bytesRequested])
: new ScopedBuffer(bytesRequested);

GetBytes(bytes);
GetBytes(buffer);

return encoding.GetString(bytes);
return encoding.GetString(buffer);
}

public StringValue GetNullTerminatedStringValue(int maxLengthBytes, Encoding? encoding = null, bool moveToMaxLength = false)
Expand Down
17 changes: 6 additions & 11 deletions MetadataExtractor/IO/IndexedReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +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
Expand Down Expand Up @@ -320,6 +318,9 @@ public double GetDouble64(int index)
/// <exception cref="IOException"/>
public string GetString(int index, int bytesRequested, Encoding encoding)
{
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

// This check is important on .NET Framework
if (bytesRequested is 0)
{
Expand All @@ -335,17 +336,11 @@ public string GetString(int index, int bytesRequested, Encoding encoding)
}
else
{
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);
}
}

Expand Down
43 changes: 43 additions & 0 deletions MetadataExtractor/IO/ScopedBuffer.cs
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
}
}
16 changes: 6 additions & 10 deletions MetadataExtractor/IO/SequentialReader.cs
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
Expand Down Expand Up @@ -221,6 +220,9 @@ public float GetS15Fixed16()
/// <exception cref="IOException"/>
public string GetString(int bytesRequested, Encoding encoding)
{
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

// This check is important on .NET Framework
if (bytesRequested is 0)
{
Expand All @@ -236,17 +238,11 @@ public string GetString(int bytesRequested, Encoding encoding)
}
else
{
byte[] bytes = ArrayPool<byte>.Shared.Rent(bytesRequested);

Span<byte> span = bytes.AsSpan(0, bytesRequested);

GetBytes(span);

var s = encoding.GetString(span);
using var buffer = new ScopedBuffer(bytesRequested);

ArrayPool<byte>.Shared.Return(bytes);
GetBytes(buffer);

return s;
return encoding.GetString(buffer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion MetadataExtractor/Util/DateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
Expand Down
Loading