Skip to content

avoid OrderBy().ToList() in DefaultContractResolver.CreateProperties#365

Merged
SimonCropp merged 2 commits into
mainfrom
avoid-OrderBy().ToList()-in-DefaultContractResolver.CreateProperties
May 18, 2026
Merged

avoid OrderBy().ToList() in DefaultContractResolver.CreateProperties#365
SimonCropp merged 2 commits into
mainfrom
avoid-OrderBy().ToList()-in-DefaultContractResolver.CreateProperties

Conversation

@SimonCropp
Copy link
Copy Markdown
Member

Replace the LINQ ordering with an in-place List.Sort, and skip
sorting entirely when no property has an explicit Order. The previous
path allocated an OrderedEnumerable, a key-selector delegate, and an
intermediate buffer for every contract built.

Benchmark (net10.0, PropertyOrderBenchmark):

Count=5,   no order:  319 ns / 464 B  ->   63 ns /  96 B  (0.20x time, 0.21x alloc)
Count=20,  no order:  710 ns / 816 B  ->  106 ns / 216 B  (0.15x time, 0.26x alloc)
Count=100, no order: 4009 ns / 2736 B ->  388 ns / 856 B  (0.10x time, 0.31x alloc)
Count=100, mixed:    6201 ns / 2736 B -> 5570 ns / 856 B  (0.90x time, 0.31x alloc)

  Replace the LINQ ordering with an in-place List<T>.Sort, and skip
  sorting entirely when no property has an explicit Order. The previous
  path allocated an OrderedEnumerable, a key-selector delegate, and an
  intermediate buffer for every contract built.

  Benchmark (net10.0, PropertyOrderBenchmark):

    Count=5,   no order:  319 ns / 464 B  ->   63 ns /  96 B  (0.20x time, 0.21x alloc)
    Count=20,  no order:  710 ns / 816 B  ->  106 ns / 216 B  (0.15x time, 0.26x alloc)
    Count=100, no order: 4009 ns / 2736 B ->  388 ns / 856 B  (0.10x time, 0.31x alloc)
    Count=100, mixed:    6201 ns / 2736 B -> 5570 ns / 856 B  (0.90x time, 0.31x alloc)
@SimonCropp SimonCropp added this to the 0.34.0 milestone May 18, 2026
…t()-in-DefaultContractResolver.CreateProperties

# Conflicts:
#	src/Benchmark.Tests/Program.cs
@SimonCropp SimonCropp merged commit add2059 into main May 18, 2026
1 of 2 checks passed
@SimonCropp SimonCropp deleted the avoid-OrderBy().ToList()-in-DefaultContractResolver.CreateProperties branch May 18, 2026 11:59
@github-actions github-actions Bot restored the avoid-OrderBy().ToList()-in-DefaultContractResolver.CreateProperties branch May 18, 2026 11:59
@SimonCropp SimonCropp deleted the avoid-OrderBy().ToList()-in-DefaultContractResolver.CreateProperties branch May 18, 2026 12:05
This was referenced May 18, 2026
This was referenced May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant