Repository navigation
Add consolidated best-practices guidance for ValueStringBuilder - #314
Merged
Merged
Conversation
Co-authored-by: linkdotnet <26365461+linkdotnet@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
linkdotnet
September 15, 2026 10:18
View session
linkdotnet
approved these changes
Sep 15, 2026
There was a problem hiding this comment.
🟡 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). |
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.
The docs covered
ValueStringBuilderfeatures 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
Best practices and pitfallsarticle that consolidates the main usage guidance.Getting started,Advanced usage, andKnown limitations.Guidance now covered explicitly
using var stringBuilder = new ValueStringBuilder();stackallocvs the capacity constructorAsSpan()should be preferred overToString()when astringis not requiredrefValueStringBuilderis a poor fit (async, closures/lambdas, escaping stack-backed state)AppendFormatlimitation around custom format components such as{0:00}and the preferred alternativesDocs structure
docs/site/articles/best_practices.md