Skip to content

Commit 020cd65

Browse files
committed
Tell compiler which way jumps will usually go in mbfl_memory_device.c
Several functions in this file are very "hot". While tweaking the code to see if I could make it faster, and looking at disassembly of resulting code, I noticed that gcc -O3 was inconsistent in how it handled `if`/`else` statements. Sometimes, gcc would put the body of an `if` statement on the 'jump taken' side of a conditional branch, and at other times, it would put it on the 'jump not taken' side. Use `EXPECTED`/`UNEXPECTED` macros to (hopefully) help the compiler organize the code for better static branch prediction.
1 parent 5180a76 commit 020cd65

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void mbfl_memory_device_reset(mbfl_memory_device *device)
7070

7171
void mbfl_memory_device_unput(mbfl_memory_device *device)
7272
{
73-
if (device->pos > 0) {
73+
if (EXPECTED(device->pos > 0)) {
7474
device->pos--;
7575
}
7676
}
@@ -89,11 +89,11 @@ int mbfl_memory_device_output(int c, void *data)
8989
{
9090
mbfl_memory_device *device = (mbfl_memory_device *)data;
9191

92-
if (device->pos >= device->length) {
92+
if (UNEXPECTED(device->pos >= device->length)) {
9393
/* reallocate buffer */
9494
size_t newlen;
9595

96-
if (device->length > SIZE_MAX - device->allocsz) {
96+
if (UNEXPECTED(device->length > SIZE_MAX - device->allocsz)) {
9797
/* overflow */
9898
return -1;
9999
}
@@ -114,12 +114,12 @@ int mbfl_memory_device_strcat(mbfl_memory_device *device, const char *psrc)
114114

115115
int mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t len)
116116
{
117-
if (len > device->length - device->pos) {
117+
if (UNEXPECTED(len > device->length - device->pos)) {
118118
/* reallocate buffer */
119119
size_t newlen;
120120

121-
if (len > SIZE_MAX - MBFL_MEMORY_DEVICE_ALLOC_SIZE
122-
|| device->length > SIZE_MAX - (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)) {
121+
if (UNEXPECTED(len > SIZE_MAX - MBFL_MEMORY_DEVICE_ALLOC_SIZE
122+
|| device->length > SIZE_MAX - (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE))) {
123123
/* overflow */
124124
return -1;
125125
}
@@ -163,17 +163,15 @@ int mbfl_wchar_device_output(int c, void *data)
163163
{
164164
mbfl_wchar_device *device = (mbfl_wchar_device *)data;
165165

166-
if (device->pos >= device->length) {
166+
if (UNEXPECTED(device->pos >= device->length)) {
167167
/* reallocate buffer */
168-
size_t newlen;
169-
170-
if (device->length > SIZE_MAX - device->allocsz) {
168+
if (UNEXPECTED(device->length > SIZE_MAX - device->allocsz)) {
171169
/* overflow */
172170
return -1;
173171
}
174172

175-
newlen = device->length + device->allocsz;
176-
if (newlen > SIZE_MAX / sizeof(int)) {
173+
size_t newlen = device->length + device->allocsz;
174+
if (UNEXPECTED(newlen > SIZE_MAX / sizeof(int))) {
177175
/* overflow */
178176
return -1;
179177
}

0 commit comments

Comments
 (0)