Skip to content

Conversation

conradludgate
Copy link
Contributor

Motivation

While rebasing #7075, I noticed the change from #7219 which updates hashbrown, requiring the addition of the raw-entry feature. That inspired me to take a look at the actual JoinMap impl and I thought it could be improved by using HashTable instead.

Solution

Replacing the tasks_by_key map with a HashTable, we unlock a few benefits:

  1. When searching by ID, we don't need to store the id in tasks_by_key, since we can fetch the ID from the AbortHandle.
  2. We don't need to duplicate the S: BuildHasher, as we are in-control over the hash-function, we can fetch the existing hasher from hashes_by_task.

However, removing the ID from tasks_by_key will slow down those lookups (only in join_next()), as they now need a vtable lookup, rather than comparing against the id already in the current cache line.


I don't feel strongly about this PR, I'm just quite fond of the HashTable API and thought I'd try it here.

@Darksonn Darksonn added the A-tokio-util Area: The tokio-util crate label Apr 11, 2025
Comment on lines +701 to +712
let hash_builder = self.hashes_by_task.hasher();
self.tasks_by_key
.shrink_to_fit(|(k, _)| hash_one(hash_builder, k));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused. Why is hashes_by_task needed inside tasks_by_key here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only need the hasher inside hashes_by_task. It saves us from having to store a separate instance of the hasher.

An alternative solution could be to have a separate hasher: S, and then make the HashMap<Id, u64> use a cheaper hash function like a simple fibonacci hash (so no state is needed). That might be overkill though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reference: conradludgate@5086eeb

@conradludgate conradludgate force-pushed the conrad/remove-raw-entry-feature branch from 170b7a9 to 6d238e2 Compare July 21, 2025 12:01
@conradludgate conradludgate force-pushed the conrad/remove-raw-entry-feature branch from 6d238e2 to bc16256 Compare July 21, 2025 12:17
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Looks good, thanks.

@Darksonn Darksonn merged commit 9e94fa7 into tokio-rs:master Jul 22, 2025
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants