Add expect.h to Replace Direct Uses of __builtin_expect()#86
Add expect.h to Replace Direct Uses of __builtin_expect()#86Lightning11wins wants to merge 7 commits intomasterfrom
expect.h to Replace Direct Uses of __builtin_expect()#86Conversation
expect.h to replace __builtin_expect()expect.h to Replace Direct Uses of __builtin_expect()
Greptile SummaryThis PR centralizes Key changes:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| centrallix-lib/include/expect.h | New header introducing LIKELY()/UNLIKELY() macros guarded by HAVE_BUILTIN_EXPECT; minor copy-paste error in module comment references mtask.c/mtask.h instead of expect.h |
| centrallix-lib/aclocal.m4 | Adds CHECK_BUILTIN_EXPECT macro using AC_COMPILE_IFELSE to probe for __builtin_expect support and define HAVE_BUILTIN_EXPECT, consistent with the identical check already present in centrallix/aclocal.m4 |
| centrallix-lib/configure.ac | Adds call to CHECK_BUILTIN_EXPECT so that HAVE_BUILTIN_EXPECT is probed and defined during centrallix-lib configuration |
| centrallix-lib/include/cxlibconfig-all.h.in | Adds #undef HAVE_BUILTIN_EXPECT placeholder so autoheader correctly generates the define in the output config header |
| centrallix-lib/src/memstr.c | Adds #include "expect.h" and wraps the early-exit size check with UNLIKELY(); HAVE_BUILTIN_EXPECT reaches this file via @defs@ in CFLAGS so the macro is properly enabled |
| centrallix-lib/src/strtcpy.c | Adds #include "expect.h" and decorates early-exit checks and the inner copy loop with UNLIKELY()/LIKELY(); no logic changes |
| centrallix-lib/src/mtlexer.c | Adds #include "expect.h"; already pulled in cxlibconfig-internal.h for config defines, and HAVE_BUILTIN_EXPECT is also covered by @defs@ in CFLAGS |
| centrallix-lib/src/qprintf.c | Adds #include "expect.h"; already includes cxlibconfig-internal.h; LIKELY()/UNLIKELY() hints applied to hot paths in the quoting printf module |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["centrallix-lib/configure.ac\nCHECK_BUILTIN_EXPECT"] --> B["centrallix-lib/aclocal.m4\nAC_COMPILE_IFELSE"]
B -->|"compiler supports __builtin_expect"| C["AC_DEFINE HAVE_BUILTIN_EXPECT=1\nwritten to cxlibconfig-all.h"]
B -->|"not supported"| D["HAVE_BUILTIN_EXPECT undefined"]
C --> E["DEFS expanded in\ncentrallix-lib Makefile CFLAGS"]
E --> F["All .c files compiled\nwith -DHAVE_BUILTIN_EXPECT"]
F --> G["expect.h\n#ifdef HAVE_BUILTIN_EXPECT"]
G -->|"defined"| H["LIKELY → __builtin_expect(x,1)\nUNLIKELY → __builtin_expect(x,0)"]
G -->|"not defined"| I["LIKELY(x) → x\nUNLIKELY(x) → x"]
H --> J["memstr.c / strtcpy.c\nmtlexer.c / qprintf.c"]
I --> J
Last reviewed commit: 099c390
Replace invalid check for __builtin_expect() with a check for __GNUC__. Improve logic for detecting when __builtin_expect() is available to handle older versions of __GNUC__. Remove doc comments that will not be displayed in most editors. Add removed information to module description. Clean up comments.
|
@greptileai Whoops, nice catch. That should be fixed now. |
nboard
left a comment
There was a problem hiding this comment.
Looks good overall. I did have a few questions before we move forward
…available. Update code to use HAVE_BUILTIN_EXPECT macro. Update generated files with generated changes from rebuild. Add autom4te.cache to the git ignore for centrallix-lib (matching centrallix).
|
@greptileai There are new changes on this PR, please rereview them. |
|
@greptileai That should be fixed now, please rereview. |
Previous code used the
__builtin_expect()macro directly and included the same, nearly identical set of compiler directives for compilers that didn't support this feature in every file that used it. This new code introduces the more clearLIKELY()andUNLIKELY()functions which still use__builtin_expect()under the hood. These are abstracted out intoexpect.hto improve maintainability and allow for more detailed comments explaining these functions without excessive duplication.This PR makes no changes to logic, mainly focusing on improving code maintainability and readability. However, it does add a couple of new compiler hints in obvious cases (such as unlikely error checks).