efd013d6d8
Summary: I was investigating performance issues in the SstFileWriter and found all of the following: - The SstFileWriter::Add() function created a local InternalKey every time it was called generating a allocation and free each time. Changed to have an InternalKey member variable that can be reset with the new InternalKey::Set() function. - In SstFileWriter::Add() the smallest_key and largest_key values were assigned the result of a ToString() call, but it is simpler to just assign them directly from the user's key. - The Slice class had no move constructor so each time one was returned from a function a new one had to be allocated, the old data copied to the new, and the old one was freed. I added the move constructor which also required a copy constructor and assignment operator. - The BlockBuilder::CurrentSizeEstimate() function calculates the current estimate size, but was being called 2 or 3 times for each key added. I changed the class to maintain a running estimate (equal to the original calculation) so that the function can return an already calculated value. - The code in BlockBuilder::Add() that calculated the shared bytes between the last key and the new key duplicated what Slice::difference_offset does, so I replaced it with the standard function. - BlockBuilder::Add() had code to copy just the changed portion into the last key value (and asserted that it now matched the new key). It is more efficient just to copy the whole new key over. - Moved this same code up into the 'if (use_delta_encoding_)' since the last key value is only needed when delta encoding is on. - FlushBlockBySizePolicy::BlockAlmostFull calculated a standard deviation value each time it was called, but this information would only change if block_size of block_size_deviation changed, so I created a member variable to hold the value to avoid the calculation each time. - Each PutVarint??() function has a buffer and calls std::string::append(). Two or three calls in a row could share a buffer and a single call to std::string::append(). Some of these will be helpful outside of the SstFileWriter. I'm not 100% the addition of the move constructor is appropriate as I wonder why this wasn't done before - maybe because of compiler compatibility? I tried it on gcc 4.8 and 4.9. Test Plan: The changes should not affect the results so the existing tests should all still work and no new tests were added. The value of the changes was seen by manually testing the SstFileWriter class through MyRocks and adding timing code to identify problem areas. Reviewers: sdong, IslamAbdelRahman Reviewed By: IslamAbdelRahman Subscribers: andrewkr, dhruba Differential Revision: https://reviews.facebook.net/D59607
63 lines
2.2 KiB
C++
63 lines
2.2 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under the BSD-style license found in the
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
// of patent rights can be found in the PATENTS file in the same 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 <vector>
|
|
|
|
#include <stdint.h>
|
|
#include "rocksdb/slice.h"
|
|
|
|
namespace rocksdb {
|
|
|
|
class BlockBuilder {
|
|
public:
|
|
BlockBuilder(const BlockBuilder&) = delete;
|
|
void operator=(const BlockBuilder&) = delete;
|
|
|
|
explicit BlockBuilder(int block_restart_interval,
|
|
bool use_delta_encoding = true);
|
|
|
|
// Reset the contents as if the BlockBuilder was just constructed.
|
|
void Reset();
|
|
|
|
// REQUIRES: Finish() has not been callled since the last call to Reset().
|
|
// REQUIRES: key is larger than any previously added key
|
|
void Add(const Slice& key, const Slice& value);
|
|
|
|
// Finish building the block and return a slice that refers to the
|
|
// block contents. The returned slice will remain valid for the
|
|
// lifetime of this builder or until Reset() is called.
|
|
Slice Finish();
|
|
|
|
// Returns an estimate of the current (uncompressed) size of the block
|
|
// we are building.
|
|
inline size_t CurrentSizeEstimate() const { return estimate_; }
|
|
|
|
// Returns an estimated block size after appending key and value.
|
|
size_t EstimateSizeAfterKV(const Slice& key, const Slice& value) const;
|
|
|
|
// Return true iff no entries have been added since the last Reset()
|
|
bool empty() const {
|
|
return buffer_.empty();
|
|
}
|
|
|
|
private:
|
|
const int block_restart_interval_;
|
|
const bool use_delta_encoding_;
|
|
|
|
std::string buffer_; // Destination buffer
|
|
std::vector<uint32_t> restarts_; // Restart points
|
|
size_t estimate_;
|
|
int counter_; // Number of entries emitted since restart
|
|
bool finished_; // Has Finish() been called?
|
|
std::string last_key_;
|
|
};
|
|
|
|
} // namespace rocksdb
|