Prefer span slices over substrings in the F# instructions - #20642
Open
xperiandri wants to merge 3 commits into
Open
xperiandri wants to merge 3 commits into
xperiandri wants to merge 3 commits into
Conversation
A string slice that is only inspected is a `ReadOnlySpan<char>`; a `Substring` or `ToString()` copies it on every call. Materialise only the value that leaves the function or is stored, and use `ReadOnlyMemory<char>` for a slice that outlives the stack frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address the identified guidance inconsistencies and System.Memory applicability issue.
Review effort: Lite
Findings: 1
What changed in this PR
Updates F# guidance to prefer span-based string slicing and avoid unnecessary allocations.
Changes:
- Recommends
ReadOnlySpan<char>for temporary inspection. - Recommends
ReadOnlyMemory<char>for stored or long-lived slices. - Adds allocation-free comparison examples.
| File | Summary |
|---|---|
.github/instructions/FSharp.instructions.md |
Adds span and memory slicing guidance; requires corrections for package availability, span lifetimes, and comparison consistency. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
This comment has been minimized.
This comment has been minimized.
Contributor
|
🔍 Tooling Safety Check — Affects-Agent-Config
|
The rule above asks for one on every `StartsWith`, and the span examples have none, which reads as a contradiction rather than what it is: the span overloads that take no comparison compare the characters, so they are ordinal already, while the `string` overloads default to the current culture. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
MemoryExtensions has StringComparison overloads for ReadOnlySpan<char> even on netstandard2.0, so spans need no exception from the rule; the overloads without one are the generic element-wise ones and only happen to be ordinal. The span Contains also covers the missing String.Contains overload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.

A string slice that is only inspected (compared, trimmed, scanned, matched against a prefix) should be a
ReadOnlySpan<char>, not aSubstringorToString()that copies it on every call. Only the value that leaves the function or is stored gets materialised, and a slice that outlives the stack frame is aReadOnlyMemory<char>.src/CompilerreferencesSystem.Memoryonnetstandard2.0, so the API is available everywhere the rule applies.🤖 Generated with Claude Code