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

Doc comments: use std::unordered_map #11113

Merged
merged 4 commits into from
Jul 17, 2024
Merged

Conversation

roberth
Copy link
Member

@roberth roberth commented Jul 16, 2024

Motivation

As suggested in #11072 and now possible after #11092

Context

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@roberth roberth requested a review from edolstra as a code owner July 16, 2024 13:21
@roberth roberth enabled auto-merge July 16, 2024 13:21
@roberth roberth disabled auto-merge July 16, 2024 14:47
@@ -2,12 +2,15 @@

#include <cinttypes>

#include "util.hh"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this included?
Can't seem to tell.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for asking. It was for hash_combine, but that wasn't obvious at all.
I've split that into its own header, partly because "util" is a bad scope for a header, but also for the less subjective argument, from the commit message:

Splitting it out immediately answers questions like [this], without increasing the number of compilation units.

I did consider using boost::hash_combine instead, but it doesn't seem to be quite as capable, accepting only two arguments.

roberth added a commit that referenced this pull request Jul 16, 2024
Splitting it out immediately answers questions like [this],
without increasing the number of compilation units.

I did consider using boost::hash_combine instead, but it doesn't seem
to be quite as capable, accepting only two arguments.

[this]: #11113 (comment)
Splitting it out immediately answers questions like [this],
without increasing the number of compilation units.

I did consider using boost::hash_combine instead, but it doesn't seem
to be quite as capable, accepting only two arguments.

[this]: #11113 (comment)
@roberth roberth force-pushed the doc-comment-unordered-map branch from fcdcffd to d0e9878 Compare July 16, 2024 20:31
size_t hash() const noexcept
{
size_t h = 854125;
hash_combine(h, id);
Copy link
Member

Choose a reason for hiding this comment

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

This hash_combine() seems unnecessary? We can just return the hash of id.

Copy link
Member Author

Choose a reason for hiding this comment

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

In Java it's common practice to include a random number to represent the distinct type, but I think you're right.
That doesn't really apply here, because we don't have a "top" type like Java's Object that would create a need to avoid collisions when used in e.g. HashMap<String, Object>.
The closest thing we might have is std::variant. I think we can assume that it hashes its index number, which is probably better than salting each type anyway, because iirc it allows multiple distinct occurrences of the same type.

In C++ we don't need to salt the hash.
@edolstra edolstra merged commit 621c23b into master Jul 17, 2024
21 checks passed
@edolstra edolstra deleted the doc-comment-unordered-map branch July 17, 2024 14:50
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.

3 participants