Commit 92904176 authored by 彭伟's avatar 彭伟 Committed by Lemon

Use ReadOnlySpan<char> Improve performance after .netcoreapp2.1 (#192)

parent 7016ef29
......@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
......
......@@ -3,7 +3,7 @@ using SkyApm.Tracing;
namespace SkyApm.Benchmark
{
public class UniqueId
public class UniqueIdGenerate
{
private static readonly IUniqueIdGenerator Generator = new UniqueIdGenerator(new RuntimeEnvironment());
......
using BenchmarkDotNet.Attributes;
using SkyApm.Tracing;
namespace SkyApm.Benchmark
{
public class UniqueIdParse
{
private static readonly IUniqueIdParser Parser = new UniqueIdParser();
private static readonly string Id = new UniqueId(long.MaxValue, long.MaxValue, long.MaxValue).ToString();
[Benchmark]
public void Parse() => Parser.TryParse(Id, out _);
}
}
......@@ -86,10 +86,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Agent.GeneralHost",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Sample.GeneralHost", "sample\SkyApm.Sample.GeneralHost\SkyApm.Sample.GeneralHost.csproj", "{477D705E-576B-46C8-8F1E-9A86EDAE9D86}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Benchmark", "benchmark\SkyApm.Benchmark\SkyApm.Benchmark.csproj", "{33581FDE-ABAF-4C27-A40A-1A2743309399}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Benchmark", "benchmark\SkyApm.Benchmark\SkyApm.Benchmark.csproj", "{33581FDE-ABAF-4C27-A40A-1A2743309399}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{2B7F59E8-147F-4399-9804-E7EAEF2DCB22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Core.Tests", "test\SkyApm.Core.Tests\SkyApm.Core.Tests.csproj", "{5E654407-E22F-4696-A33F-C4B372F547BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -196,6 +198,10 @@ Global
{33581FDE-ABAF-4C27-A40A-1A2743309399}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33581FDE-ABAF-4C27-A40A-1A2743309399}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33581FDE-ABAF-4C27-A40A-1A2743309399}.Release|Any CPU.Build.0 = Release|Any CPU
{5E654407-E22F-4696-A33F-C4B372F547BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E654407-E22F-4696-A33F-C4B372F547BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E654407-E22F-4696-A33F-C4B372F547BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E654407-E22F-4696-A33F-C4B372F547BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -234,6 +240,7 @@ Global
{4C1CF0E7-4CD3-4731-9A56-E033D5216D58} = {EF6194B2-9ACB-49B9-8049-DD6AFAEB0399}
{477D705E-576B-46C8-8F1E-9A86EDAE9D86} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB}
{33581FDE-ABAF-4C27-A40A-1A2743309399} = {2B7F59E8-147F-4399-9804-E7EAEF2DCB22}
{5E654407-E22F-4696-A33F-C4B372F547BD} = {613F0980-91ED-4064-8E8C-168582EF4AD7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
......
......@@ -9,7 +9,8 @@
<PackageReleaseNotes>
</PackageReleaseNotes>
<RootNamespace>SkyWalking</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
<DefineConstants Condition="'$(TargetFramework)' == 'netcoreapp2.1'">$(DefineConstants);SPAN</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.2.0" />
......
......@@ -16,6 +16,8 @@
*
*/
using System;
namespace SkyApm.Tracing
{
public class UniqueIdParser : IUniqueIdParser
......@@ -23,14 +25,42 @@ namespace SkyApm.Tracing
public bool TryParse(string text, out UniqueId uniqueId)
{
uniqueId = default(UniqueId);
if (text == null) return false;
if (string.IsNullOrEmpty(text)) return false;
#if SPAN
var id = text.AsSpan();
var index = FindIndex(id);
if (index < 1) return false;
var id1 = id.Slice(0, index);
index = FindIndex(id.Slice(index + 1));
if (index < 1) return false;
if (!long.TryParse(id1, out var part0)) return false;
if (!long.TryParse(id.Slice(id1.Length + 1, index), out var part1)) return false;
if (!long.TryParse(id.Slice(id1.Length + index + 2), out var part2)) return false;
#else
var parts = text.Split("\\.".ToCharArray(), 3);
if (parts.Length < 3) return false;
if (!long.TryParse(parts[0], out var part0)) return false;
if (!long.TryParse(parts[1], out var part1)) return false;
if (!long.TryParse(parts[2], out var part2)) return false;
#endif
uniqueId = new UniqueId(part0, part1, part2);
return true;
}
#if SPAN
private static int FindIndex(ReadOnlySpan<char> id)
{
var index = 0;
do
{
if (id[index] == '\\' || id[index] == '.')
return index;
} while (++index < id.Length);
return -1;
}
#endif
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<ProjectReference Include="..\..\src\SkyApm.Core\SkyApm.Core.csproj" />
</ItemGroup>
</Project>
using SkyApm.Tracing;
using System;
using Xunit;
namespace SkyApm.Core.Tests
{
public class UniqueIdParserTest
{
private static readonly IUniqueIdParser Parser = new UniqueIdParser();
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("1", false)]
[InlineData("1.1", false)]
[InlineData("1.1.", false)]
[InlineData("1.1.a", false)]
[InlineData("1.1.1.1", false)]
[InlineData("1\\1.-1", true)]
public void TryParse_Return(string text, bool result) =>
Assert.Equal(result, Parser.TryParse(text, out _));
[Theory]
[InlineData("1.2.3", 1, 2, 3)]
[InlineData("123.456.789", 123, 456, 789)]
[InlineData("-1.-2.-3", -1, -2, -3)]
[InlineData("9223372036854775807.9223372036854775807.9223372036854775807", 9223372036854775807, 9223372036854775807, 9223372036854775807)]
[InlineData("-9223372036854775807.-9223372036854775807.-9223372036854775807", -9223372036854775807, -9223372036854775807, -9223372036854775807)]
public void TryParse_Out(string text, long part1, long part2, long part3)
{
Parser.TryParse(text, out var id);
Assert.Equal(part1, id.Part1);
Assert.Equal(part2, id.Part2);
Assert.Equal(part3, id.Part3);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment