diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-06-08 15:50:14 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-06-08 15:50:14 +0200 |
commit | cfaf2f58f51ca71818a3c322eb13532109f7020e (patch) | |
tree | e5dbe7502c21f2279f8cbc1ac73a4d7d2a1b384f | |
parent | d21121e59b21c73afa3622abe256771f1e73ff69 (diff) |
Add likely() and unlikely() macros
-rw-r--r-- | src/lexer.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/lexer.c b/src/lexer.c index 77e60f2..5196c64 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -11,6 +11,14 @@ #include "lexer.h" #include "unicode.h" +#ifdef __GNUC__ +# define likely(x) __builtin_expect(!!(x), 1) +# define unlikely(x) __builtin_expect(!!(x), 0) +#else +# define likely(x) (x) +# define unlikely(x) (x) +#endif + #define SIZE_WDTH (sizeof(size_t) * 8) static bool skip_comment(const uchar **, const uchar *); @@ -36,7 +44,7 @@ lexstring(const uchar *code, size_t codesz) struct lexemes_soa data = mk_lexemes_soa(); const uchar *start = code, *end = start + codesz; - while (code < end) { + while (likely(code < end)) { const uchar *spnbeg = code, *spnend; rune ch = utf8_decode(&code); @@ -90,17 +98,17 @@ lexstring(const uchar *code, size_t codesz) data.strs[data.len].p = spnbeg; spnend = code; - while (code < end && rune_is_xidc(ch)) { + while (likely(code < end) && rune_is_xidc(ch)) { spnend = code; ch = utf8_decode(&code); } - if (code < end) + if (likely(code < end)) code = spnend; data.strs[data.len++].len = spnend - spnbeg; } - if (data.len == data.cap) + if (unlikely(data.len == data.cap)) lexemes_soa_resz(&data); } @@ -113,7 +121,7 @@ skip_comment(const uchar **ptr, const uchar *end) int nst = 1; const uchar *p = *ptr; - for (p++; p < end; p++) { + for (p++; likely(p < end); p++) { if (p + 1 < end) { if (p[0] == '*' && p[1] == '/') { p++; |