News for hackers. Stuff that matters.
Re:Faster IndexOf for Substrings in .NET by ryanmccullagh on Tuesday January 04, 2022 @03:03AM (#29792496)
the c# code is looking a lot like C with the goto’s. Great to see this style in c#.
Re:Faster IndexOf for Substrings in .NET by andix on Tuesday January 04, 2022 @05:02AM (#29793178)
If you want high performance C# code, it needs to look like this. Very much like C, rather unreadable.

But form my experience, only a very tiny fraction of your code needs to be like that, as in most of the code maintainability and readability is much more important than performance.

Re:Faster IndexOf for Substrings in .NET by rawling on Tuesday January 04, 2022 @03:21AM (#29792606)
3 gotos replacing while(true) loops, and one... just reducing nesting?
Re:Faster IndexOf for Substrings in .NET by dubiousconst281 on Tuesday January 04, 2022 @03:27AM (#29792639)
I'm guessing the reason is because of code gen issues, there's a lot of workarounds throughout the BCL code base.
Re:Faster IndexOf for Substrings in .NET by vips7L on Tuesday January 04, 2022 @09:57AM (#29795424)
Seems like every .NET optimization I see is revolved around getting the JIT to generate the right assembly.
Re:Faster IndexOf for Substrings in .NET by ygra on Tuesday January 04, 2022 @10:31AM (#29795790)
There's a lot more that doesn't have to do with the JIT. Yes, in some cases it's about providing hints to the JIT to generate better code. Things like casting to unsigned types for example. But the weird ones (one I came across recently was something like condition ? true : false) are fairly rare. Which makes sense in a way: Not all code is that performance-critical that you have to save two instructions. But for things where the baseline performance is a few nanoseconds in many cases, it probably makes sense taking whatever measures you can to ensure that the machine code looks like you'd expect (and make a note to improve the JIT, which also happens regularly).

Technically you could count vectorization in the category of codegen issues, but since the JIT won't output vector instructions on its own I don't think that's really fair (and you have the same issues in languages like C or C++ that do auto-vectorize, but not reliably or good enough).

What is also frequently optimized, is allocation behavior by ensuring that the code doesn't allocate more memory than needed and in a lot of cases no memory at all. And C# has a lot of tools for that kind of optimization that don't really veer into code that looks atypical.