Skip to content

Commit 060b622

Browse files
committed
Mark branches as likely/unlikely
1 parent 77c59e7 commit 060b622

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/generic/stage1_find_marks.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ really_inline void find_structural_bits_64(
169169
}
170170

171171
int find_structural_bits(const uint8_t *buf, size_t len, simdjson::ParsedJson &pj) {
172-
if (len > pj.byte_capacity) {
172+
if (unlikely(len > pj.byte_capacity)) {
173173
std::cerr << "Your ParsedJson object only supports documents up to "
174174
<< pj.byte_capacity << " bytes but you are trying to process "
175175
<< len << " bytes" << std::endl;
@@ -207,7 +207,7 @@ int find_structural_bits(const uint8_t *buf, size_t len, simdjson::ParsedJson &p
207207
/* If we have a final chunk of less than 64 bytes, pad it to 64 with
208208
* spaces before processing it (otherwise, we risk invalidating the UTF-8
209209
* checks). */
210-
if (idx < len) {
210+
if (likely(idx < len)) {
211211
uint8_t tmp_buf[64];
212212
memset(tmp_buf, 0x20, 64);
213213
memcpy(tmp_buf, buf + idx, len - idx);
@@ -217,22 +217,21 @@ int find_structural_bits(const uint8_t *buf, size_t len, simdjson::ParsedJson &p
217217
idx += 64;
218218
}
219219

220-
/* finally, flatten out the remaining structurals from the last iteration
221-
*/
220+
/* finally, flatten out the remaining structurals from the last iteration */
222221
flatten_bits(base_ptr, idx, structurals);
223222

224223
simdjson::ErrorValues error = detect_errors_on_eof(unescaped_chars_error, prev_in_string);
225-
if (error != simdjson::SUCCESS) {
224+
if (unlikely(error != simdjson::SUCCESS)) {
226225
return error;
227226
}
228227

229228
pj.n_structural_indexes = base_ptr - pj.structural_indexes;
230229
/* a valid JSON file cannot have zero structural indexes - we should have
231230
* found something */
232-
if (pj.n_structural_indexes == 0u) {
231+
if (unlikely(pj.n_structural_indexes == 0u)) {
233232
return simdjson::EMPTY;
234233
}
235-
if (pj.structural_indexes[pj.n_structural_indexes - 1] > len) {
234+
if (unlikely(pj.structural_indexes[pj.n_structural_indexes - 1] > len)) {
236235
return simdjson::UNEXPECTED_ERROR;
237236
}
238237
if (len != pj.structural_indexes[pj.n_structural_indexes - 1]) {

src/generic/stage1_find_marks_flatten.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ really_inline void flatten_bits(uint32_t *&base_ptr, uint32_t idx, uint64_t bits
4343

4444
// Do the next 8 all together (we hope in most cases it won't happen at all
4545
// and the branch is easily predicted).
46-
if (cnt > 8) {
46+
if (unlikely(cnt > 8)) {
4747
for (int i=8; i<16; i++) {
4848
base_ptr[i] = idx + trailing_zeroes(bits);
4949
bits = bits & (bits - 1);
@@ -53,14 +53,15 @@ really_inline void flatten_bits(uint32_t *&base_ptr, uint32_t idx, uint64_t bits
5353
// Most files don't have 16+ structurals per block, so we take several basically guaranteed
5454
// branch mispredictions here. 16+ structurals per block means either punctuation ({} [] , :)
5555
// or the start of a value ("abc" true 123) every 4 characters.
56-
if (cnt > 16) {
56+
if (unlikely(cnt > 16)) {
5757
uint32_t i = 16;
5858
do {
5959
base_ptr[i] = idx + trailing_zeroes(bits);
6060
bits = bits & (bits - 1);
6161
i++;
6262
} while (i < cnt);
6363
}
64+
6465
base_ptr += cnt;
6566
}
6667
#endif // SIMDJSON_NAIVE_FLATTEN

src/haswell/simdutf8check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ struct utf8_checker<Architecture::HASWELL> {
218218
__m256i any_bits_on = in.reduce([&](auto a, auto b) {
219219
return _mm256_or_si256(a, b);
220220
});
221-
if ((_mm256_testz_si256(any_bits_on, high_bit)) == 1) {
221+
if (likely(_mm256_testz_si256(any_bits_on, high_bit) == 1)) {
222222
// it is ascii, we just check continuation
223223
this->has_error = _mm256_or_si256(
224224
_mm256_cmpgt_epi8(this->previous.carried_continuations,

src/haswell/stage1_find_marks.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ really_inline void flatten_bits(uint32_t *&base_ptr, uint32_t idx, uint64_t bits
107107

108108
// Do the next 8 all together (we hope in most cases it won't happen at all
109109
// and the branch is easily predicted).
110-
if (cnt > 8) {
110+
if (unlikely(cnt > 8)) {
111111
for (int i=8; i<16; i++) {
112112
base_ptr[i] = idx + trailing_zeroes(bits);
113113
bits = _blsr_u64(bits);
@@ -117,14 +117,15 @@ really_inline void flatten_bits(uint32_t *&base_ptr, uint32_t idx, uint64_t bits
117117
// Most files don't have 16+ structurals per block, so we take several basically guaranteed
118118
// branch mispredictions here. 16+ structurals per block means either punctuation ({} [] , :)
119119
// or the start of a value ("abc" true 123) every four characters.
120-
if (cnt > 16) {
120+
if (unlikely(cnt > 16)) {
121121
uint32_t i = 16;
122122
do {
123123
base_ptr[i] = idx + trailing_zeroes(bits);
124124
bits = _blsr_u64(bits);
125125
i++;
126126
} while (i < cnt);
127127
}
128+
128129
base_ptr += cnt;
129130
}
130131

0 commit comments

Comments
 (0)