Reduce extra key comparision in DBIter::Next()

Summary: Now DBIter::Next() always compares with current key with itself first, which is unnecessary if the last key is not a merge key. I made the change and didn't see db_iter_test fails. Want to hear whether people have any idea what I miss.

Test Plan: Run all unit tests

Subscribers: leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D48279
This commit is contained in:
sdong 2015-10-06 17:48:27 -07:00
parent 9a9d4759b2
commit 33e0c93826
2 changed files with 11 additions and 2 deletions

View File

@ -186,9 +186,17 @@ void DBIter::Next() {
if (!iter_->Valid()) { if (!iter_->Valid()) {
iter_->SeekToFirst(); iter_->SeekToFirst();
} }
} else if (iter_->Valid() && !current_entry_is_merged_) {
// If the current value is not a merge, the iter position is the
// current key, which is already returned. We can safely issue a
// Next() without checking the current key.
// If the current key is a merge, very likely iter already points
// to the next internal position.
iter_->Next();
} }
// If the current value is merged, we might already hit end of iter_ // Now we point to the next internal position, for both of merge and
// not merge cases.
if (!iter_->Valid()) { if (!iter_->Valid()) {
valid_ = false; valid_ = false;
return; return;

View File

@ -1910,9 +1910,10 @@ TEST_F(DBTest, IterReseek) {
DestroyAndReopen(options); DestroyAndReopen(options);
CreateAndReopenWithCF({"pikachu"}, options); CreateAndReopenWithCF({"pikachu"}, options);
// insert two keys with same userkey and verify that // insert three keys with same userkey and verify that
// reseek is not invoked. For each of these test cases, // reseek is not invoked. For each of these test cases,
// verify that we can find the next key "b". // verify that we can find the next key "b".
ASSERT_OK(Put(1, "a", "zero"));
ASSERT_OK(Put(1, "a", "one")); ASSERT_OK(Put(1, "a", "one"));
ASSERT_OK(Put(1, "a", "two")); ASSERT_OK(Put(1, "a", "two"));
ASSERT_OK(Put(1, "b", "bone")); ASSERT_OK(Put(1, "b", "bone"));