Skip to content
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

OpenHashMaps.mergePRIMITIVE: Avoid double-hashing key #337

Merged
merged 2 commits into from
Nov 26, 2024

Conversation

mhansen
Copy link
Contributor

@mhansen mhansen commented Nov 19, 2024

The #if VALUES_PRIMITIVE && ! VALUE_CLASS_Boolean comes from the superclass in Map.drv:

#if VALUES_PRIMITIVE && ! VALUE_CLASS_Boolean

The implementation mostly is copied from the method below (merge), with dead code removed because VALUES_PRIMITIVE is always true.

I'm using the METHOD_ARG_VALUE_BINARY_OPERATOR so that we prefer using the JDK ${Primitive}BinaryOperator classes, and fall back to the fastutil primitive-BinaryOperators where not present in the JDK.

I ran the junit tests locally and they pass.

Towards #336

@vigna
Copy link
Owner

vigna commented Nov 19, 2024

Looks good to me. Disambiguation will happen at the interface level, and it should be inlined.

@vigna
Copy link
Owner

vigna commented Nov 19, 2024

BTW, did you check that your code is actually executed (either by prints or by coverage tools)? The dispatch of that stuff is absolutely devilish...

@mhansen
Copy link
Contributor Author

mhansen commented Nov 19, 2024

Thank you! You're right, I should check this is actually executing. I think I'll write a test with an object that counts how many times it was hashed, asserting it was just hashed once after. I'll just need to be a little careful about rehashing.

@vigna
Copy link
Owner

vigna commented Nov 19, 2024

You can also just use a coverage tool. Or add a print in the code and make a one-off test, checking that something is printed.

@mhansen
Copy link
Contributor Author

mhansen commented Nov 19, 2024

I've confirmed it's executing. It's actually down from 3 hashes (get, containsKey, put) down to 1. I'm pretty happy to write a test for this; fastutil is a long-term project and it seems worth adding some test to stop this unintentionally regressing in future.

@mhansen
Copy link
Contributor Author

mhansen commented Nov 19, 2024

I think I can add a test in Object2IntOpenHashMapTest.

mhansen and others added 2 commits November 20, 2024 12:56
3 is too many times. In a future commit, we will reduce this.

The goal here is to demonstrate that the future commit works.
Previously, we were hashing the key 3x:
- get
- containsKey
- put

Now, we override mergeDouble with a hashamp-specific implementation that
hashes the key once.

Towards vigna#336
@mhansen
Copy link
Contributor Author

mhansen commented Nov 20, 2024

PTAL; I've added a test in commit 1, then reduced the allocations in commit 2.

@vigna vigna merged commit fcac58f into vigna:master Nov 26, 2024
@mhansen
Copy link
Contributor Author

mhansen commented Nov 26, 2024

Thank you for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants