Skip to content

fix: prefer AsSpan over Substring where span overloads are available#123

Merged
vbreuss merged 1 commit into
mainfrom
chore/prefer-asspan-over-substring
May 10, 2026
Merged

fix: prefer AsSpan over Substring where span overloads are available#123
vbreuss merged 1 commit into
mainfrom
chore/prefer-asspan-over-substring

Conversation

@vbreuss

@vbreuss vbreuss commented May 10, 2026

Copy link
Copy Markdown
Member

No description provided.

@vbreuss vbreuss self-assigned this May 10, 2026
Copilot AI review requested due to automatic review settings May 10, 2026 15:46
@vbreuss vbreuss added the bug Something isn't working label May 10, 2026
@vbreuss vbreuss enabled auto-merge (squash) May 10, 2026 15:47
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Test Results

    7 files  ±0      7 suites  ±0   7s ⏱️ -1s
  508 tests ±0    507 ✅ ±0  1 💤 ±0  0 ❌ ±0 
2 968 runs  ±0  2 967 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit db19153. ± Comparison against base commit 97e4371.

@vbreuss vbreuss merged commit ae9e23e into main May 10, 2026
16 checks passed
@vbreuss vbreuss deleted the chore/prefer-asspan-over-substring branch May 10, 2026 15:48

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.

Pull request overview

This PR optimizes string slicing by using span-based APIs on newer TFMs (net8.0+ / net10.0) to avoid allocations, while keeping Substring for netstandard2.0 compatibility.

Changes:

  • Use Guid.TryParse(ReadOnlySpan<char>) with value.AsSpan(...) for net8.0+ in ExpectationJsonConverter.
  • Use StringBuilder.Append(ReadOnlySpan<char>) with line.AsSpan(...) for net8.0+ in TrimCommonWhiteSpace.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
Source/aweXpect.Json/Json/ExpectationJsonConverter.cs Uses AsSpan for parsing the GUID suffix on net8.0+ to avoid substring allocation.
Source/aweXpect.Json/Helpers/StringExtensions.cs Uses AsSpan when appending trimmed lines on net8.0+ to reduce allocations.

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

Comment on lines +53 to +57
#if NET8_0_OR_GREATER
sb.Append('\n').Append(line.AsSpan(commonWhiteSpace.Length));
#else
sb.Append('\n').Append(line.Substring(commonWhiteSpace.Length));
#endif
@github-actions

Copy link
Copy Markdown

👽 Mutation Results

Mutation testing badge

aweXpect.Json

Details
File Score Killed Survived Timeout No Coverage Ignored Compile Errors Total Detected Total Undetected Total Mutants
Helpers/StringExtensions.cs 75.00% 18 6 0 0 9 4 18 6 37
Json/ExpectationJsonConverter.cs 75.00% 6 0 0 2 5 6 6 2 19

The final mutation score is 75.00%

Coverage Thresholds: high:80 low:60 break:0

@github-actions

Copy link
Copy Markdown

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.63GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.203
[Host] : .NET 8.0.26 (8.0.26, 8.0.2626.16921), X64 RyuJIT x86-64-v3
DefaultJob : .NET 8.0.26 (8.0.26, 8.0.2626.16921), X64 RyuJIT x86-64-v3

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
IsValidJson_aweXpect 1.673 μs 0.0074 μs 0.0061 μs 0.0820 0.0038 0.0019 1.38 KB

@github-actions

Copy link
Copy Markdown

This is addressed in release v1.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants