Optimize about sstableKeyCompare (#11610)

Summary:
We observed `CompactionOutputs::UpdateGrandparentBoundaryInfo` consumes much time for `InternalKey::DecodeFrom` and `InternalKey::~InternalKey` in flame graph.

This PR omit the InternalKey object in `CompactionOutputs::UpdateGrandparentBoundaryInfo` .

![image](https://github.com/facebook/rocksdb/assets/1574991/661eaeec-2f46-46c6-a6a8-9738d6c191de)

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

Reviewed By: ajkr

Differential Revision: D47426971

Pulled By: cbi42

fbshipit-source-id: f0d3a8186d778294515c0685032f5b395c4d6a62
oxigraph-main
leipeng 1 year ago committed by Facebook GitHub Bot
parent c3c84b3397
commit bc0db33483
  1. 9
      db/compaction/compaction.cc
  2. 15
      db/compaction/compaction.h
  3. 11
      db/compaction/compaction_outputs.cc

@ -23,14 +23,13 @@ namespace ROCKSDB_NAMESPACE {
const uint64_t kRangeTombstoneSentinel = const uint64_t kRangeTombstoneSentinel =
PackSequenceAndType(kMaxSequenceNumber, kTypeRangeDeletion); PackSequenceAndType(kMaxSequenceNumber, kTypeRangeDeletion);
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a, int sstableKeyCompare(const Comparator* uc, const Slice& a, const Slice& b) {
const InternalKey& b) { auto c = uc->CompareWithoutTimestamp(ExtractUserKey(a), ExtractUserKey(b));
auto c = user_cmp->CompareWithoutTimestamp(a.user_key(), b.user_key());
if (c != 0) { if (c != 0) {
return c; return c;
} }
auto a_footer = ExtractInternalKeyFooter(a.Encode()); auto a_footer = ExtractInternalKeyFooter(a);
auto b_footer = ExtractInternalKeyFooter(b.Encode()); auto b_footer = ExtractInternalKeyFooter(b);
if (a_footer == kRangeTombstoneSentinel) { if (a_footer == kRangeTombstoneSentinel) {
if (b_footer != kRangeTombstoneSentinel) { if (b_footer != kRangeTombstoneSentinel) {
return -1; return -1;

@ -31,8 +31,19 @@ namespace ROCKSDB_NAMESPACE {
// that key never appears in the database. We don't want adjacent sstables to // that key never appears in the database. We don't want adjacent sstables to
// be considered overlapping if they are separated by the range tombstone // be considered overlapping if they are separated by the range tombstone
// sentinel. // sentinel.
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a, int sstableKeyCompare(const Comparator* user_cmp, const Slice&, const Slice&);
const InternalKey& b); inline int sstableKeyCompare(const Comparator* user_cmp, const Slice& a,
const InternalKey& b) {
return sstableKeyCompare(user_cmp, a, b.Encode());
}
inline int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
const Slice& b) {
return sstableKeyCompare(user_cmp, a.Encode(), b);
}
inline int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
const InternalKey& b) {
return sstableKeyCompare(user_cmp, a.Encode(), b.Encode());
}
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey* a, int sstableKeyCompare(const Comparator* user_cmp, const InternalKey* a,
const InternalKey& b); const InternalKey& b);
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a, int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,

@ -127,11 +127,6 @@ size_t CompactionOutputs::UpdateGrandparentBoundaryInfo(
if (grandparents.empty()) { if (grandparents.empty()) {
return curr_key_boundary_switched_num; return curr_key_boundary_switched_num;
} }
assert(!internal_key.empty());
InternalKey ikey;
ikey.DecodeFrom(internal_key);
assert(ikey.Valid());
const Comparator* ucmp = compaction_->column_family_data()->user_comparator(); const Comparator* ucmp = compaction_->column_family_data()->user_comparator();
// Move the grandparent_index_ to the file containing the current user_key. // Move the grandparent_index_ to the file containing the current user_key.
@ -139,7 +134,7 @@ size_t CompactionOutputs::UpdateGrandparentBoundaryInfo(
// index points to the last file containing the key. // index points to the last file containing the key.
while (grandparent_index_ < grandparents.size()) { while (grandparent_index_ < grandparents.size()) {
if (being_grandparent_gap_) { if (being_grandparent_gap_) {
if (sstableKeyCompare(ucmp, ikey, if (sstableKeyCompare(ucmp, internal_key,
grandparents[grandparent_index_]->smallest) < 0) { grandparents[grandparent_index_]->smallest) < 0) {
break; break;
} }
@ -152,13 +147,13 @@ size_t CompactionOutputs::UpdateGrandparentBoundaryInfo(
being_grandparent_gap_ = false; being_grandparent_gap_ = false;
} else { } else {
int cmp_result = sstableKeyCompare( int cmp_result = sstableKeyCompare(
ucmp, ikey, grandparents[grandparent_index_]->largest); ucmp, internal_key, grandparents[grandparent_index_]->largest);
// If it's same key, make sure grandparent_index_ is pointing to the last // If it's same key, make sure grandparent_index_ is pointing to the last
// one. // one.
if (cmp_result < 0 || if (cmp_result < 0 ||
(cmp_result == 0 && (cmp_result == 0 &&
(grandparent_index_ == grandparents.size() - 1 || (grandparent_index_ == grandparents.size() - 1 ||
sstableKeyCompare(ucmp, ikey, sstableKeyCompare(ucmp, internal_key,
grandparents[grandparent_index_ + 1]->smallest) < grandparents[grandparent_index_ + 1]->smallest) <
0))) { 0))) {
break; break;

Loading…
Cancel
Save