Skip to content

Add expect.h to Replace Direct Uses of __builtin_expect()#86

Open
Lightning11wins wants to merge 7 commits intomasterfrom
expect
Open

Add expect.h to Replace Direct Uses of __builtin_expect()#86
Lightning11wins wants to merge 7 commits intomasterfrom
expect

Conversation

@Lightning11wins
Copy link
Contributor

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 clear LIKELY() and UNLIKELY() functions which still use __builtin_expect() under the hood. These are abstracted out into expect.h to 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).

@Lightning11wins Lightning11wins self-assigned this Feb 28, 2026
@Lightning11wins Lightning11wins added enhancement ai-review Request AI review of this PR labels Feb 28, 2026
@Lightning11wins Lightning11wins changed the title Add expect.h to replace __builtin_expect() Add expect.h to Replace Direct Uses of __builtin_expect() Feb 28, 2026
@greptile-apps
Copy link

greptile-apps bot commented Feb 28, 2026

Greptile Summary

This PR centralizes __builtin_expect() branch-prediction hints into a new expect.h header, replacing per-file duplicated compiler-compatibility boilerplate with clean LIKELY()/UNLIKELY() macros. It also correctly addresses the previously-raised concern that centrallix-lib's independent build system never probed for __builtin_expect — by adding CHECK_BUILTIN_EXPECT to centrallix-lib/aclocal.m4, calling it from centrallix-lib/configure.ac, and adding the HAVE_BUILTIN_EXPECT placeholder to cxlibconfig-all.h.in. Because centrallix-lib/Makefile.in compiles with @DEFS@ expanded directly into CFLAGS, all .c files — including memstr.c and strtcpy.c which do not explicitly #include the config header — will correctly see HAVE_BUILTIN_EXPECT as a command-line -D flag when the compiler supports the builtin.

Key changes:

  • New expect.h: Introduces LIKELY(x) and UNLIKELY(x) macros backed by __builtin_expect() when available, with pass-through fallbacks otherwise.
  • centrallix-lib build system: Adds autoconf probe for __builtin_expect, mirroring the equivalent check that already existed in centrallix/aclocal.m4.
  • Four .c files updated: memstr.c, strtcpy.c, mtlexer.c, qprintf.c now include expect.h and annotate error/boundary checks with UNLIKELY() and hot-path conditions with LIKELY().
  • One minor issue found: The module comment in expect.h incorrectly references (mtask.c, mtask.h) — a copy-paste artifact from another header.

Confidence Score: 4/5

  • This PR is safe to merge; it makes no logic changes and the build-system fix correctly propagates HAVE_BUILTIN_EXPECT to all affected files.
  • All previously-raised concerns (wrong #ifdef guard, missing centrallix-lib autoconf probe) have been resolved. The @defs@ expansion in CFLAGS ensures the define reaches every compiled file. The only remaining issue is a cosmetic copy-paste error in the expect.h module comment. No logic is changed in any of the four .c files.
  • centrallix-lib/include/expect.h — minor module comment copy-paste error; no functional impact.

Important Files Changed

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
Loading

Last reviewed commit: 099c390

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

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.
@Lightning11wins
Copy link
Contributor Author

@greptileai Whoops, nice catch. That should be fixed now.

Copy link
Contributor

@nboard nboard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@Lightning11wins
Copy link
Contributor Author

@greptileai There are new changes on this PR, please rereview them.

@Lightning11wins
Copy link
Contributor Author

@greptileai That should be fixed now, please rereview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request AI review of this PR enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants