meson icon indicating copy to clipboard operation
meson copied to clipboard

Optimize the OptionKey class more

Open dcbaker opened this issue 10 months ago • 8 comments

These changes attempt to make the OptionKey faster to initializer and to compare.

with some light testing this makes initializing options keys roughly 5x as fast (when initializing the same key many times) and comparing two pre-initialized keys roughly twice as fast.

dcbaker avatar Feb 12 '25 19:02 dcbaker

This will conflict with absolutely everything in the optionrefactor branch. :(

jpakkane avatar Feb 12 '25 19:02 jpakkane

I have zero idea why all of the test are failing in CI. They all pass 100% locally

dcbaker avatar Feb 12 '25 20:02 dcbaker

Shouldn't the hash only be used as a quick filter, only to then test the tuple if the hash matches?

bonzini avatar Feb 12 '25 21:02 bonzini

s/Optimze/Optimize/ in the PR title.

QuLogic avatar Feb 13 '25 00:02 QuLogic

Shouldn't the hash only be used as a quick filter, only to then test the tuple if the hash matches?

If the hashes match and they're not equal we have a problem, since that would mean that OptionKey('a') in {OptionKey('a'): ...} would have different behavior than OptionKey('a') == OptionKey('a'), which would seem like a bug.

dcbaker avatar Feb 13 '25 00:02 dcbaker

No, the hash collections always check with __eq__, hashing is only used to start the lookup at a good place.

If you create a subclass of str which always returns 1 for hash, set and dict performance will certainly suck but they will give correct results.

bonzini avatar Feb 13 '25 01:02 bonzini

Okay, in that case the commit is bogus and we should just go back to the tuple == tuple thing

dcbaker avatar Feb 13 '25 02:02 dcbaker

@bonzini is right about hash collisions. Sorry for guiding you in the wrong direction...

Like @dnicolodi said, it is possible to implement the cache without breaking the API, using the __new__ method. Here is my try in this: #14250.

bruchar1 avatar Feb 13 '25 13:02 bruchar1

Is this still needed as the other optimization branch got merged?

jpakkane avatar Mar 01 '25 11:03 jpakkane

No, it should be the same.

bonzini avatar Mar 01 '25 13:03 bonzini