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

fix(HashMap): change capacity() to *const Self #19770

Closed
wants to merge 2 commits into from

Conversation

0xNineteen
Copy link

HashMap.capacity requires a mutable reference whereas it should be const

Copy link
Contributor

@SeanTheGleaming SeanTheGleaming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does raise the question of why we are passing by const pointer so much in hash_map.zig. In HashMapUnmanaged, .capacity() requires a const pointer because .header() requires a const pointer, but looking at .header(), there is nothing that we need a pointer to the hash map for. Replacing these parameters with just the value, all the tests pass. Maybe I'm missing something, but I can't see why we aren't doing this. Does anyone have a good reason for passing by pointer?

@Trevor-Strong
Copy link

The only reason for using pointers instead of just the value is to force the hashmap to be passed by reference instead of relying on the compiler's implicit pass-by-reference. That being said, the compiler should only turn value parameters into reference parameters when it's faster. By explicitly having the parameter be a const pointer, we lose out on this potential optimization.

If this was done originally because the compiler was choosing to pass the hashmap by value but it was actually faster to pass by reference then that sounds like a compiler bug and we should fix that instead of using a workaround. The standard library is the last place I would expect to see compiler bug workarounds.

@mlugg
Copy link
Member

mlugg commented Apr 26, 2024

The above analysis is accurate -- such workarounds are effectively banned in std. There have been (and continue to be) bugs around PRO (Parameter Reference Optimization), but working around them in std is considered bad form.

OP: your change here is completely fine, and I'll be happy to merge it as-is, but would you be willing to go the extra mile and look over the existing uses of *const T in this file and change them to T where appropriate?

@0xNineteen
Copy link
Author

yeah sure i can checkout the additional changes

@andrewrk
Copy link
Member

andrewrk commented May 9, 2024

Please don't open a PR without testing the code first. It doesn't even compile.

@andrewrk andrewrk closed this May 9, 2024
@0xNineteen
Copy link
Author

the original pr compiled fine, which is why i opened it - these changes weren't complete yet because i was having troubles compiling from source/testing

SeanTheGleaming added a commit to SeanTheGleaming/zig that referenced this pull request May 16, 2024
 - Used `Self` instead of `*const Self` where appropriate (orignally proposed in ziglang#19770)
 - Replaced `@intFromPtr` and `@ptrFromInt` with `@ptrCast`, `@alignCast`, and pointer arithmetic where appropriate

With this, the only remaining instance on pointer-int conversion in hash_map.zig is in `HashMapUnmanaged.removeByPtr`, which easily be able to be eliminated once pointer subtraction is supported.
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.

None yet

6 participants