Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Avoid heap allocating in char.ConvertFromUtf32 - #6141

Merged
jkotas merged 6 commits into
dotnet:masterfrom
jamesqo:char-stack
Jul 7, 2016
Merged

jkotas merged 6 commits into
dotnet:masterfrom
jamesqo:char-stack

Conversation

@jamesqo

@jamesqo jamesqo commented Jul 6, 2016

Copy link
Copy Markdown

Currently char.ConvertFromUtf32 creates a new char array of length 2, modifies it, and then creates a string out of that. This is a bit wasteful, since it means an extra heap allocation that won't be used by the caller. This changes that to allocate 2 chars' worth of space on the stack via declaring a local uint, then taking the address of that, modifying it, and passing it into the string(char*, int, int) constructor.

I didn't use stackalloc here since from my benchmarks it seems to be a bit slower than declaring a local variable; see results for heap, stackalloc, and local uint (here is the source code). Also note that since most of the overhead in those timings is probably allocating/calling the string ctor, the differences may be greater than they reflect.

Separated out from #4881 (which should probably be merged sometime)

cc @jkotas

Comment thread src/mscorlib/src/System/Char.cs Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Might as well fix the misspelling while making changes here: sumplementary -> supplementary

Comment thread src/mscorlib/src/System/Char.cs Outdated

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.

I do not think that the comment about stackalloc is relevant here. It would be generally better to allocate the space as uint here regardless - given how the optimizations that the JIT is capable of.

A different problem is that dotnet/coreclr#5719 is pretty confused. I do not think it is a real problem. I have just added a comment to it. We have been using stackalloc for years for unsafe perf optimizations with pretty clear results.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@jkotas I've removed the comment about stackalloc.

@jkotas

jkotas commented Jul 7, 2016

Copy link
Copy Markdown
Member

I always have mixed feelings about optimizing rare paths, in slow convenience methods, using unsafe code. It is asking for security bug to sneak in. Given that char is low level type, this one is probably ok.

Comment thread src/mscorlib/src/System/Char.cs Outdated

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.

Also, the method needs to be marked with SecuritySafeCritical critical attribute now because of it contains unsafe code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

@jkotas
jkotas merged commit ebbff97 into dotnet:master Jul 7, 2016
@jkotas

jkotas commented Jul 7, 2016

Copy link
Copy Markdown
Member

Could you please port the change to CoreRT as well?

@jkotas

jkotas commented Jul 7, 2016

Copy link
Copy Markdown
Member

Thanks!

@jamesqo
jamesqo deleted the char-stack branch July 7, 2016 23:22
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants