From 719af44045dff21f3577ae7063b07edf7c4ee0ef Mon Sep 17 00:00:00 2001 From: leipeng Date: Tue, 30 Aug 2022 13:24:49 +0800 Subject: [PATCH] DBImpl::GetApproximateSizes(): optimize by reusing InternalKey --- db/db_impl/db_impl.cc | 5 +++-- db/dbformat.h | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index 4c7c2f172..494d0e264 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -4140,6 +4140,7 @@ Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options, SuperVersion* sv = GetAndRefSuperVersion(cfd); v = sv->current; + InternalKey k1, k2; for (int i = 0; i < n; i++) { Slice start = range[i].start; Slice limit = range[i].limit; @@ -4158,8 +4159,8 @@ Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options, } #endif // Convert user_key into a corresponding internal key. - InternalKey k1(start, kMaxSequenceNumber, kValueTypeForSeek); - InternalKey k2(limit, kMaxSequenceNumber, kValueTypeForSeek); + SetInternalKey(k1.rep(), start, kMaxSequenceNumber, kValueTypeForSeek); + SetInternalKey(k2.rep(), limit, kMaxSequenceNumber, kValueTypeForSeek); sizes[i] = 0; if (options.include_files) { sizes[i] += versions_->ApproximateSize( diff --git a/db/dbformat.h b/db/dbformat.h index ce251d0c3..4b33e9ec4 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -160,6 +160,16 @@ inline void UnPackSequenceAndType(uint64_t packed, uint64_t* seq, EntryType GetEntryType(ValueType value_type); +inline void SetInternalKey(std::string* result, Slice ukey, uint64_t seqvt) { + result->assign(ukey.data(), ukey.size()); + PutFixed64(result, seqvt); +} +inline void SetInternalKey(std::string* result, Slice ukey, + SequenceNumber seq, ValueType vt) { + result->assign(ukey.data(), ukey.size()); + PutFixed64(result, PackSequenceAndType(seq, vt)); +} + // Append the serialization of "key" to *result. extern void AppendInternalKey(std::string* result, const ParsedInternalKey& key);