Skip to content

Commit bd22a90

Browse files
committed
Remove branch hints
Profile-guided optimization gives equal or better results
1 parent 139dab5 commit bd22a90

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

block.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include "config.h"
77
#include "block.h"
8-
#include "branch.h"
98
#include "page.h"
109

1110
struct block *block_new(size_t page_size) {
@@ -57,22 +56,22 @@ void block_free(struct block *block) {
5756

5857
__attribute__ ((noinline))
5958
static void *add_page(struct block *block) {
60-
if (UNLIKELY(block->count == block->size)) {
59+
if (block->count == block->size) {
6160
size_t new_size = block->size * 2;
6261
void **pages = realloc(block->pages, sizeof(*pages) * new_size);
63-
if (UNLIKELY(!pages)) {
62+
if (!pages) {
6463
return NULL;
6564
}
6665
block->pages = pages;
6766
size_t *offsets = realloc(block->offsets, sizeof(*offsets) * new_size);
68-
if (UNLIKELY(!offsets)) {
67+
if (!offsets) {
6968
return NULL;
7069
}
7170
block->offsets = offsets;
7271
block->size = new_size;
7372
}
7473
void *page = page_alloc(block->page_size);
75-
if (UNLIKELY(!page)) {
74+
if (!page) {
7675
return NULL;
7776
}
7877
block->pages[block->count] = page;
@@ -82,14 +81,14 @@ static void *add_page(struct block *block) {
8281
}
8382

8483
void *block_alloc(struct block *block, size_t size) {
85-
if (UNLIKELY(size > block->page_size)) {
84+
if (size > block->page_size) {
8685
return NULL;
8786
}
8887
size_t offset = block->offsets[block->count - 1];
8988
void *page;
90-
if (UNLIKELY(block->page_size - offset < size)) {
89+
if (block->page_size - offset < size) {
9190
page = add_page(block);
92-
if (UNLIKELY(!page)) {
91+
if (!page) {
9392
return NULL;
9493
}
9594
offset = 0;

branch.h

-2
This file was deleted.

optimize.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "config.h"
55
#include "optimize.h"
6-
#include "branch.h"
76

87
#ifdef INLINE_UNSIGNED
98
const static uint32_t unsigned_tag = 0x80000000;
@@ -52,7 +51,7 @@ static bool resize_counts(struct strings_frequency *frequency,
5251
}
5352
struct id_count *counts = realloc(frequency->counts,
5453
sizeof(*counts) * new_size);
55-
if (UNLIKELY(!counts)) {
54+
if (!counts) {
5655
return false;
5756
}
5857
memset(counts + frequency->size, 0,
@@ -92,14 +91,14 @@ bool strings_frequency_add(struct strings_frequency *frequency, uint32_t id) {
9291
return true;
9392
}
9493
#endif
95-
if (UNLIKELY(!id)) {
94+
if (!id) {
9695
return true;
9796
}
98-
if (UNLIKELY(frequency->sorted_by_count)) {
97+
if (frequency->sorted_by_count) {
9998
sort_by_id(frequency);
10099
}
101100
if (frequency->max_id < id) {
102-
if (UNLIKELY(frequency->size < id)) {
101+
if (frequency->size < id) {
103102
if (!resize_counts(frequency, id)) {
104103
return false;
105104
}
@@ -146,7 +145,7 @@ struct strings *strings_optimize(struct strings *strings,
146145
break;
147146
}
148147
const char *string = strings_lookup_id(strings, id_count->id);
149-
if (UNLIKELY(!string || !strings_intern(optimized, string))) {
148+
if (!string || !strings_intern(optimized, string)) {
150149
goto error;
151150
}
152151
}

strings.c

+18-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "config.h"
66
#include "strings.h"
77
#include "tree.h"
8-
#include "branch.h"
98

109
#ifdef INLINE_UNSIGNED
1110
#include "unsigned.h"
@@ -132,26 +131,26 @@ bool strings_hash_seed(struct strings *strings, uint32_t seed) {
132131
static tree_node_t *create_node(struct strings *strings, uint32_t hash,
133132
const char *string) {
134133
tree_node_t *node = block_alloc(strings->index, sizeof(*node));
135-
if (UNLIKELY(!node)) {
134+
if (!node) {
136135
return NULL;
137136
}
138137
node->hash = hash;
139138
node->next = NULL;
140139
node->id = strings->total + 1;
141-
if (UNLIKELY(node->id == id_overflow)) {
140+
if (node->id == id_overflow) {
142141
return NULL;
143142
}
144143

145144
size_t len = strlen(string);
146145
void *string_ptr = block_alloc(strings->strings, len + 1);
147-
if (UNLIKELY(!string_ptr)) {
146+
if (!string_ptr) {
148147
return NULL;
149148
}
150149
memcpy(string_ptr, string, len + 1);
151150
node->string = (const char *)string_ptr;
152151

153152
uint32_t *hash_ptr = block_alloc(strings->hashes, sizeof(*hash_ptr));
154-
if (UNLIKELY(!hash_ptr)) {
153+
if (!hash_ptr) {
155154
return NULL;
156155
}
157156
*hash_ptr = hash;
@@ -174,7 +173,7 @@ static bool strings_intern_collision(struct strings *strings,
174173
for (;;) {
175174
if (!node->next) {
176175
node = node->next = create_node(strings, hash, string);
177-
if (UNLIKELY(!node)) {
176+
if (!node) {
178177
return 0;
179178
}
180179
break;
@@ -195,7 +194,7 @@ uint32_t strings_intern(struct strings *strings, const char *string) {
195194
#ifdef INLINE_UNSIGNED
196195
if (is_small_unsigned(string)) {
197196
uint32_t number = to_unsigned(string);
198-
if (LIKELY(number < unsigned_tag)) {
197+
if (number < unsigned_tag) {
199198
return number | unsigned_tag;
200199
}
201200
}
@@ -204,11 +203,11 @@ uint32_t strings_intern(struct strings *strings, const char *string) {
204203
uint32_t hash = strings_hash(strings, string);
205204
tree_node_t *node = find_node(strings, hash);
206205
if (node) {
207-
if (UNLIKELY(strcmp(node->string, string))) {
206+
if (strcmp(node->string, string)) {
208207
return strings_intern_collision(strings, node, string, hash);
209208
}
210209
} else {
211-
if (UNLIKELY(!(node = create_node(strings, hash, string)))) {
210+
if (!(node = create_node(strings, hash, string))) {
212211
return 0;
213212
}
214213
tree_insert(&strings->hash_map, node);
@@ -221,7 +220,7 @@ uint32_t strings_lookup(const struct strings *strings, const char *string) {
221220
#ifdef INLINE_UNSIGNED
222221
if (is_small_unsigned(string)) {
223222
uint32_t number = to_unsigned(string);
224-
if (LIKELY(number < unsigned_tag)) {
223+
if (number < unsigned_tag) {
225224
return number | unsigned_tag;
226225
}
227226
}
@@ -231,7 +230,7 @@ uint32_t strings_lookup(const struct strings *strings, const char *string) {
231230
tree_node_t *node = find_node(strings, hash);
232231
if (node) {
233232
do {
234-
if (LIKELY(!strcmp(node->string, string))) {
233+
if (!strcmp(node->string, string)) {
235234
return node->id;
236235
}
237236
} while ((node = node->next));
@@ -248,7 +247,7 @@ const char *strings_lookup_id(struct strings *strings, uint32_t id) {
248247
}
249248
#endif
250249

251-
if (UNLIKELY(id > strings->total)) {
250+
if (id > strings->total) {
252251
return NULL;
253252
}
254253

@@ -262,7 +261,7 @@ const char *strings_lookup_id(struct strings *strings, uint32_t id) {
262261

263262
tree_node_t *node = find_node(strings, hash);
264263
do {
265-
if (LIKELY(node->id == id)) {
264+
if (node->id == id) {
266265
return node->string;
267266
}
268267
} while ((node = node->next));
@@ -280,16 +279,16 @@ void strings_cursor_init(struct strings_cursor *cursor,
280279

281280
bool strings_cursor_next(struct strings_cursor *cursor) {
282281
const struct block *block = cursor->strings;
283-
if (LIKELY(cursor->id)) {
282+
if (cursor->id) {
284283
const char *string = strings_cursor_string(cursor);
285284
cursor->offset += strlen(string) + 1;
286-
if (UNLIKELY(cursor->offset >= block->offsets[cursor->page])) {
285+
if (cursor->offset >= block->offsets[cursor->page]) {
287286
cursor->page++;
288287
cursor->offset = 0;
289288
}
290289
}
291-
if (UNLIKELY(cursor->page >= block->count ||
292-
cursor->offset >= block->offsets[cursor->page])) {
290+
if (cursor->page >= block->count ||
291+
cursor->offset >= block->offsets[cursor->page]) {
293292
cursor->id = 0;
294293
return false;
295294
}
@@ -298,7 +297,7 @@ bool strings_cursor_next(struct strings_cursor *cursor) {
298297
}
299298

300299
const char *strings_cursor_string(const struct strings_cursor *cursor) {
301-
if (UNLIKELY(!cursor->id)) {
300+
if (!cursor->id) {
302301
return NULL;
303302
}
304303
const void *page = cursor->strings->pages[cursor->page];
@@ -338,7 +337,7 @@ bool strings_restore(struct strings *strings,
338337
offset += sizeof(tree_node_t)) {
339338
node = (tree_node_t *)((uintptr_t)block->pages[page] + offset);
340339
existing = find_node(strings, node->hash);
341-
if (UNLIKELY(existing)) {
340+
if (existing) {
342341
while (existing->next) {
343342
existing = existing->next;
344343
}

0 commit comments

Comments
 (0)