Skip to content

SafePixelCollection.CheckIndex always allocates strings even if they aren't needed #1436

Closed
@zlepper

Description

@zlepper

Magick.NET version

13.2.0 (Also relevant on main)

Environment (Operating system, version and so on)

Windows

Description

The code here: https://github.com/dlemstra/Magick.NET/blob/main/src/Magick.NET/Pixels/SafePixelCollection.cs#L196

    private void CheckIndex(int x, int y)
    {
        Throw.IfOutOfRange(nameof(x), 0, Image.Width - 1, x, $"Invalid X coordinate: {x}.");
        Throw.IfOutOfRange(nameof(y), 0, Image.Height - 1, y, $"Invalid Y coordinate: {y}.");
    }

Always allocates the 2 error strings, even if they aren't going to be used due to the interpolation.

Why is this an issue? Mainly because it causes extra work for the GC. If you want to get absolutely crazy, you can use create an interpolated string handler, that can conditionally create the string, as seen here: https://devblogs.microsoft.com/dotnet/string-interpolation-in-c-10-and-net-6/#debug-assert-without-the-overhead

Steps to Reproduce

Call GetPixel(x, y) with a memory profiler attached.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions