-
Notifications
You must be signed in to change notification settings - Fork 1.8k
What if we did not use dictionaries? #28322
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
base: main
Are you sure you want to change the base?
Conversation
@albyrock87 you did some good work there, but do you think that this idea could make it better? Did you try an approach like this at all? |
@mattleibow may you expand on this?
I don't understand what use case was not covered. |
Nothing actually. But theoretically not using I think it is also the dictionary. So we construct a dictionary, create delegates/actions, insert into the dic, then have to lookup and execute - all of shich is slower than exeuting simple IL. This PR is an idea to potentially remove dictionaries and delegates from the equation.
You are caching the On a separate note, I did see that you run |
@mattleibow So why are you saying these two sentences?
What have I broken? Regarding performance
Looking at the numbers you reported it seems to me the switch is slower than my approach.
Now taking your final timings: 156.64 ms and divide by the multiplier we get 116.93 ms which is way slower than 61.61 ms.
That method is invoked just once because it sets all the 3 fields. Properties:
|
Nothing. I am trying something totally different in where we don't use dictionaries with actions at all. Your optimizations were about optimizating the lists of actions and caching keys. My idea is to not do anything and just use a giant switch statement. I had to revert becasue you made the snapshot code cache the actions, and this PR hopes to remove the need for the the list of actions and keys entirely and just be a direct call to the static method. And about the perf yeah, I know it is slower than your work, mostly because I had to revert and it is all hacks right now. Just an idea. Once I have fixed my code a bit more, I will have to see if it is faster. But, the comparison after reverting shows that it is a fair bit faster than the original implemention, which confirms the hypothesis that a switch is faster than the dictionary scanning we did before. So yeah, your code is way better than what we had before, but I am basically wanting to convert the updates to just be a call to Update, then switch and then execute the MapPropertyName directly. |
I see. I was concerned I broke something:) That said, I think your approach may give good results. I've seen nested switch patterns done like this:
I think it should bring better performance. |
That probably doesn't make much sense. C# compiler uses hash function for string switch-case. You can take a look what switch-case is translated into: |
@Dreamescaper that's amazing! Thanks for sharing. So at this point the only big improvement could be something like a FrozenDictionary, possibly generated at compile time. For reference: dotnet/runtime#77891 @ "Source Generators" |
return; | ||
|
||
// This property is a special one and needs to be set before other properties. | ||
MapContainerView(viewHandler, view); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be TryUpdateProperty
.
Description of Change
What if we did not implement core handler mappers using a big dictionary? We know exactly what there will be at build time.
This has overhead:
This PR attempts to investigate a possiblity using a giant switch statement.
Benchmarks on:
BEFORE
With #28077, @albyrock87 made things are really fast:
AFTER
However, that PR was too good and cached all the things. However, it cached the delegates which we don't even have. So I had to revert that and add a TryUpdateProperty instead. This meant that after the revert with the base code:
Terrible, but if I then switch to the new mapper (assuming I did it right):
This means that there is a large improvement just by reducing the lookups. If we were to replace all our implementations with a giant switch statement, we could see a large improvement in performance overall.
Issues Fixed
Fixes #