Skip to content

Commit c5789f1

Browse files
committed
Add Linux Kernel style branch hinting
Ad likely/unlikely macros. Only added 2 obvious places. Need to do profiling to determine other sites that would be helpful.
1 parent 19300d4 commit c5789f1

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

auparse/lru.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string.h>
2727
#include <syslog.h>
2828
#include "lru.h"
29+
#include "gcc-attributes.h"
2930

3031
//#define DEBUG
3132

@@ -97,7 +98,7 @@ static unsigned int hash_name(const char *name)
9798
{
9899
unsigned int h = 5381;
99100
unsigned char c;
100-
while ((c = *(const unsigned char *)name++))
101+
while (likely((c = *(const unsigned char *)name++)))
101102
h = ((h << 5) + h) + c;
102103
return h;
103104
}

gcc-attributes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@
7070
# define __wur
7171
#endif
7272

73+
/* Linux kernel style branch prediction hints. */
74+
#ifndef likely
75+
# define likely(x) __builtin_expect(!!(x), 1)
76+
#endif
77+
78+
#ifndef unlikely
79+
# define unlikely(x) __builtin_expect(!!(x), 0)
80+
#endif
81+
7382
#endif /* AUDIT_GCC_ATTRIBUTES_H */

src/auditd-event.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static int format_raw(const struct audit_reply *rep)
354354
/* Replace \n with space so it looks nicer. */
355355
ptr = format_buf;
356356
while (*ptr) {
357-
if (*ptr == '\n')
357+
if (unlikely(*ptr == '\n'))
358358
*ptr = ' ';
359359
ptr++;
360360
}

0 commit comments

Comments
 (0)