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

Performance optimization for SvgPointCollectionConverter parser #808

Closed
Show file tree
Hide file tree
Changes from all 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
42 changes: 32 additions & 10 deletions Source/DataTypes/SvgPointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Text;
using Svg.Helpers;

namespace Svg
{
Expand Down Expand Up @@ -44,8 +45,10 @@ public override string ToString()
/// <summary>
/// A class to convert string into <see cref="SvgPointCollection"/> instances.
/// </summary>
internal class SvgPointCollectionConverter : TypeConverter
public class SvgPointCollectionConverter : TypeConverter
{
private static readonly char[] SplitChars = new[] { ' ', '\t', '\n', '\r', ',' };

/// <summary>
/// Converts the given object to the type of this converter, using the specified context and culture information.
/// </summary>
Expand All @@ -60,18 +63,37 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
{
if (value is string s)
{
var coords = s.AsSpan().Trim();
var state = new CoordinateParserState(ref coords);
var result = new SvgPointCollection();
while (CoordinateParser.TryGetFloat(out var pointValue, ref coords, ref state))
{
result.Add(new SvgUnit(SvgUnitType.User, pointValue));
}

return result;
return Parse(s.AsSpan());
}

return base.ConvertFrom(context, culture, value);
}

public static SvgPointCollection Parse(ReadOnlySpan<char> points)
{
#if false
var coords = points.Trim();
var state = new CoordinateParserState(ref coords);
var result = new SvgPointCollection();
while (CoordinateParser.TryGetFloat(out var pointValue, ref coords, ref state))
{
result.Add(new SvgUnit(SvgUnitType.User, pointValue));
}
return result;
#else
var collection = new SvgPointCollection();
var splitChars = SplitChars.AsSpan();
var parts = new StringSplitEnumerator(points, splitChars);

foreach (var part in parts)
{
var partValue = part.Value;
var result = StringParser.ToFloat(ref partValue);
collection.Add(new SvgUnit(SvgUnitType.User, result));
}

return collection;
#endif
}
}
}
6 changes: 6 additions & 0 deletions Tests/Svg.Benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ dotnet run -c Release -f netcoreapp3.1 -- -f '*SvgUnitCollectionConverter_*'
```
dotnet run -c Release -f netcoreapp3.1 -- -f '*SvgNumberCollectionConverter_*'
```

### Run `SvgPointCollectionConverter` Benchmarks

```
dotnet run -c Release -f netcoreapp3.1 -- -f '*SvgPointCollectionConverter_*'
```
16 changes: 16 additions & 0 deletions Tests/Svg.Benchmark/SvgPointCollectionConverterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Text;
using BenchmarkDotNet.Attributes;
using Svg;

namespace Svg.Benchmark
{
public class SvgPointCollectionConverterBenchmarks
{
[Benchmark]
public void SvgPointCollectionConverter_Parse()
{
SvgPointCollectionConverter.Parse("1.6,3.2 1.2,5".AsSpan());
}
}
}