Skip to content

[c++] hide symbols that aren't in the C API - #7496

Merged
jameslamb merged 12 commits into
mainfrom
fix/symbol-visibility
Oct 24, 2026
Merged

jameslamb merged 12 commits into
mainfrom
fix/symbol-visibility

Conversation

@jameslamb

@jameslamb jameslamb commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Notes for Reviewers

Contributes to #7327

By convention, we consider the LGBM_* functions in the C API to be the full public API of lib_lightgbm.{dll,dylib,so}. Everything else is considered an internal implementation detail and not intended to be accessed directly.

I say "by convention" because today there are 1,000+ symbols exported from the shared library on macOS / Linux. On my Mac using clang, I saw:

  • (991) various LightGBM internals, including tons of C++ types and methods
  • (98) intentionally-public LGBM_ functions
  • (59) various symbols from the vendored json11 code
  • (38) _Arrow* symbols from nanoarrow
  • (6) fmt:: symbols from fmt
  • (1) global locking variable from OpenMP (_.gomp_critical_user_.reduction.var)
code to see them (click me)
cmake -B build -S .
cmake --build build --target _lightgbm -j4
nm -gU ./lib_lightgbm.* | c++filt

This proposes limiting the visibility of all those unintentionally-exported symbols.

Benefits of these changes

  • reduced risk of conflicts with other libraries (which can cause segfaults, correctness problems, and other runtime issues)
  • reduced memory needed to load the library
  • faster library loading
  • faster compiled code (e.g. because the compiler can be more aggressive handling inlines because it knows they won't need to be used outside the library)
  • reduced binary size

See "Why is the new C++ visibility support so useful?" at https://gcc.gnu.org/wiki/Visibility for more.

Notes for Reviewers

I have been wanting to do this for a looooong time 😊

Is this safe?

I think so. I had an AI agent check (and I manually spot-checked) all the projects listed in https://github.com/lightgbm-org/LightGBM#external-unofficial-repositories. All but 1 of them either do not call into lib_lightgbm.{dll,dylib,so} at all or do so only through the C API. There's plenty of software that isn't public on GitHub but still... I think we should make this change.

The only exception is https://github.com/microsoft/LightGBM-transform, but that was abandoned 3 years ago, and builds its own copy of lib_lightgbm anyway, AND I'd like to drop support for it soon anyway: #7381

There's more work to do after this

I'd hoped this would fix #6595 but it doesn't, the reproducible example from there still reproduces.

But still, this will help prevent stuff like that!

References

Comment thread .ci/check-python-dists.sh
Comment on lines +42 to +43
--max-allowed-size-compressed '4M' \
--max-allowed-size-uncompressed '10M' \

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

slightly smaller binary, yay 😁

@jameslamb jameslamb changed the title WIP: [c++] hide symbols that aren't in the C API [c++] hide symbols that aren't in the C API Oct 18, 2026
@jameslamb
jameslamb marked this pull request as ready for review October 18, 2026 03:23
@jameslamb

Copy link
Copy Markdown
Member Author

I think this is ready for review. I'm very excited about this one, have been wanting to do it for a long time 😊

@borchero borchero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool! 🧹

@jameslamb

Copy link
Copy Markdown
Member Author

Thanks very much for taking a look (and all other reviews) @borchero !

@jameslamb
jameslamb merged commit 064a6db into main Oct 24, 2026
62 checks passed
@jameslamb
jameslamb deleted the fix/symbol-visibility branch October 24, 2026 01:12
@jameslamb jameslamb mentioned this pull request Nov 12, 2026
5 of 38 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python-package] SegFault on MacOS when pytorch is installed

2 participants