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

chore: remove redundant Find calls when inserting into DenseSet #1917

Merged
merged 1 commit into from Sep 22, 2023

Conversation

romange
Copy link
Collaborator

@romange romange commented Sep 22, 2023

A regular DenseSet insertion first checks for uniqueness and then inserts a new element. Sometimes we know that the new element is new and we can insert it without checking for uniqueness first.

Also, pass hashcode into internal functions so we could save some hash calls.

A regular DenseSet insertion first checks for uniqueness and then inserts a new element.
Sometimes we know that the new element is new and we can insert it without checking for
uniqueness first.

Also, pass hashcode into internal functions so we could save some hash calls.

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
Comment on lines -438 to +442
auto [newk, added] = score_map->AddOrUpdate(string_view{ele, sdslen(ele)}, score);
DCHECK(added);
obj = score_map->AddUnique(string_view{ele, sdslen(ele)}, score);

*out_flags = ZADD_OUT_ADDED;
*newscore = score;
obj = newk;
added = score_tree->Insert(obj);
bool added = score_tree->Insert(obj);
Copy link
Contributor

Choose a reason for hiding this comment

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

So that's the only external use so far? I also assume we can use this when we created the key for regular hashes (small nit)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you can see that also reimplemented StringMap::AddOrSkip that also benefits from this function.
I agree that AddOrUpdate functions should disappear from the interface because they will always be inefficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm, actually it's not easy to remove AddOrUpdate because of technical reasons of how entries are created in DenseSet. Will keep thinking about it.

@romange romange merged commit 4fccda6 into main Sep 22, 2023
10 checks passed
@romange romange deleted the AddUniq branch September 22, 2023 19:48
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

2 participants