Skip to content
Merged
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
19 changes: 18 additions & 1 deletion src/Argon/Serialization/DefaultContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,24 @@ protected virtual IList<JsonProperty> CreateProperties(Type type, MemberSerializ
properties.AddProperty(property);
}

return properties.OrderBy(_ => _.Order ?? -1).ToList();
var list = new List<JsonProperty>(properties);

var needsSort = false;
foreach (var property in list)
{
if (property.Order != null)
{
needsSort = true;
break;
}
}

if (needsSort)
{
list.Sort(static (a, b) => (a.Order ?? -1).CompareTo(b.Order ?? -1));
}

return list;
}

public virtual JsonNameTable GetNameTable() => nameTable;
Expand Down
63 changes: 63 additions & 0 deletions src/ArgonTests/Benchmarks/PropertyOrderBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2007 James Newton-King. All rights reserved.
// Use of this source code is governed by The MIT License,
// as found in the license.md file.

using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
public class PropertyOrderBenchmark
{
JsonPropertyCollection properties = null!;

[Params(5, 20, 100)]
public int Count { get; set; }

[Params(false, true)]
public bool AnyOrder { get; set; }

[GlobalSetup]
public void Setup()
{
properties = new(typeof(object));
for (var i = 0; i < Count; i++)
{
var property = new JsonProperty(typeof(string), typeof(object))
{
PropertyName = "P" + i
};
if (AnyOrder && i % 3 == 0)
{
property.Order = Count - i;
}

properties.AddProperty(property);
}
}

[Benchmark(Baseline = true)]
public IList<JsonProperty> OrderByToList() =>
properties.OrderBy(_ => _.Order ?? -1).ToList();

[Benchmark]
public IList<JsonProperty> InPlaceSort()
{
var list = new List<JsonProperty>(properties);

var needsSort = false;
foreach (var property in list)
{
if (property.Order != null)
{
needsSort = true;
break;
}
}

if (needsSort)
{
list.Sort(static (a, b) => (a.Order ?? -1).CompareTo(b.Order ?? -1));
}

return list;
}
}
2 changes: 1 addition & 1 deletion src/Benchmark.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Main(string[] args)
var attribute = (AssemblyFileVersionAttribute)typeof(JsonConvert).Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))!;
Console.WriteLine($"Json.NET Version: {attribute.Version}");

var switcher = new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString), typeof(SerializeJTokenList), typeof(CamelCaseBenchmarks), typeof(ReadQuotedNumbers), typeof(WriteBase64Benchmark), typeof(SplitFullyQualifiedTypeNameBench)]);
var switcher = new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString), typeof(SerializeJTokenList), typeof(CamelCaseBenchmarks), typeof(ReadQuotedNumbers), typeof(WriteBase64Benchmark), typeof(SplitFullyQualifiedTypeNameBench), typeof(PropertyOrderBenchmark)]);
if (args.Length == 0)
{
switcher.Run(["*"]);
Expand Down
Loading