Skip to content

[repo-assist] perf: replace formatObject Array.map+Join with StringBuilder#440

Merged
sergey-tihon merged 2 commits into
masterfrom
repo-assist/perf-formatobject-stringbuilder-c640d34972ab61ea
May 17, 2026
Merged

[repo-assist] perf: replace formatObject Array.map+Join with StringBuilder#440
sergey-tihon merged 2 commits into
masterfrom
repo-assist/perf-formatobject-stringbuilder-c640d34972ab61ea

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Rewrite RuntimeHelpers.formatObject to stream output directly into a StringBuilder instead of building an intermediate array of strings and then joining them.

Motivation

The original implementation allocated several short-lived objects per call:

  • An intermediate string[] via Array.map
  • Per-property strings from p.Name + "=" + s (one allocation per property)
  • Quoted-string temporaries: "\"" + v.ToString() + "\"" (two allocations)
  • Array element list via Array.ofSeq + "[" + String.Join(...) + "]" (extra wrapping alloc)
  • Outer wrapping: "{" + String.Join(strs) + "}" (extra wrapping alloc)

With StringBuilder, the same output is produced in a single pass with no intermediate string objects.

Change

  • Replaced Array.map + String.Join with a single StringBuilder loop in formatObject
  • Logic is identical: {Prop=value; ...} formatting, alphabetical property order from the cached getProperties result, quoted strings, bracketed arrays, null for null values

Test Status

All 417 existing unit tests pass (no new tests needed — existing FormatObjectTests module already covers the formatting contract).

SwaggerProvider.Tests  Total: 417, Errors: 0, Failed: 0, Skipped: 1, Time: 0.930s

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@79c99dfd73f3b7ad8ab2b0f4944838018dbe4736

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>
@sergey-tihon sergey-tihon marked this pull request as ready for review May 17, 2026 21:15
Copilot AI review requested due to automatic review settings May 17, 2026 21:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 formatObject using a single StringBuilder pass.
  • Preserved existing formatting rules: {Prop=value; ...}, alphabetical property order, quoted strings, bracketed arrays, and null rendering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sergey-tihon sergey-tihon merged commit c1fd6d0 into master May 17, 2026
6 checks passed
@sergey-tihon sergey-tihon deleted the repo-assist/perf-formatobject-stringbuilder-c640d34972ab61ea branch May 17, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants