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

Make RawTable::insert_no_grow unsafe #254

Merged
merged 1 commit into from
Mar 30, 2021

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Mar 29, 2021

For performance reasons, this method assumes that there is sufficient
capacity for the new element, and it misbehaves otherwise, breaking
invariants or even segfaulting. The necessary conditions could be
checked, but if we're to keep it lean, it should be unsafe, so the
burden is on the caller to ensure capacity.

Fixes #253.

For performance reasons, this method _assumes_ that there is sufficient
capacity for the new element, and it misbehaves otherwise, breaking
invariants or even segfaulting. The necessary conditions could be
checked, but if we're to keep it lean, it should be `unsafe`, so the
burden is on the caller to ensure capacity.

Fixes rust-lang#253.
self.table.growth_left -= special_is_empty(old_ctrl) as usize;
// If we are replacing a DELETED entry then we don't need to update
// the load counter.
self.table.growth_left -= special_is_empty(old_ctrl) as usize;
Copy link
Member

Choose a reason for hiding this comment

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

Since this function is already unsafe, we could add an extra assume_no_deleted argument which assumes that the table is fresh and does not contain any DELETED entries? This would skip the check on old_ctrl and might improve performance in some cases.

However it might be exposing too much of the hash table internals. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

So assume_no_deleted would still be a condition there, but one that you would hope is constant-propagated?

I think it's too much of a micro-optimization, especially for exposing internals. I ran some indexmap benchmarks that are heavy on rebuild_hash_table, which is a clear followed by a loop of insert_no_grow. About 50% of that function's perf profile is simply the bucket.write, mostly due to cache misses. Most of the remaining time is spread around finding the insert slot, and less than 4% on updating growth_left. I even tried hard-coding that to growth_left -= 1, but the difference was lost in the noise.

@Amanieu
Copy link
Member

Amanieu commented Mar 30, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 30, 2021

📌 Commit 26f725b has been approved by Amanieu

@bors
Copy link
Collaborator

bors commented Mar 30, 2021

⌛ Testing commit 26f725b with merge ba4ff68...

@bors
Copy link
Collaborator

bors commented Mar 30, 2021

☀️ Test successful - checks-travis
Approved by: Amanieu
Pushing ba4ff68 to master...

@bors bors merged commit ba4ff68 into rust-lang:master Mar 30, 2021
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.

RawTable::insert_no_grow needs safety checks, or should be unsafe
3 participants