Skip to content

Commit

Permalink
Eliminate a few allocations in ColorSpace transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon authored and BobLd committed Apr 12, 2024
1 parent 6d54355 commit 4fcc5c6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Tokens;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Functions;
Expand Down Expand Up @@ -572,7 +573,12 @@ internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> decoded)
transformed.Add(ConvertToByte(colors[c]));
}
}

#if NET8_0_OR_GREATER
return CollectionsMarshal.AsSpan(transformed);
#else
return transformed.ToArray();
#endif
}

/// <inheritdoc/>
Expand Down Expand Up @@ -721,7 +727,7 @@ public override IColor GetColor(params double[] values)
internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> values)
{
var cache = new Dictionary<int, double[]>();
var transformed = new List<byte>();
var transformed = new List<byte>(values.Length * 3);
for (var i = 0; i < values.Length; i += 3)
{
byte b = values[i++];
Expand All @@ -737,7 +743,11 @@ internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> values)
}
}

#if NET8_0_OR_GREATER
return CollectionsMarshal.AsSpan(transformed);
#else
return transformed.ToArray();
#endif
}

/// <inheritdoc/>
Expand Down

0 comments on commit 4fcc5c6

Please sign in to comment.