FastExpressionCompiler icon indicating copy to clipboard operation
FastExpressionCompiler copied to clipboard

Optimize Switch with OpCodes.Switch

Open dadhi opened this issue 1 year ago • 7 comments

Use Switch IL Code for Integer, long, ... (if possible). When int difference is less than 3 it should be used!

See the #133, #397 to reference the other Switch features

dadhi avatar Mar 18 '24 11:03 dadhi

Hello,

I came across this issue and was wondering if you're planning to pick this up? We have some bits of generated code which would heavily benifit from using switch-statements. At the moment, we're calculating the density of our cases and fallback to .NET's Compile method when it exceeds a threshold of 70%. We prefer to use the FastExpressionCompiler, but our code runs faster when switch-statements can be emitted. We have a high performance data processing pipeline were the generated code will be executed many many times in a short period.

Thanks in advance!

victorr99 avatar Aug 29 '25 13:08 victorr99

Yes, it is planned with the priority depending on the demand. Now, I will pick it up sooner. Btw, PRs are welcome.

dadhi avatar Aug 29 '25 13:08 dadhi

Alright, thanks! I'm a bit short in time atm, but I would love to submit a PR. If I'm able to find some time I'll definitely have a look at it.

victorr99 avatar Aug 29 '25 14:08 victorr99

@dadhi I've made a start for the switch-opcode support. ~It does't work now as it yields an InvalidProgramException which I have to look at.~ (not anymore :)) I got a few questions:

  1. you got everything in one big file, do you mind that the support for switches will be moved to other files/classes? I think this will make it easier to extend the support for switches in later releases, as it's likely to grow significantly if you want to add support for bucketing, string-length swtiches or more smart switch usages.
  2. I don't fully understand the output of the PrintIL method, it seems that it's printing a lot more than I'm actually emititing. Can I safely use that method for debugging purposes or should I rely on the Demit methods?
  3. I'm using JetBrains Rider, any recommended settings to make working with large files a bit easier? Rider becomes a bit slow during editing and I'm not getting all editor support I normally get (this is not the reason of question 1 btw).

Here's what I have now: https://github.com/dadhi/FastExpressionCompiler/compare/master...victorr99:FastExpressionCompiler:feature/swtich

The implementation is quite straightforward: it compiles a jump table and emits the switch statement. I think support for all integer types should work. I'm not implementing bucketing (thus: if (value < 50) { switch ... } else if (value < 100) { switch ... } ) nor string-length switches.

victorr99 avatar Sep 05 '25 13:09 victorr99

Hi and thanks for the work. I will look into it a bit later when I have time. Quick answers for 1 and 3.

  1. No, I won't do this just for the switch.
  2. It sucks actually, I don't know how to help here. JetBrains should consider performance as a productivity feature. Personally, I am using VSCode. It works.

dadhi avatar Sep 05 '25 16:09 dadhi

Here's what I have now: https://github.com/dadhi/FastExpressionCompiler/compare/master...victorr99:FastExpressionCompiler:feature/swtich

Thanks for the work, I see that you did a lot. I will use it as the basis to drive my implementation from here.

dadhi avatar Sep 12 '25 09:09 dadhi

Thanks a lot, I'm looking forward for the actual implementation and release!
Please let me know if you've any questions about this initial implementation.

victorr99 avatar Sep 12 '25 10:09 victorr99