perf: reduce collection humanization allocations - #2
Open
artl93 wants to merge 1 commit into
Open
Conversation
Write built-in collection formats directly into the result string, avoiding the intermediate StringBuilder and joined string for collections with three or more displayable items. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ad56389e-2890-49a6-a52f-76ee42a29891
artl93
force-pushed
the
artl93/perf-bench
branch
from
July 17, 2026 22:11
dc8274c to
3245e30
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Write built-in collection formats directly into the final result string for collections with three or more displayable items. This removes the intermediate
StringBuilder, its backing buffer, and the intermediate joined string while preserving the existing two-item fast path and custom-format fallback.This fork PR was reduced from 12 unrelated files to one coherent public API path after rebasing onto
mainatf9292aa90948de0aea2d4fa7d6549b1b2432c0fb.Scope
Retained and redesigned
DefaultCollectionFormatter: directstring.Createoutput for the two built-in formats ({0} {1} {2}and Oxford{0}, {1} {2}).Dropped
Benchmark methodology
Temporary change-validation harness outside the repository, using BenchmarkDotNet 0.15.8 and the default out-of-process toolchain (not the repo benchmark project's global
InProcessEmitToolchain).Humanizer.dllfromf9292aa90948de0aea2d4fa7d6549b1b2432c0fb3245e30044359fe8ecea61d0a49cd50b027d775d11.0.100-preview.3.26207.10610.0.300and run on .NET10.0.8, Arm64 RyuJIT, concurrent workstation GCJob.MediumRun: 2 process launches, 10 warmups, 15 measured iterationsMemoryDiagnoser, identical jobs, full JSON/CSV/GitHub Markdown exportsstring[].Humanize()under English (Oxford) and French (non-Oxford) culturesExact final command:
dotnet run -c Release --no-build -- --filter '*' --artifacts ./artifacts-final --noOverwrite--appleswas also attempted, but BenchmarkDotNet 0.15.8 crashed after its pilot phase on this parameterized suite withSequence contains more than one matching element; final claims use the successful MediumRun above.Full results
The unchanged two-item path has identical allocation and timing within/near measurement noise. No measured case regressed. Collections of 3+ items improve 16–43% in mean throughput and reduce allocation by 42–65%; the representative distribution improves 25% with 27% fewer allocated bytes.
Validation
Humanizer.Tests, net10.0: 56,561 passedHumanizer.Tests, net8.0: 56,561 passedmainby pre-existing CRLFIDE0055errors in untouchedEnumDehumanizeExtensions.csandPolyfillShims.cs.dotnet pack src/Humanizer/Humanizer.csproj -c Release -o <path>while suppressing only pre-existingIDE0055,CS1573, andCS1591diagnostics built all five library TFMs and producedHumanizer.3.5.0-preview.52.gf9292aa909.nupkg.No upstream PR, issue comment, or other submission was created.