Skip to content

Remove string allocations - #9718

Merged
cartermp merged 1 commit into
dotnet:masterfrom
kerams:patch-2
Jul 21, 2020
Merged

Remove string allocations#9718
cartermp merged 1 commit into
dotnet:masterfrom
kerams:patch-2

Conversation

@kerams

Copy link
Copy Markdown
Contributor

No description provided.

@cartermp

cartermp commented Jul 20, 2020

Copy link
Copy Markdown
Contributor

The other two are obvious but I was curious about the removal of substring and use of span. This benchark: https://gist.github.com/cartermp/da23761991bd1cf0f179edfe1d00b737

yielded these results:

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.388 (2004/?/20H1)
Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100-preview.6.20318.15
[Host] : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT DEBUG
.NET 4.7.2 : .NET Framework 4.8 (4.8.4180.0), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
MethodJobRuntimenameMeanErrorStdDevRatioGen 0Gen 1Gen 2Allocated
IsValidStrippedName.NET 4.7.2.NET 4.7.2abc123thxypo37.52 ns0.484 ns0.429 ns1.000.0134--56 B
IsValidStrippedNameSpan.NET 4.7.2.NET 4.7.2abc123thxypo26.21 ns0.153 ns0.143 ns0.70----
IsValidStrippedName.NET Core 3.1.NET Core 3.1abc123thxypo29.98 ns0.321 ns0.300 ns1.000.0114--48 B
IsValidStrippedNameSpan.NET Core 3.1.NET Core 3.1abc123thxypo19.67 ns0.149 ns0.125 ns0.66----
IsValidStrippedName.NET 4.7.2.NET 4.7.2xyz35.00 ns0.328 ns0.307 ns1.000.0076--32 B
IsValidStrippedNameSpan.NET 4.7.2.NET 4.7.2xyz26.49 ns0.181 ns0.170 ns0.76----
IsValidStrippedName.NET Core 3.1.NET Core 3.1xyz28.86 ns0.489 ns0.458 ns1.000.0076--32 B
IsValidStrippedNameSpan.NET Core 3.1.NET Core 3.1xyz18.77 ns0.172 ns0.161 ns0.65----

(note: github struggles to render the actual proper values in tables)

@cartermpcartermp 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.

Great changes, thanks!

@TIHanTIHan 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.

The changes look good, but I would like to see more information regarding these changes.

Do the changes actually give a measurable performance improvement in the overall tooling? Can you provide evidence?

Also, more description of the PR would be helpful too instead of it being empty.

Normally I would 'request changes', but since this isn't touching the internals of the compiler - I can be a bit more relaxed.

@TIHan

Copy link
Copy Markdown
Contributor

To note: The only reason why I made the comment that I did was because we have to use AsSpan and depending on the case, can make me a little nervous in terms of readability if we start doing that everywhere.

@kerams

Copy link
Copy Markdown
ContributorAuthor

I was just browsing the repo and noticed these things could be improved, so that's what I did thinking any performance uplift (and I'm pretty sure this change will have a completely negligible impact on its own) is better than nothing, admittedly with no regard to readability. That's all there is to say, really ;)

@KevinRansomKevinRansom 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.

These are good changes. I agree the perf improvements will be minuscule, we normally try to avoid drive-by churn because of unintended consequences, however, these look focused enough that the risk is pretty low.

Thank you for taking care of this

Kevin

@cartermp

Copy link
Copy Markdown
Contributor

These changes may not actually be miniscule - one of these cases removes a new string allocation for every single span of text we classify in a document. For large documents that could be meaningful.

@kerams

Copy link
Copy Markdown
ContributorAuthor

@cartermp, how would one go about measuring the difference on a sizeable file/codebase?

@cartermp

Copy link
Copy Markdown
Contributor

Measuring the difference is best done with PerfView and capturing a trace before/after your changes for the same codebase and operations done in the codebase. It's kind of annoying and time-consuming work. In general I think this is fine to take without advanced analysis since the improvements are pretty obvious. But for less obvious things we would at least need to see some more information about the current performance characteristics and a writeup about how the new code improves on it.

@cartermp
cartermp merged commit 77d9e23 into dotnet:masterJul 21, 2020
@kerams
kerams deleted the patch-2 branch July 22, 2020 05:42
@kerams
kerams restored the patch-2 branch July 25, 2020 07:36
@kerams
kerams deleted the patch-2 branch July 25, 2020 07:37
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Feb 23, 2021
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Jan 26, 2022
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Jan 26, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@kerams@cartermp@TIHan@KevinRansom