[repo-assist] perf: replace formatObject Array.map+Join with StringBuilder#440
Merged
sergey-tihon merged 2 commits intoMay 17, 2026
Conversation
Rewrite formatObject to stream directly into a StringBuilder instead of
- allocating an intermediate string[] via Array.map
- allocating per-property strings via 'p.Name + "=" + s'
- allocating quoted-string via '"\"" + v.ToString() + "\""'
- allocating array result via Array.ofSeq + '[' + Join + ']'
- allocating outer wrapping via '{' + Join(strs) + '}'
All 417 existing unit tests continue to pass.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes RuntimeHelpers.formatObject in SwaggerProvider.Runtime by switching from building an intermediate string[] (via Array.map + String.Join) to streaming the formatted output directly into a StringBuilder, reducing allocations while preserving the existing formatting contract.
Changes:
- Reimplemented
formatObjectusing a singleStringBuilderpass. - Preserved existing formatting rules:
{Prop=value; ...}, alphabetical property order, quoted strings, bracketed arrays, andnullrendering.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
2 tasks
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Rewrite
RuntimeHelpers.formatObjectto stream output directly into aStringBuilderinstead of building an intermediate array of strings and then joining them.Motivation
The original implementation allocated several short-lived objects per call:
string[]viaArray.mapp.Name + "=" + s(one allocation per property)"\"" + v.ToString() + "\""(two allocations)Array.ofSeq+"[" + String.Join(...) + "]"(extra wrapping alloc)"{" + String.Join(strs) + "}"(extra wrapping alloc)With
StringBuilder, the same output is produced in a single pass with no intermediate string objects.Change
Array.map+String.Joinwith a singleStringBuilderloop informatObject{Prop=value; ...}formatting, alphabetical property order from the cachedgetPropertiesresult, quoted strings, bracketed arrays,nullfor null valuesTest Status
All 417 existing unit tests pass (no new tests needed — existing
FormatObjectTestsmodule already covers the formatting contract).