Skip to content

Commit 9ea8bf9

Browse files
committed
DB::Put() to estimate write batch data size needed and pre-allocate buffer
Summary: In one of CPU profiles, we see some CPU costs of string::reserve() inside Batch.Put(). This patch should be able to reduce some of the costs by allocating sufficient buffer before hand. Since it is a trivial percentage of CPU costs, I didn't find a way to show the improvement in one of the benchmarks. I'll deploy it to same application and do the same CPU profiling to make sure those CPU costs are reduced. Test Plan: make all check Reviewers: haobo, kailiu, igor Reviewed By: haobo CC: leveldb, nkg- Differential Revision: https://reviews.facebook.net/D15135
1 parent 8454cfe commit 9ea8bf9

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

db/db_impl.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -3789,7 +3789,10 @@ Status DBImpl::GetDbIdentity(std::string& identity) {
37893789
// Default implementations of convenience methods that subclasses of DB
37903790
// can call if they wish
37913791
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
3792-
WriteBatch batch;
3792+
// Pre-allocate size of write batch conservatively.
3793+
// 8 bytes are taken by header, 4 bytes for count, 1 byte for type,
3794+
// and we allocate 11 extra bytes for key length, as well as value length.
3795+
WriteBatch batch(key.size() + value.size() + 24);
37933796
batch.Put(key, value);
37943797
return Write(opt, &batch);
37953798
}

db/write_batch.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ namespace rocksdb {
3535
// WriteBatch header has an 8-byte sequence number followed by a 4-byte count.
3636
static const size_t kHeader = 12;
3737

38-
WriteBatch::WriteBatch() {
38+
WriteBatch::WriteBatch(size_t reserved_bytes) {
39+
rep_.reserve((reserved_bytes > kHeader) ? reserved_bytes : kHeader);
3940
Clear();
4041
}
4142

include/rocksdb/write_batch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct SliceParts;
3535

3636
class WriteBatch {
3737
public:
38-
WriteBatch();
38+
explicit WriteBatch(size_t reserved_bytes = 0);
3939
~WriteBatch();
4040

4141
// Store the mapping "key->value" in the database.

0 commit comments

Comments
 (0)