Commit 7016ef29 authored by 彭伟's avatar 彭伟 Committed by Lemon

Improve performance: Use struct (#190)

* Improve performance: Use struct

* Add benchmark

* benchmark

* fix build error
parent 96554c82
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
namespace SkyApm.Benchmark
{
class Program
{
public static void Main(params string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
.Run(args, ManualConfig
.Create(DefaultConfig.Instance)
.With(MemoryDiagnoser.Default)
.With(ExecutionValidator.FailOnError));
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.*" />
<ProjectReference Include="..\..\src\SkyApm.Core\SkyApm.Core.csproj" />
</ItemGroup>
</Project>
using BenchmarkDotNet.Attributes;
using SkyApm.Tracing;
namespace SkyApm.Benchmark
{
public class UniqueId
{
private static readonly IUniqueIdGenerator Generator = new UniqueIdGenerator(new RuntimeEnvironment());
[Benchmark]
public void Generate() => Generator.Generate();
}
}
......@@ -86,6 +86,10 @@ 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}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{2B7F59E8-147F-4399-9804-E7EAEF2DCB22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -188,6 +192,10 @@ Global
{477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Release|Any CPU.Build.0 = Release|Any CPU
{33581FDE-ABAF-4C27-A40A-1A2743309399}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -225,6 +233,7 @@ Global
{B43160C8-C9E3-43F6-83E9-A9A45B32F022} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
......
/*
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
......@@ -20,7 +20,7 @@ using System;
namespace SkyApm.Tracing
{
public class UniqueId : IEquatable<UniqueId>
public struct UniqueId : IEquatable<UniqueId>
{
public long Part1 { get; }
......@@ -37,26 +37,25 @@ namespace SkyApm.Tracing
public override string ToString() => $"{Part1}.{Part2}.{Part3}";
public bool Equals(UniqueId other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (Part1 != other.Part1) return false;
if (Part2 != other.Part2) return false;
return Part3 == other.Part3;
}
public bool Equals(UniqueId other) =>
Part1 == other.Part1 && Part2 == other.Part2 && Part3 == other.Part3;
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (!(obj is UniqueId id)) return false;
return Equals(id);
}
public override bool Equals(object obj) =>
obj is UniqueId other && Equals(other);
public override int GetHashCode()
{
throw new NotImplementedException();
unchecked
{
var hashCode = Part1.GetHashCode();
hashCode = (hashCode * 397) ^ Part2.GetHashCode();
hashCode = (hashCode * 397) ^ Part3.GetHashCode();
return hashCode;
}
}
public static bool operator ==(UniqueId left, UniqueId right) => left.Equals(right);
public static bool operator !=(UniqueId left, UniqueId right) => !left.Equals(right);
}
}
\ No newline at end of file
}
/*
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
......@@ -22,7 +22,7 @@ namespace SkyApm.Tracing
{
public bool TryParse(string text, out UniqueId uniqueId)
{
uniqueId = null;
uniqueId = default(UniqueId);
if (text == null) return false;
var parts = text.Split("\\.".ToCharArray(), 3);
if (parts.Length < 3) return false;
......@@ -33,4 +33,4 @@ namespace SkyApm.Tracing
return true;
}
}
}
\ No newline at end of file
}
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