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
Angius2mo ago
benchmark it and see
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
my question would be more like for compile time and memory
Angius
Angius2mo ago
Benchmark.bet reports memory as well Not sure about build times
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
what about in terms of design?
Angius
Angius2mo ago
That code will be hidden from the end user anyway
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
Seems like switch wins as I supposed
No description
mindhardt
mindhardt2mo 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
Pedro Gil MoraOP2mo ago
normal, why? same performance
mindhardt
mindhardt2mo ago
Ok then, was worth checking
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
seems it has a slightly overhead cost
No description
Joschi
Joschi2mo ago
Something seems off about those tests. Would you mind sharing the test code?
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
done in LinqPad
No description
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
I'll try providing random values
Angius
Angius2mo ago
I wonder how a switch expression would fare
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
explain please
Angius
Angius2mo 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
Denis2mo ago
Switch expressions is a different way of writing switches, in case that's where the confusion comes from
Angius
Angius2mo ago
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
Action action = number switch {
1 => Method1,
2 => Method2,
//...
};
action();
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
Oh I understand, not switch statement. I dont like them that much (switch expressions)
Angius
Angius2mo ago
You're the first person I meet who doesn't like switch expressions lol
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
I need to execute void methods
Angius
Angius2mo ago
So?
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
I mean for this case of course I use them, a lot
Denis
Denis2mo ago
Switch expressions return the delegate which you can then execute
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
I know, but it introduces some overheads in the lowered version some times this time I want to remove all possible overheads
Angius
Angius2mo 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
Pedro Gil MoraOP2mo ago
still though it should be faster than dictionary, I mean switch expression the variable
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
No description
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
No description
Angius
Angius2mo ago
huh
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
that can be enhanced if expression returns a value, but for void, it's pretty much like this
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
almost the same if it returns an int
No description
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
No description
Joschi
Joschi2mo 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
Pedro Gil MoraOP2mo ago
I tried frozen, normal, immutable.. none of them were as fast as dictionary
Joschi
Joschi2mo ago
You meant switch?
Pedro Gil Mora
Pedro Gil MoraOP2mo ago
yep
RumTery
RumTery4w 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
Pedro Gil MoraOP4w ago
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