Skip to content

Avoid allocation when parsing whitespace-free UTF-8 X-format Guids - #135272

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-guid-utf8-x-allocation
Oct 6, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-guid-utf8-x-allocation

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fix an inverted whitespace check in UTF-8 X-format Guid parsing. The initial scan stops at the first non-whitespace rune, causing whitespace-free input to allocate and copy a temporary byte array.

Stop at the first whitespace rune instead, enabling the existing return-original-span path. For the canonical X-format input, allocations drop from 96 bytes to zero per parse.

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[MemoryDiagnoser]
public class Bench {
    [Benchmark]
    public Guid Parse() =>
        Guid.Parse("{0x00112233,0x4455,0x6677,{0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff}}"u8);
}
Method Toolchain Mean Error Ratio Gen0 Allocated Alloc Ratio
Parse main 157.9 ns 0.20 ns 1.36 0.0114 96 B NA
Parse PR #135272 116.2 ns 0.45 ns 1.00 - - NA

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5d630bcd-7f26-414f-94df-31fe18fb5203
@EgorBo

EgorBo commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

@EgorBot -arm

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[MemoryDiagnoser]
public class Bench {
    [Benchmark]
    public Guid Parse() =>
        Guid.Parse("{0x00112233,0x4455,0x6677,{0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff}}"u8);
}

Note

Benchmark comment generated with GitHub Copilot.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The focused correction matches the helper’s intended behavior, with existing tests covering UTF-8 X-format inputs both with and without whitespace.

Review effort: Balanced
Findings: None

What changed in this PR

Corrects UTF-8 X-format GUID whitespace detection, avoiding an unnecessary allocation for canonical inputs.

Changes:

  • Stops scanning at the first whitespace rune.
  • Preserves the original span when no whitespace exists.
File Description
src/​libraries/​System.Private.CoreLib/​src/​System/​Guid.cs Fixes the inverted whitespace condition.

@EgorBo
EgorBo requested a review from MihaZupan October 6, 2026 12:47
@EgorBo

EgorBo commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

PTAL @MihaZupan

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch

@EgorBo

EgorBo commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

/ba-g #135283

@EgorBo
EgorBo merged commit d223cba into dotnet:main Oct 6, 2026
145 of 147 checks passed
@EgorBo
EgorBo deleted the fix-guid-utf8-x-allocation branch October 6, 2026 16:16
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 8, 2026
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.

3 participants