-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider using stackalloc for string.Split #6266
Comments
CC @danmosemsft @AlexGhiondea |
Sounds like a great idea for an enhancement. Anyone interested in picking this up? |
I'm looking into it, seems like a nice introduction to coreclr. |
@4real I sent you the invite, please ping us when you accept. @danmosemsft I thought you have same admin permissions as I do ... we should fix that ;-) |
@karelz Accepted invite! @danmosemsft Thanks, I'll have a look at Benchmark.NET. I take it that sufficient proof is empirical evidence a new solution is significantly faster and there being a line of reasoning as to why that is? Should I write a performance test for this issue specifically? (Apologies if this is covered by the documentation, I haven't processed it all yet.) |
@4real there aren't really hard rules but generally we're looking for
Of course there are plenty of perf changes where it's a no brainer and sometimes we don't even bother measuring (eg just removing redundant code). |
Incidentally for profiling you can use the Visual Studio 2017 profiler but many of us use PerfView as it's owned by our team. It's very powerful and has good tutorials. |
Thanks, I'll have a look at PerfView too. From what I can tell, there are XUnit.Performance tests in the codebase for the GC and JIT, but not mscorlib. So instead, I will create a Benchmark.NET performance test for string.Split which will not be included in the commit, but for which I will post the results of here - does this seem like the correct approach? I will however create a functional test to show correctness of changes to string.Split if deemed not covered by existing tests. |
@4real correct, that's what we normally do -- have a throwaway perf test -- of course you could archive it in a gist with a link in the PR in case we want it again. There are lots of XUnit.Performance tests in the repo and they are run regularly but those are to catch regressions and there is a certain cost to maintaining them (watching results). So we could consider adding one if it was an important scenario that might regress. Normally we don't. In fact we may delete some of those tests -- they need an audit. |
@4real still planning to take a look? just realized I never checked back. it would be nice to optmize this. |
I would like to give it a try if nobody is looking |
Hello, feel free to take it over, I've just realized how much time this has been resting. My attention has been shifted elsewhere unfortunately and I wouldn't start working on this within reasonable time. |
@cod7alex I sent you Collaborator invite - that will allow to assign the issue to you (GH limitation). Ping me when you accept. |
@cod7alex to restate the above, we typically use PerfView to profile, Benchmark.NET to prove the win (post results here). To run corefx tests (where most tests are) against changes made in coreclr (where string is) look at https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/developer-guide.md#testing-with-private-coreclr-bits to see the flag to force this to happen. If the benchmark results and change is satisfactory we will want to check in benchmark tests.Currently we still use xunit.perf for checked-in tests (that may change) - see existing tests in corefx\src\System.Runtime\tests\Performance\Perf.String.cs. This may be helpful I think we need better docs on using Benchmark.NET against private coreclr changes, based on https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/dogfooding.md. Hopefully @ViktorHofer can throw up some notes he has. We can also help answer questions. |
thank you @danmosemsft |
BenchmarkDotNet instructions not yet reviewed by someone else available here: dotnet/corefx#25612 |
@ViktorHofer this guide is very helpful, i was able to run benchmark. |
Can someone help me with Spans? If i have the code like the following, can i pass Span to function and fill it and what is the way to do it if yes? Is it even a good idea?
|
@cod7alex this is explicitly forbidden by design of |
thanks @Rattenkrieg, I am not really familiar with stackalloc. So it means I should inline this everywhere it is needed and return the filled span from methods? |
@Rattenkrieg many thanks |
@cod7alex By the way, you don't have to stackalloc a temporary ptr and then create a span from that. You can just stackalloc a Span directly. https://github.com/dotnet/corefx/pull/24212/files |
@jamesqo yes, i have found that information recently. Thank you. |
Right now,
string.Split
allocates a new int array for both thechar
andstring
versions. We might want to consider usingstackalloc
here instead for strings of lengths under a certain threshold.Alternatively, we could introduce some type of
ArrayPool
-like API to mscorlib and use that, or just forego allocating anything altogether and simply count the length needed for the resulting array on our first pass.The text was updated successfully, but these errors were encountered: