rocksdb/util/user_comparator_wrapper.h
Andrew Kryczka dd29ad4223 Separate internal and user key comparators in BlockIter (#6944)
Summary:
Replace `BlockIter::comparator_` and `IndexBlockIter::user_comparator_wrapper_` with a concrete `UserComparatorWrapper` and `InternalKeyComparator`. The motivation for this change was the inconvenience of not knowing the concrete type of `BlockIter::comparator_`, which prevented calling specialized internal key comparison functions to optimize comparison of keys with global seqno applied.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/6944

Test Plan:
benchmark setup -- single file DBs, in-memory, no compression. "normal_db"
created by regular flush; "ingestion_db" created by ingesting a file. Both
DBs have same contents.

```
$ TEST_TMPDIR=/dev/shm/normal_db/ ./db_bench -benchmarks=fillrandom,compact -write_buffer_size=10485760000 -disable_auto_compactions=true -compression_type=none -num=1000000
$ ./ldb write_extern_sst ./tmp.sst --db=/dev/shm/ingestion_db/dbbench/ --compression_type=no --hex --create_if_missing < <(./sst_dump --command=scan --output_hex --file=/dev/shm/normal_db/dbbench/000007.sst | awk 'began {print "0x" substr($1, 2, length($1) - 2), "==>", "0x" $5} ; /^Sst file format: block-based/ {began=1}')
$ ./ldb ingest_extern_sst ./tmp.sst --db=/dev/shm/ingestion_db/dbbench/
```

benchmark run command:

```
$ TEST_TMPDIR=/dev/shm/$DB/ ./db_bench -benchmarks=seekrandom -seek_nexts=$SEEK_NEXT -use_existing_db=true -cache_index_and_filter_blocks=false -num=1000000 -cache_size=0 -threads=1 -reads=200000000 -mmap_read=1 -verify_checksum=false
```

results: perf improved marginally for ingestion_db and did not change significantly for normal_db:

SEEK_NEXT | DB | code | ops/sec | % change
-- | -- | -- | -- | --
0 | normal_db | master | 350880 |  
0 | normal_db | PR6944 | 351040 | 0.0
0 | ingestion_db | master | 343255 |  
0 | ingestion_db | PR6944 | 349424 | 1.8
10 | normal_db | master | 218711 |  
10 | normal_db | PR6944 | 217892 | -0.4
10 | ingestion_db | master | 220334 |  
10 | ingestion_db | PR6944 | 226437 | 2.8

Reviewed By: pdillinger

Differential Revision: D21924676

Pulled By: ajkr

fbshipit-source-id: ea4288a2eefa8112eb6c651a671c1de18c12e538
2020-07-07 17:26:16 -07:00

81 lines
2.9 KiB
C++

// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#pragma once
#include "monitoring/perf_context_imp.h"
#include "rocksdb/comparator.h"
namespace ROCKSDB_NAMESPACE {
// Wrapper of user comparator, with auto increment to
// perf_context.user_key_comparison_count.
class UserComparatorWrapper final : public Comparator {
public:
// `UserComparatorWrapper`s constructed with the default constructor are not
// usable and will segfault on any attempt to use them for comparisons.
UserComparatorWrapper() : user_comparator_(nullptr) {}
explicit UserComparatorWrapper(const Comparator* const user_cmp)
: Comparator(user_cmp->timestamp_size()), user_comparator_(user_cmp) {}
~UserComparatorWrapper() = default;
const Comparator* user_comparator() const { return user_comparator_; }
int Compare(const Slice& a, const Slice& b) const override {
PERF_COUNTER_ADD(user_key_comparison_count, 1);
return user_comparator_->Compare(a, b);
}
bool Equal(const Slice& a, const Slice& b) const override {
PERF_COUNTER_ADD(user_key_comparison_count, 1);
return user_comparator_->Equal(a, b);
}
const char* Name() const override { return user_comparator_->Name(); }
void FindShortestSeparator(std::string* start,
const Slice& limit) const override {
return user_comparator_->FindShortestSeparator(start, limit);
}
void FindShortSuccessor(std::string* key) const override {
return user_comparator_->FindShortSuccessor(key);
}
const Comparator* GetRootComparator() const override {
return user_comparator_->GetRootComparator();
}
bool IsSameLengthImmediateSuccessor(const Slice& s,
const Slice& t) const override {
return user_comparator_->IsSameLengthImmediateSuccessor(s, t);
}
bool CanKeysWithDifferentByteContentsBeEqual() const override {
return user_comparator_->CanKeysWithDifferentByteContentsBeEqual();
}
int CompareTimestamp(const Slice& ts1, const Slice& ts2) const override {
return user_comparator_->CompareTimestamp(ts1, ts2);
}
using Comparator::CompareWithoutTimestamp;
int CompareWithoutTimestamp(const Slice& a, bool a_has_ts, const Slice& b,
bool b_has_ts) const override {
PERF_COUNTER_ADD(user_key_comparison_count, 1);
return user_comparator_->CompareWithoutTimestamp(a, a_has_ts, b, b_has_ts);
}
private:
const Comparator* user_comparator_;
};
} // namespace ROCKSDB_NAMESPACE