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

Remove Marshalling code during Color Translation, reduce allocations #9330

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

h3xds1nz
Copy link
Contributor

@h3xds1nz h3xds1nz commented Jun 30, 2024

Description

Improves slightly performance and subsequent allocations when subtracting/multiplying or just generally working with Color using a custom profile / ColorContext by removing Marshal code/allocs and simply using stackalloc for the buffer, replacing some dead code (It is obvious Translate method has been rewritten previously) and removing the try/finally block used for unmanaged memory deallocation.

Redundant array inits to 0 have been removed in ICM2Color and also some heap-allocated arrays have been replaced with stack-allocated spans in the call chain.

Last two commits are not included in benchmarks but are pretty straightforward improvements.

Color Subtraction with custom profile

Method Mean Error StdDev Gen0 Allocated
Marshalled 2.919 ms 0.0460 ms 0.0408 ms 3.9063 79.03 KB
StackAlloc 2.887 ms 0.0555 ms 0.0519 ms 3.9063 78.82 KB
Benchmark code
public Uri sRGB = new("C:\\Windows\\System32\\spool\\drivers\\color\\sRGB Color Space Profile.icm", UriKind.Absolute);

[Benchmark]
public Color Benchmark()
{
    Color x = Color.FromAValues(1.0f, [0.0f, 1.0f, 1.0f], sRGB);
    Color y = Color.FromAValues(0.5f, [0.0f, 0.5f, 1.0f], sRGB);

    return x - y;
}

Multiplication by factor with custom profile

Method Mean Error StdDev Allocated
Marshalled 1.918 ms 0.0116 ms 0.0097 ms 47.67 KB
StackAlloc 1.915 ms 0.0153 ms 0.0144 ms 47.53 KB
Benchmark code
public Uri sRGB = new("C:\\Windows\\System32\\spool\\drivers\\color\\sRGB Color Space Profile.icm", UriKind.Absolute);

[Benchmark]
public Color Benchmark()
{
    Color x = Color.FromAValues(1.0f, [0.0f, 1.0f, 1.0f], sRGB);

    return x * 0.1f;
}

Raw Translate method call chain

Method Mean Error StdDev Gen0 Allocated
Marshalled 118.36 ns 1.284 ns 1.201 ns 0.0086 144 B
StackAlloc 22.80 ns 0.430 ns 0.402 ns 0.0043 72 B

Customer Impact

Improved performance, decreased allocations.

Regression

None.

Testing

Build, benchmark, using the code and comparing outputs (addition, subtraction, multiplication).

Risk

Low.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners June 30, 2024 19:33
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jun 30, 2024
@miloush
Copy link
Contributor

miloush commented Jun 30, 2024

LGTM

@miloush
Copy link
Contributor

miloush commented Jul 1, 2024

using the code and comparing outputs (addition, subtraction, multiplication).

Did you test it on multichannel color contexts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage Queue for test pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants