Switch to .ToString() instead of $"{variable}" in the codebase #668

Closed
opened 2026-01-29 17:41:17 +00:00 by claunia · 2 comments
Owner

Originally created by @MasterJediLNR on GitHub (Dec 10, 2022).

Describe the bug
In reviewing the code for the Uri.ToODataUri(), I saw that you folks are doing string interpolation for writing QueryString values. Activating LearningMode™️, I was curious to see if that methodology was faster in .NET 7 than using ToString(). So I found a benchmark and ran it on .NET 7.

image

Turns out, even with the improvements to string manipulation, ToString() is more than twice as fast as $"{variable}".

There are also places in the ToODataUri method that interpolate values that are already strings, slowing down the method just for the sake of visual consistency.

You folks should heavily consider swapping out that code wherever possible to speed up the library.

Originally created by @MasterJediLNR on GitHub (Dec 10, 2022). **Describe the bug** In reviewing the code for the `Uri.ToODataUri()`, I saw that you folks are doing string interpolation for writing QueryString values. Activating LearningMode™️, I was curious to see if that methodology was faster in .NET 7 than using `ToString()`. So I [found a benchmark](https://gist.github.com/mattwarren/d2be775eec5a7adba928) and ran it on .NET 7. <img width="602" alt="image" src="https://user-images.githubusercontent.com/95591152/206867858-1e83a57e-9bf5-42a3-9885-428b13911de9.png"> Turns out, even with the improvements to string manipulation, `ToString()` is more than twice as fast as `$"{variable}"`. There are also places in the ToODataUri method that interpolate values that are already strings, slowing down the method just for the sake of visual consistency. You folks should heavily consider swapping out that code wherever possible to speed up the library.
Author
Owner

@enchev commented on GitHub (Dec 11, 2022):

I’m afraid that we will not do that. First because using ToString() requires additional check if the variable is null and second since building ODats url is not some heavy data processing and usually one to three variables are converted to string.

@enchev commented on GitHub (Dec 11, 2022): I’m afraid that we will not do that. First because using ToString() requires additional check if the variable is null and second since building ODats url is not some heavy data processing and usually one to three variables are converted to string.
Author
Owner

@MasterJediLNR commented on GitHub (Dec 11, 2022):

That reasoning doesn't make any sense, you already have those variable assignments wrapped in null checks. There is zero additional cost at that point.

Why would you leave the code less performant because you think it's low impact, without any evidence to support that assumption? I'm building complex OData URLs in my app, and the amount of time savings I'd be able to make up is more than the 50ms of the human perceptual range.

@MasterJediLNR commented on GitHub (Dec 11, 2022): That reasoning doesn't make any sense, you [already have those variable assignments wrapped in null checks](https://github.com/radzenhq/radzen-blazor/blob/master/Radzen.Blazor/OData.cs#L240). There is zero additional cost at that point. Why would you leave the code less performant because you think it's low impact, without any evidence to support that assumption? I'm building complex OData URLs in my app, and the amount of time savings I'd be able to make up is more than the 50ms of the human perceptual range.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#668