Dictionary vs switch in source generated code

I have doubt. Im generating a compiled communication framework and the "endpoint handler" is a dictionary is int64 key-based with actions processing the context. keyvalue pairs are generated with my source generator. My question is. if I generate the equivalent switch to handle like 150 cases in compile time. would be inefficient?
40 Replies
Angius
Angius4w ago
benchmark it and see
Pedro Gil Mora
my question would be more like for compile time and memory
Angius
Angius4w ago
Benchmark.bet reports memory as well Not sure about build times
Pedro Gil Mora
what about in terms of design?
Angius
Angius4w ago
That code will be hidden from the end user anyway
Pedro Gil Mora
Seems like switch wins as I supposed
No description
mindhardt
mindhardt4w ago
Was this a frozen dict or a normal one? Iirc there was a dialog here that dict wins a switch in speed above some ~80 branches
Pedro Gil Mora
normal, why? same performance
mindhardt
mindhardt4w ago
Ok then, was worth checking
Pedro Gil Mora
seems it has a slightly overhead cost
No description
Joschi
Joschi4w ago
Something seems off about those tests. Would you mind sharing the test code?
Pedro Gil Mora
done in LinqPad
No description
Pedro Gil Mora
I'll try providing random values
Angius
Angius4w ago
I wonder how a switch expression would fare
Pedro Gil Mora
explain please
Angius
Angius4w ago
Explain... what exactly? You benchmark dictionary vs switch statement I wonder how performant would a switch expression be And whether there would even be a difference
Denis
Denis4w ago
Switch expressions is a different way of writing switches, in case that's where the confusion comes from
Angius
Angius4w ago
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
Pedro Gil Mora
Oh I understand, not switch statement. I dont like them that much (switch expressions)
Angius
Angius4w ago
You're the first person I meet who doesn't like switch expressions lol
Pedro Gil Mora
I need to execute void methods
Angius
Angius4w ago
So?
Pedro Gil Mora
I mean for this case of course I use them, a lot
Denis
Denis4w ago
Switch expressions return the delegate which you can then execute
Pedro Gil Mora
I know, but it introduces some overheads in the lowered version some times this time I want to remove all possible overheads
Angius
Angius4w ago
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
is not that different from the dictionary approach, code-wise
var dict = Dictionary<int, Action> {
[1] = Method1,
[2] = Method2,
//...
};
dict[number]();
var dict = Dictionary<int, Action> {
[1] = Method1,
[2] = Method2,
//...
};
dict[number]();
Hence my suggestion to benchmark it "some times" doesn't mean this time
Pedro Gil Mora
still though it should be faster than dictionary, I mean switch expression the variable
Angius
Angius4w ago
huh
Pedro Gil Mora
that can be enhanced if expression returns a value, but for void, it's pretty much like this
Pedro Gil Mora
almost the same if it returns an int
No description
Joschi
Joschi4w ago
You use an immutable dictionary here. These are actually significantly slower in lookup time compared to frozen dictionaries. Iirc correctly they are even slower than normal dictionaries.
Pedro Gil Mora
I tried frozen, normal, immutable.. none of them were as fast as dictionary
Joschi
Joschi4w ago
You meant switch?
Pedro Gil Mora
yep
RumTery
RumTery6d ago
If your keys are just 0, 1, 2, 3, why not array? Also, calling delegate will never be as fast as direct method call in switch
Pedro Gil Mora
There's a difference of course. My test it's about which one reflect fastest lookup rather than measuring the final method to be called inside every case
Want results from more Discord servers?
Add your server