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
Optimization for range-based switch #22997
Comments
While there's no question that For a start, you should ensure that the benchmarks are representative for your scenario. I doubt that random numbers reflect actual Unicode text. Also, the binary search with array version is pretty poor, there are numerous improvements you could make there. Just a few examples:
I don't know if doing that is enough to catch up And it's rather interesting that BinaryIf is faster than Switch. The classic switch does use binary search but it also uses switch tables here and there. The fact that BinaryIf is faster would imply that the classic switch choice of using tables isn't that great. Though it all depends on the value distribution... |
Interesting. I am not sure that a lot can be done to help It would be much easier to emit efficient BTW, compiler should be able to detect "contiguous range of values" cases and emit them as range checks, so at least in theory the flat switch, even though unwieldy in source, should result in IL that employs range checks. I wonder if that really happens in this scenario... |
I've tried the suggested improvements: ufcpp/GraphemeSplitter@4159c73
The benchmark data is not ideal but I don't want so much precision. |
Ported from dotnet/csharplang#198 (comment)
TL; DR
I want the following
switch
to be optimized toO(log N)
.Background
I recently implemented a Unicode grapheme breaking algorithm. In this library, a
switch
statement generated from GraphemeBreakProperty table has about 20 thousand lines.Cascading
if
statement andcase
-when
versions of that is much shorter but 25 times slower than theswitch
.Measurement
I measured performance of:
if
switch
with expandedcase
sswitch
withwhen
clauses(My PC is Core i7-4790 3.60GHz.)
A result:
FYR
limit number
Measurement results with limiting number:
20 lines:
50 lines:
100 lines:
200 lines:
500 lines:
1000 lines:
executable size
I also measured executable sizes.
build script
Debug (
/o-
) build:Release (
/o+
) build:compilation time
In addition, it requires 50 seconds to compile the 20 thousand-line switch-case.
The text was updated successfully, but these errors were encountered: