Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use string.Equals over string.Compare in ContentType for improved performance #9724

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

h3xds1nz
Copy link
Contributor

@h3xds1nz h3xds1nz commented Sep 5, 2024

Description

Replaces two uses of string.Compare in ContentType which is used for equality check with string.Equals.

Difference in the last character

Method Mean [ns] Error [ns] StdDev [ns] Code Size [B] Allocated [B]
Original 36.525 ns 0.7577 ns 0.7781 ns 446 B -
PR_EDIT 4.873 ns 0.0175 ns 0.0155 ns 467 B -

Difference in 5th character

Method Mean [ns] Error [ns] StdDev [ns] Code Size [B] Allocated [B]
Original 4.627 ns 0.0493 ns 0.0437 ns 446 B -
PR_EDIT 2.668 ns 0.0138 ns 0.0129 ns 467 B -

Benchmark source strings

//First invocation (last char is different)
return string.Compare("someveryrandomstringthatwillonlydifferintheveryendwewillmakesureofit0",
"someveryrandomstringthatwillonlydifferintheveryendwewillmakesureofit1", 
StringComparison.OrdinalIgnoreCase) == 0;
return string.Equals("someveryrandomstringthatwillonlydifferintheveryendwewillmakesureofit0",
"someveryrandomstringthatwillonlydifferintheveryendwewillmakesureofit1", 
StringComparison.OrdinalIgnoreCase);

//Second invocation (5th char is different)
return string.Compare("some0veryrandomstringthatwillonlydifferintheveryendwewillmakesureofit0",
"some1veryrandomstringthatwillonlydifferintheveryendwewillmakesureofit1", 
StringComparison.OrdinalIgnoreCase) == 0;
return string.Equals("some0veryrandomstringthatwillonlydifferintheveryendwewillmakesureofit0",
"some1veryrandomstringthatwillonlydifferintheveryendwewillmakesureofit1", 
StringComparison.OrdinalIgnoreCase);

Customer Impact

Improved performance.

Regression

No.

Testing

Local build.

Risk

None, you can't ruin anything with this swap.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners September 5, 2024 19:51
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Sep 5, 2024
@h3xds1nz
Copy link
Contributor Author

h3xds1nz commented Sep 5, 2024

CI failure caused by #9632 requesting System.Drawing.Common from .NET 8 (v8.0.8).

@lindexi
Copy link
Member

lindexi commented Sep 6, 2024

Reference: #7836

I forgot about the subsequent optimization...

@h3xds1nz
Copy link
Contributor Author

h3xds1nz commented Sep 6, 2024

Now it shall be complete haha.

@lindexi
Copy link
Member

lindexi commented Sep 6, 2024

Good job

@harshit7962
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@siagupta0202
Copy link
Contributor

LGTM

@harshit7962 harshit7962 merged commit cae5cef into dotnet:main Oct 3, 2024
8 checks passed
@harshit7962
Copy link
Member

@h3xds1nz Thank you for your contributions.

@h3xds1nz
Copy link
Contributor Author

h3xds1nz commented Oct 3, 2024

@harshit7962 Thank you for including them.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 3, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants