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 comparison count for format_version=3 indexes #6650

Closed
wants to merge 1 commit into from

Conversation

ajkr
Copy link
Contributor

@ajkr ajkr commented Apr 6, 2020

Summary: In index blocks since format_version=3, user keys are written
rather than internal keys. When reading such blocks, the comparator is
obtained via InternalKeyComparator::user_comparator(). We need to
wrap that function's result in a UserComparatorWrapper in order for
its comparisons to be counted in PerfContext::user_key_comparison_count.

Test Plan: ran db_bench and verified
PerfContext::user_key_comparison_count became larger.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ajkr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ajkr ajkr force-pushed the fix-comparison-accounting branch from 9d47cf6 to 065fd7c Compare April 6, 2020 06:53
@facebook-github-bot
Copy link
Contributor

@ajkr has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ajkr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@siying siying left a comment

Choose a reason for hiding this comment

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

Hopefully the extra virtual function call doesn't regress the performance much.

db/dbformat.h Outdated
@@ -214,7 +214,7 @@ class InternalKeyComparator
virtual void FindShortSuccessor(std::string* key) const override;

const Comparator* user_comparator() const {
return user_comparator_.user_comparator();
return &user_comparator_;
Copy link
Contributor

Choose a reason for hiding this comment

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

There seems to be a lot of caller to this functions. Are we fixing accounts for all of them, or is there a chance we overcount somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think about it again. Can we create a new function that returns the wrapper instead? In that case, the user of the function will be explicit about whether the comparisons are counted underlying or not. And it can also potentially avoid one virtual function call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mind helping me understand what kind of comparison should not be counted when PerfContext is enabled? I am not that familiar with this wrapper so am still naively hoping we can land in one of the following simple situations:

  1. the wrapper is cheap and we allow it to count all comparisons
  2. the wrapper is expensive so we get rid of it entirely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Never mind, I saw there's an existing pattern to follow (call UserComparatorWrapper(rep_->internal_comparator.user_comparator()).Compare()). Will follow that pattern.

Summary: In index blocks since `format_version=3`, user keys are written
rather than internal keys. When reading such blocks, the comparator is
obtained via `InternalKeyComparator::user_comparator()`. That function
must not return an unwrapped result as the wrapper class provides
accounting logic to populate `PerfContext::user_key_comparison_count`.

Test Plan: ran db_bench and verified
`PerfContext::user_key_comparison_count` became larger.
@ajkr ajkr force-pushed the fix-comparison-accounting branch from 065fd7c to 6e6fa71 Compare April 11, 2020 01:03
@facebook-github-bot
Copy link
Contributor

@ajkr has updated the pull request. Re-import the pull request

@ajkr
Copy link
Contributor Author

ajkr commented Apr 11, 2020

@siying I've addressed your comment; this is ready for another look.

By the way, I am thinking we should get rid of the UserComparatorWrapper entirely. It is too confusing to have a subclass of Comparator while not wanting to make virtual function calls on it. Not to mention it's hard to know when to wrap a comparator, or whether a comparator has already been wrapped.

An alternative is just use macros. There would be a COMPARE() macro that handles bumping the counter followed by invoking the comparison. What do you think? (This would be a future PR.)

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ajkr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@ajkr merged this pull request in 9eca6d6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants