reuse scratch buffer in transaction_log_reader (#5702)

Summary:
in order to avoid reallocations for a scratch std::string on every call to Next().
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5702

Differential Revision: D16867803

fbshipit-source-id: 1391220a1b172b23336bbc71dc0c79ccf3b1c701
main
jsteemann 5 years ago committed by Facebook Github Bot
parent 2f41ecfe75
commit 62829ff751
  1. 12
      db/transaction_log_impl.cc
  2. 3
      db/transaction_log_impl.h

@ -78,19 +78,16 @@ Status TransactionLogIteratorImpl::status() { return current_status_; }
bool TransactionLogIteratorImpl::Valid() { return started_ && is_valid_; } bool TransactionLogIteratorImpl::Valid() { return started_ && is_valid_; }
bool TransactionLogIteratorImpl::RestrictedRead( bool TransactionLogIteratorImpl::RestrictedRead(Slice* record) {
Slice* record,
std::string* scratch) {
// Don't read if no more complete entries to read from logs // Don't read if no more complete entries to read from logs
if (current_last_seq_ >= versions_->LastSequence()) { if (current_last_seq_ >= versions_->LastSequence()) {
return false; return false;
} }
return current_log_reader_->ReadRecord(record, scratch); return current_log_reader_->ReadRecord(record, &scratch_);
} }
void TransactionLogIteratorImpl::SeekToStartSequence(uint64_t start_file_index, void TransactionLogIteratorImpl::SeekToStartSequence(uint64_t start_file_index,
bool strict) { bool strict) {
std::string scratch;
Slice record; Slice record;
started_ = false; started_ = false;
is_valid_ = false; is_valid_ = false;
@ -104,7 +101,7 @@ void TransactionLogIteratorImpl::SeekToStartSequence(uint64_t start_file_index,
reporter_.Info(current_status_.ToString().c_str()); reporter_.Info(current_status_.ToString().c_str());
return; return;
} }
while (RestrictedRead(&record, &scratch)) { while (RestrictedRead(&record)) {
if (record.size() < WriteBatchInternal::kHeader) { if (record.size() < WriteBatchInternal::kHeader) {
reporter_.Corruption( reporter_.Corruption(
record.size(), Status::Corruption("very small log record")); record.size(), Status::Corruption("very small log record"));
@ -155,7 +152,6 @@ void TransactionLogIteratorImpl::Next() {
} }
void TransactionLogIteratorImpl::NextImpl(bool internal) { void TransactionLogIteratorImpl::NextImpl(bool internal) {
std::string scratch;
Slice record; Slice record;
is_valid_ = false; is_valid_ = false;
if (!internal && !started_) { if (!internal && !started_) {
@ -167,7 +163,7 @@ void TransactionLogIteratorImpl::NextImpl(bool internal) {
if (current_log_reader_->IsEOF()) { if (current_log_reader_->IsEOF()) {
current_log_reader_->UnmarkEOF(); current_log_reader_->UnmarkEOF();
} }
while (RestrictedRead(&record, &scratch)) { while (RestrictedRead(&record)) {
if (record.size() < WriteBatchInternal::kHeader) { if (record.size() < WriteBatchInternal::kHeader) {
reporter_.Corruption( reporter_.Corruption(
record.size(), Status::Corruption("very small log record")); record.size(), Status::Corruption("very small log record"));

@ -86,6 +86,7 @@ class TransactionLogIteratorImpl : public TransactionLogIterator {
size_t current_file_index_; size_t current_file_index_;
std::unique_ptr<WriteBatch> current_batch_; std::unique_ptr<WriteBatch> current_batch_;
std::unique_ptr<log::Reader> current_log_reader_; std::unique_ptr<log::Reader> current_log_reader_;
std::string scratch_;
Status OpenLogFile(const LogFile* log_file, Status OpenLogFile(const LogFile* log_file,
std::unique_ptr<SequentialFileReader>* file); std::unique_ptr<SequentialFileReader>* file);
@ -107,7 +108,7 @@ class TransactionLogIteratorImpl : public TransactionLogIterator {
VersionSet const* const versions_; VersionSet const* const versions_;
const bool seq_per_batch_; const bool seq_per_batch_;
// Reads from transaction log only if the writebatch record has been written // Reads from transaction log only if the writebatch record has been written
bool RestrictedRead(Slice* record, std::string* scratch); bool RestrictedRead(Slice* record);
// Seeks to startingSequenceNumber reading from startFileIndex in files_. // Seeks to startingSequenceNumber reading from startFileIndex in files_.
// If strict is set,then must get a batch starting with startingSequenceNumber // If strict is set,then must get a batch starting with startingSequenceNumber
void SeekToStartSequence(uint64_t start_file_index = 0, bool strict = false); void SeekToStartSequence(uint64_t start_file_index = 0, bool strict = false);

Loading…
Cancel
Save