diff --git a/YamlDotNet.Benchmark/Program.cs b/YamlDotNet.Benchmark/Program.cs index e8df09cd..834f606b 100644 --- a/YamlDotNet.Benchmark/Program.cs +++ b/YamlDotNet.Benchmark/Program.cs @@ -1,8 +1,25 @@ -using System.Diagnostics; +// This file is part of YamlDotNet - A .NET library for YAML. +// Copyright (c) Antoine Aubry and contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +using BenchmarkDotNet.Running; using YamlDotNet.Benchmark; -using YamlDotNet.Serialization; -var serializer = new SerializerBuilder().JsonCompatible().Build(); -var v = new { nan = float.NaN, inf = float.NegativeInfinity, posinf = float.PositiveInfinity, max = float.MaxValue, min = float.MinValue, good = .1234f, good1 = 1, good2 = -.1234, good3= -1 }; -var yaml = serializer.Serialize(v); -Console.WriteLine(yaml); +BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); diff --git a/YamlDotNet.Benchmark/SerializationBenchmarks.cs b/YamlDotNet.Benchmark/SerializationBenchmarks.cs new file mode 100644 index 00000000..41f51849 --- /dev/null +++ b/YamlDotNet.Benchmark/SerializationBenchmarks.cs @@ -0,0 +1,54 @@ +// This file is part of YamlDotNet - A .NET library for YAML. +// Copyright (c) Antoine Aubry and contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +using System.Text; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Jobs; +using YamlDotNet.Serialization; + +namespace YamlDotNet.Benchmark; + +[MemoryDiagnoser] +[MediumRunJob(RuntimeMoniker.Net80)] +[MediumRunJob(RuntimeMoniker.Net47)] +public class SerializationBenchmarks +{ + public class SampleRecord + { + public SampleRecord(string name, string description) + { + Name = name; + Description = description; + } + + public string Name { get; private set; } + public string Description { get; private set; } + } + + private readonly IReadOnlyCollection configs = Enumerable.Range(0, 10_000).Select(i => new SampleRecord("MyName", "MyDescription")).ToList(); + private readonly ISerializer serializer = new SerializerBuilder().DisableAliases().Build(); + + [Benchmark] + public string Serializer() + { + return serializer.Serialize(configs); + } +} diff --git a/YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj b/YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj index 79208c1f..a9e0b4a9 100644 --- a/YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj +++ b/YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj @@ -1,14 +1,16 @@ - + Exe - net8.0 + net8.0;net47 enable enable + 10.0 - + + diff --git a/YamlDotNet/ReflectionExtensions.cs b/YamlDotNet/ReflectionExtensions.cs index f1e472c9..ad75e5ea 100644 --- a/YamlDotNet/ReflectionExtensions.cs +++ b/YamlDotNet/ReflectionExtensions.cs @@ -279,34 +279,9 @@ public static bool IsInstanceOf(this Type type, object o) public static Attribute[] GetAllCustomAttributes(this PropertyInfo member) { - // IMemberInfo.GetCustomAttributes ignores it's "inherit" parameter for properties, - // and the suggested replacement (Attribute.GetCustomAttributes) is not available - // on netstandard1.3 - var result = new List(); - var type = member.DeclaringType; - var name = member.Name; - - while (type != null) - { - var property = type.GetPublicProperty(name); - - if (property != null) - { - result.AddRange(property.GetCustomAttributes(typeof(TAttribute))); - - if ((property.GetGetMethod()?.IsHideBySig == true) - || (property.GetSetMethod()?.IsHideBySig == true)) - { - // Don't continue up the hierarchy. - break; - } - } - - type = type.BaseType(); - } - - return result.ToArray(); + return Attribute.GetCustomAttributes(member, typeof(TAttribute), inherit: true); } + private static readonly ConcurrentDictionary typesHaveNullContext = new ConcurrentDictionary(); public static bool AcceptsNull(this MemberInfo member) {