Skip to content

Commit

Permalink
Improve CQ3 (#38)
Browse files Browse the repository at this point in the history
* Use ReadOnlySpan<byte> optimization

see: dotnet/roslyn#24621

* Reduce array allocations

* Prefer OrdinalIgnoreCase to InvariantCultureIgnoreCase

* Use StringComparison.Ordinal with StartsWith

* Add readonly to readonly Rect members
  • Loading branch information
iamcarbon authored Jul 18, 2020
1 parent 4be749d commit 9694e0b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/Svg.Picture/Primitives/Rect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public struct Rect

public static readonly Rect Empty = default;

public bool IsEmpty => Left == default && Top == default && Right == default && Bottom == default;
public readonly bool IsEmpty => Left == default && Top == default && Right == default && Bottom == default;

public float Width => Right - Left;
public readonly float Width => Right - Left;

public float Height => Bottom - Top;
public readonly float Height => Bottom - Top;

public Size Size => new Size(Width, Height);
public readonly Size Size => new Size(Width, Height);

public Point Location => new Point(Left, Top);
public readonly Point Location => new Point(Left, Top);

public Rect(float left, float top, float right, float bottom)
{
Expand Down
34 changes: 18 additions & 16 deletions src/Svg.Skia/Svg/SvgExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ namespace Svg.Skia
{
internal static class SvgExtensions
{
private static readonly char[] s_space_tab = new char[2] { ' ', '\t' };
private static readonly char[] s_space_tab = { ' ', '\t' };

private static readonly char[] s_comma = new char[] { ',' };
private static readonly char[] s_comma = { ',' };

[Flags]
internal enum PathPointType : byte
Expand Down Expand Up @@ -139,7 +139,7 @@ internal enum PathPointType : byte
// C_lin = C_srgb / 12.92;
// else
// C_lin = pow((C_srgb + 0.055) / 1.055, 2.4);
public static byte[] s_sRGBtoLinearRGB = new byte[256]
public static ReadOnlySpan<byte> s_sRGBtoLinearRGB => new byte[256]
{
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
Expand All @@ -164,7 +164,7 @@ internal enum PathPointType : byte
// C_srgb = C_lin * 12.92;
// else
// C_srgb = 1.055 * pow(C_lin, 1.0 / 2.4) - 0.055;
public static byte[] s_linearRGBtoSRGB = new byte[256]
public static ReadOnlySpan<byte> s_linearRGBtoSRGB => new byte[256]
{
0, 13, 22, 28, 34, 38, 42, 46, 50, 53, 56, 59, 61, 64, 66, 69,
71, 73, 75, 77, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 96, 98,
Expand All @@ -188,7 +188,7 @@ internal enum PathPointType : byte

private const string MimeTypeSvg = "image/svg+xml";

private static readonly byte[] s_gZipMagicHeaderBytes = { 0x1f, 0x8b };
private static ReadOnlySpan<byte> s_gZipMagicHeaderBytes => new byte[2] { 0x1f, 0x8b };

public const string SourceGraphic = "SourceGraphic";

Expand Down Expand Up @@ -516,7 +516,7 @@ public static bool HasRequiredFeatures(this SvgElement svgElement)
}
else
{
var features = requiredFeaturesString.Trim().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
var features = requiredFeaturesString.Trim().Split(s_space_tab, StringSplitOptions.RemoveEmptyEntries);
if (features.Length > 0)
{
foreach (var feature in features)
Expand Down Expand Up @@ -2470,9 +2470,9 @@ public static object GetImageFromWeb(Uri uri)
stream.Position = 0;
}

var isSvgMimeType = response.ContentType.StartsWith(MimeTypeSvg, StringComparison.InvariantCultureIgnoreCase);
var isSvg = uri.LocalPath.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase);
var isSvgz = uri.LocalPath.EndsWith(".svgz", StringComparison.InvariantCultureIgnoreCase);
var isSvgMimeType = response.ContentType.StartsWith(MimeTypeSvg, StringComparison.OrdinalIgnoreCase);
var isSvg = uri.LocalPath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);
var isSvgz = uri.LocalPath.EndsWith(".svgz", StringComparison.OrdinalIgnoreCase);

if (isSvgMimeType || isSvg)
{
Expand Down Expand Up @@ -2512,7 +2512,7 @@ public static object GetImageFromWeb(Uri uri)
charset = string.Empty;
}

if (headers.Count > 0 && headers[headers.Count - 1].Trim().Equals("base64", StringComparison.InvariantCultureIgnoreCase))
if (headers.Count > 0 && headers[headers.Count - 1].Trim().Equals("base64", StringComparison.OrdinalIgnoreCase))
{
base64 = true;
headers.RemoveAt(headers.Count - 1);
Expand All @@ -2527,14 +2527,14 @@ public static object GetImageFromWeb(Uri uri)
}

var attribute = p[0].Trim();
if (attribute.Equals("charset", StringComparison.InvariantCultureIgnoreCase))
if (attribute.Equals("charset", StringComparison.OrdinalIgnoreCase))
{
charset = p[1].Trim();
}
}

var data = uriString.Substring(headerEndIndex + 1);
if (mimeType.Equals(MimeTypeSvg, StringComparison.InvariantCultureIgnoreCase))
if (mimeType.Equals(MimeTypeSvg, StringComparison.OrdinalIgnoreCase))
{
if (base64)
{
Expand All @@ -2556,7 +2556,7 @@ public static object GetImageFromWeb(Uri uri)
using var stream = new MemoryStream(Encoding.Default.GetBytes(data));
return LoadSvg(stream, svgOwnerDocument.BaseUri);
}
else if (mimeType.StartsWith("image/") || mimeType.StartsWith("img/"))
else if (mimeType.StartsWith("image/", StringComparison.Ordinal) || mimeType.StartsWith("img/", StringComparison.Ordinal))
{
var dataBytes = base64 ? Convert.FromBase64String(data) : Encoding.Default.GetBytes(data);
using var stream = new MemoryStream(dataBytes);
Expand Down Expand Up @@ -3279,7 +3279,7 @@ public static void GetSvgVisualElementClipPath(SvgVisualElement svgVisualElement
public static SKRect? GetClipRect(SvgVisualElement svgVisualElement, SKRect skRectBounds)
{
var clip = svgVisualElement.Clip;
if (!string.IsNullOrEmpty(clip) && clip.StartsWith("rect("))
if (!string.IsNullOrEmpty(clip) && clip.StartsWith("rect(", StringComparison.Ordinal))
{
clip = clip.Trim();
var offsets = new List<float>();
Expand Down Expand Up @@ -3523,6 +3523,8 @@ public static float[] CreateIdentityColorMatrixArray()
};
}

private static readonly char[] s_colorMatrixSplitChars = { ' ', '\t', '\n', '\r', ',' };

public static SKImageFilter? CreateColorMatrix(SvgColourMatrix svgColourMatrix, CompositeDisposable disposable, SKImageFilter? input = null, CropRect? cropRect = null)
{
SKColorFilter skColorFilter;
Expand Down Expand Up @@ -3589,7 +3591,7 @@ public static float[] CreateIdentityColorMatrixArray()
}
else
{
var parts = svgColourMatrix.Values.Split(new char[] { ' ', '\t', '\n', '\r', ',' }, StringSplitOptions.RemoveEmptyEntries);
var parts = svgColourMatrix.Values.Split(s_colorMatrixSplitChars, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 20)
{
matrix = new float[20];
Expand Down Expand Up @@ -5470,7 +5472,7 @@ public virtual void PostProcess()
{
// TODO:
}
else if (enableBackground.StartsWith("new"))
else if (enableBackground.StartsWith("new", StringComparison.Ordinal))
{
if (enableBackground.Length > 3)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Svg.Skia/Typeface/CustomTypefaceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Svg.Skia
{
public sealed class CustomTypefaceProvider : ITypefaceProvider, IDisposable
{
public static char[] s_fontFamilyTrim = new char[] { '\'' };
public static readonly char[] s_fontFamilyTrim = { '\'' };

public SKTypeface? Typeface { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Svg.Skia/Typeface/DefaultTypefaceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Svg.Skia
{
public sealed class DefaultTypefaceProvider : ITypefaceProvider
{
public static char[] s_fontFamilyTrim = new char[] { '\'' };
public static readonly char[] s_fontFamilyTrim = { '\'' };

public SKTypeface? FromFamilyName(string fontFamily, SKFontStyleWeight fontWeight, SKFontStyleWidth fontWidth, SKFontStyleSlant fontStyle)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Svg.Skia/Typeface/FontManagerTypefacerovider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Svg.Skia
{
public sealed class FontManagerTypefacerovider : ITypefaceProvider
{
public static char[] s_fontFamilyTrim = new char[] { '\'' };
public static readonly char[] s_fontFamilyTrim = { '\'' };

public SKFontManager FontManager { get; set; }

Expand Down

0 comments on commit 9694e0b

Please sign in to comment.