Skip to content

Commit bb19b53

Browse files
author
Kai Liu
committed
Aggressively inlining the short functions in coding.cc
Summary: This diff takes an even more aggressive way to inline the functions. A decent rule that I followed is "not inline a function if it is more than 10 lines long." Normally optimizing code by inline is ugly and hard to control, but since one of our usecase has significant amount of CPU used in functions from coding.cc, I'd like to try this diff out. Test Plan: 1. the size for some .o file increased a little bit, but most less than 1%. So I think the negative impact of inline is negligible. 2. As the regression test shows (ran for 10 times and I calculated the average number) Metrics Befor After ======================================================================== rocksdb.build.fillseq.qps 426595 444515 (+4.6%) rocksdb.build.memtablefillrandom.qps 121739 123110 rocksdb.build.memtablereadrandom.qps 1285103 1280520 rocksdb.build.overwrite.qps 125816 135570 (+9%) rocksdb.build.readrandom_fillunique_random.qps 285995 296863 rocksdb.build.readrandom_memtable_sst.qps 1027132 1027279 rocksdb.build.readrandom.qps 1041427 1054665 rocksdb.build.readrandom_smallblockcache.qps 1028631 1038433 rocksdb.build.readwhilewriting.qps 918352 914629 Reviewers: haobo, sdong, igor CC: leveldb Differential Revision: https://reviews.facebook.net/D15291
1 parent 7dea558 commit bb19b53

File tree

3 files changed

+178
-179
lines changed

3 files changed

+178
-179
lines changed

include/rocksdb/options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct Options {
136136
// errors. This may have unforeseen ramifications: for example, a
137137
// corruption of one DB entry may cause a large number of entries to
138138
// become unreadable or for the entire DB to become unopenable.
139-
// If any of the writes to the database fails (Put, Delete, Merge, Write),
139+
// If any of the writes to the database fails (Put, Delete, Merge, Write),
140140
// the database will switch to read-only mode and fail all other
141141
// Write operations.
142142
// Default: false

util/coding.cc

+15-178
Original file line numberDiff line numberDiff line change
@@ -8,128 +8,39 @@
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
99

1010
#include "util/coding.h"
11-
1211
#include <algorithm>
1312

1413
namespace rocksdb {
1514

16-
void EncodeFixed32(char* buf, uint32_t value) {
17-
#if __BYTE_ORDER == __LITTLE_ENDIAN
18-
memcpy(buf, &value, sizeof(value));
19-
#else
20-
buf[0] = value & 0xff;
21-
buf[1] = (value >> 8) & 0xff;
22-
buf[2] = (value >> 16) & 0xff;
23-
buf[3] = (value >> 24) & 0xff;
24-
#endif
25-
}
26-
27-
void EncodeFixed64(char* buf, uint64_t value) {
28-
#if __BYTE_ORDER == __LITTLE_ENDIAN
29-
memcpy(buf, &value, sizeof(value));
30-
#else
31-
buf[0] = value & 0xff;
32-
buf[1] = (value >> 8) & 0xff;
33-
buf[2] = (value >> 16) & 0xff;
34-
buf[3] = (value >> 24) & 0xff;
35-
buf[4] = (value >> 32) & 0xff;
36-
buf[5] = (value >> 40) & 0xff;
37-
buf[6] = (value >> 48) & 0xff;
38-
buf[7] = (value >> 56) & 0xff;
39-
#endif
40-
}
41-
42-
void PutFixed32(std::string* dst, uint32_t value) {
43-
char buf[sizeof(value)];
44-
EncodeFixed32(buf, value);
45-
dst->append(buf, sizeof(buf));
46-
}
47-
48-
void PutFixed64(std::string* dst, uint64_t value) {
49-
char buf[sizeof(value)];
50-
EncodeFixed64(buf, value);
51-
dst->append(buf, sizeof(buf));
52-
}
53-
5415
char* EncodeVarint32(char* dst, uint32_t v) {
5516
// Operate on characters as unsigneds
5617
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
5718
static const int B = 128;
58-
if (v < (1<<7)) {
19+
if (v < (1 << 7)) {
5920
*(ptr++) = v;
60-
} else if (v < (1<<14)) {
21+
} else if (v < (1 << 14)) {
6122
*(ptr++) = v | B;
62-
*(ptr++) = v>>7;
63-
} else if (v < (1<<21)) {
23+
*(ptr++) = v >> 7;
24+
} else if (v < (1 << 21)) {
6425
*(ptr++) = v | B;
65-
*(ptr++) = (v>>7) | B;
66-
*(ptr++) = v>>14;
67-
} else if (v < (1<<28)) {
26+
*(ptr++) = (v >> 7) | B;
27+
*(ptr++) = v >> 14;
28+
} else if (v < (1 << 28)) {
6829
*(ptr++) = v | B;
69-
*(ptr++) = (v>>7) | B;
70-
*(ptr++) = (v>>14) | B;
71-
*(ptr++) = v>>21;
30+
*(ptr++) = (v >> 7) | B;
31+
*(ptr++) = (v >> 14) | B;
32+
*(ptr++) = v >> 21;
7233
} else {
7334
*(ptr++) = v | B;
74-
*(ptr++) = (v>>7) | B;
75-
*(ptr++) = (v>>14) | B;
76-
*(ptr++) = (v>>21) | B;
77-
*(ptr++) = v>>28;
78-
}
79-
return reinterpret_cast<char*>(ptr);
80-
}
81-
82-
void PutVarint32(std::string* dst, uint32_t v) {
83-
char buf[5];
84-
char* ptr = EncodeVarint32(buf, v);
85-
dst->append(buf, ptr - buf);
86-
}
87-
88-
char* EncodeVarint64(char* dst, uint64_t v) {
89-
static const unsigned int B = 128;
90-
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
91-
while (v >= B) {
92-
*(ptr++) = (v & (B-1)) | B;
93-
v >>= 7;
35+
*(ptr++) = (v >> 7) | B;
36+
*(ptr++) = (v >> 14) | B;
37+
*(ptr++) = (v >> 21) | B;
38+
*(ptr++) = v >> 28;
9439
}
95-
*(ptr++) = static_cast<unsigned char>(v);
9640
return reinterpret_cast<char*>(ptr);
9741
}
9842

99-
void PutVarint64(std::string* dst, uint64_t v) {
100-
char buf[10];
101-
char* ptr = EncodeVarint64(buf, v);
102-
dst->append(buf, ptr - buf);
103-
}
104-
105-
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
106-
PutVarint32(dst, value.size());
107-
dst->append(value.data(), value.size());
108-
}
109-
110-
void PutLengthPrefixedSliceParts(std::string* dst,
111-
const SliceParts& slice_parts) {
112-
uint32_t total_bytes = 0;
113-
for (int i = 0; i < slice_parts.num_parts; ++i) {
114-
total_bytes += slice_parts.parts[i].size();
115-
}
116-
PutVarint32(dst, total_bytes);
117-
for (int i = 0; i < slice_parts.num_parts; ++i) {
118-
dst->append(slice_parts.parts[i].data(), slice_parts.parts[i].size());
119-
}
120-
}
121-
122-
int VarintLength(uint64_t v) {
123-
int len = 1;
124-
while (v >= 128) {
125-
v >>= 7;
126-
len++;
127-
}
128-
return len;
129-
}
130-
131-
const char* GetVarint32PtrFallback(const char* p,
132-
const char* limit,
43+
const char* GetVarint32PtrFallback(const char* p, const char* limit,
13344
uint32_t* value) {
13445
uint32_t result = 0;
13546
for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
@@ -147,18 +58,6 @@ const char* GetVarint32PtrFallback(const char* p,
14758
return nullptr;
14859
}
14960

150-
bool GetVarint32(Slice* input, uint32_t* value) {
151-
const char* p = input->data();
152-
const char* limit = p + input->size();
153-
const char* q = GetVarint32Ptr(p, limit, value);
154-
if (q == nullptr) {
155-
return false;
156-
} else {
157-
*input = Slice(q, limit - q);
158-
return true;
159-
}
160-
}
161-
16261
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
16362
uint64_t result = 0;
16463
for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
@@ -176,58 +75,6 @@ const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
17675
return nullptr;
17776
}
17877

179-
bool GetVarint64(Slice* input, uint64_t* value) {
180-
const char* p = input->data();
181-
const char* limit = p + input->size();
182-
const char* q = GetVarint64Ptr(p, limit, value);
183-
if (q == nullptr) {
184-
return false;
185-
} else {
186-
*input = Slice(q, limit - q);
187-
return true;
188-
}
189-
}
190-
191-
const char* GetLengthPrefixedSlice(const char* p, const char* limit,
192-
Slice* result) {
193-
uint32_t len;
194-
p = GetVarint32Ptr(p, limit, &len);
195-
if (p == nullptr) return nullptr;
196-
if (p + len > limit) return nullptr;
197-
*result = Slice(p, len);
198-
return p + len;
199-
}
200-
201-
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
202-
uint32_t len;
203-
if (GetVarint32(input, &len) &&
204-
input->size() >= len) {
205-
*result = Slice(input->data(), len);
206-
input->remove_prefix(len);
207-
return true;
208-
} else {
209-
return false;
210-
}
211-
}
212-
213-
Slice GetLengthPrefixedSlice(const char* data) {
214-
uint32_t len;
215-
const char* p = data;
216-
p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
217-
return Slice(p, len);
218-
}
219-
220-
Slice GetSliceUntil(Slice* slice, char delimiter) {
221-
uint32_t len;
222-
for (len = 0; len < slice->size() && slice->data()[len] != delimiter; ++len) {
223-
// nothing
224-
}
225-
226-
Slice ret(slice->data(), len);
227-
slice->remove_prefix(len + ((len < slice->size()) ? 1 : 0));
228-
return ret;
229-
}
230-
23178
void BitStreamPutInt(char* dst, size_t dstlen, size_t offset,
23279
uint32_t bits, uint64_t value) {
23380
assert((offset + bits + 7)/8 <= dstlen);
@@ -316,14 +163,4 @@ void BitStreamPutInt(std::string* dst, size_t offset, uint32_t bits,
316163
BitStreamGetInt(dst, offset, bits));
317164
}
318165

319-
uint64_t BitStreamGetInt(const std::string* src, size_t offset,
320-
uint32_t bits) {
321-
return BitStreamGetInt(src->data(), src->size(), offset, bits);
322-
}
323-
324-
uint64_t BitStreamGetInt(const Slice* src, size_t offset,
325-
uint32_t bits) {
326-
return BitStreamGetInt(src->data(), src->size(), offset, bits);
327-
}
328-
329166
} // namespace rocksdb

0 commit comments

Comments
 (0)