Skip to content

Add consolidated best-practices guidance for ValueStringBuilder - #314

Merged
linkdotnet merged 1 commit into
mainfrom
copilot/check-documentation-extend
Sep 15, 2026
Merged

linkdotnet merged 1 commit into
mainfrom
copilot/check-documentation-extend

Conversation

Copilot AI commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

The docs covered ValueStringBuilder features and limitations, but the operational guidance was fragmented across multiple pages. This updates the documentation to give both new and advanced users a single place to find the main pitfalls, safe defaults, and performance-oriented usage patterns.

  • What changed

    • Added a dedicated Best practices and pitfalls article that consolidates the main usage guidance.
    • Linked the new guidance from Getting started, Advanced usage, and Known limitations.
    • Updated the docs navigation so the guidance is discoverable early instead of being buried across multiple pages.
  • Guidance now covered explicitly

    • Safe default usage with using var stringBuilder = new ValueStringBuilder();
    • When to use the default constructor vs stackalloc vs the capacity constructor
    • Why AsSpan() should be preferred over ToString() when a string is not required
    • Why helpers should take the builder by ref
    • Where ValueStringBuilder is a poor fit (async, closures/lambdas, escaping stack-backed state)
    • The AppendFormat limitation around custom format components such as {0:00} and the preferred alternatives
  • Docs structure

    • New article: docs/site/articles/best_practices.md
    • Cross-links added from the starter and advanced docs so the guidance supports both onboarding and deeper optimization work
Span<char> buffer = stackalloc char[64];
using var stringBuilder = new ValueStringBuilder(buffer);

stringBuilder.Append("ID-");
stringBuilder.Append(42, "D5");

ReadOnlySpan<char> value = stringBuilder.AsSpan();

Co-authored-by: linkdotnet <26365461+linkdotnet@users.noreply.github.com>
@linkdotnet
linkdotnet marked this pull request as ready for review September 15, 2026 10:19
Copilot AI lite review requested due to automatic review settings September 15, 2026 10:19
@linkdotnet
linkdotnet merged commit e65db37 into main Sep 15, 2026
2 checks passed
@linkdotnet
linkdotnet deleted the copilot/check-documentation-extend branch September 15, 2026 10:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Address the pooled-array leak and the two documentation corrections.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Consolidates ValueStringBuilder guidance into a discoverable best-practices article and links it from existing documentation.

Changes:

  • Adds guidance on construction, disposal, spans, lifetimes, and formatting.
  • Links the guidance from onboarding, advanced usage, and limitations pages.
  • Adds the article to documentation navigation.
File summaries
File Summary
docs/site/articles/toc.yml Adds the best-practices navigation entry.
docs/site/articles/known_limitations.md Adds a cross-link; typo correction needed (nit, 3 votes).
docs/site/articles/getting_started.md Adds recommended defaults and a cross-link.
docs/site/articles/best_practices.md Adds consolidated guidance. A moderate issue (3 votes) requires disposing the builder or increasing the buffer to avoid an ArrayPool leak; a lifetime clarification is also needed (nit, 3 votes).
docs/site/articles/advanced_usage.md Adds a cross-link to the best-practices guidance.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

private static string FormatId(int value)
{
Span<char> buffer = stackalloc char[32];
var stringBuilder = new ValueStringBuilder(buffer);
Comment on lines +106 to +108
Since `ValueStringBuilder` is a `ref struct`, it cannot be used in `async` methods, iterator methods, or lambda/closure scenarios that capture it.

When your flow needs those language features, build the string in a synchronous helper first or fall back to `System.Text.StringBuilder` if the value must live longer.
* Can't be used in methods that use the `yield` keyword

If not off this applies to your use case, you are good to go. Using `ref struct` is a trade for performance and fewer allocations in contrast to its use cases.
If not off this applies to your use case, you are good to go. Using `ref struct` is a trade for performance and fewer allocations in contrast to its use cases. For practical guidance on when these trade-offs are acceptable and how to work with them safely, see [Best practices and pitfalls](xref:best_practices).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants