Skip to content

Reduce CppInterOp Emscripten shared library size #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcbarton
Copy link
Collaborator

@mcbarton mcbarton commented Jul 1, 2025

Description

Please include a summary of changes, motivation and context for this PR.

@vgvassilev This change locally reduced the Emscripten shared library size to 47/48 Mb (depends of whether you use dir -lh or du -lh). It still passed all tests and xeus-cpp still could run all the notebook cells it could before (exception throwing is broken in CppInterOps deployment at the moment for some reason). I have cleared the cache of all llvm 20 Emscripten builds for this PR.

Fixes # (issue)

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Requires documentation updates

Testing

Please describe the test(s) that you added and ran to verify your changes.

Checklist

  • I have read the contribution guide recently

@mcbarton mcbarton requested a review from vgvassilev July 1, 2025 14:42
Copy link

codecov bot commented Jul 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.18%. Comparing base (2585d0d) to head (de7101c).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #655   +/-   ##
=======================================
  Coverage   79.18%   79.18%           
=======================================
  Files           9        9           
  Lines        3853     3853           
=======================================
  Hits         3051     3051           
  Misses        802      802           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anutosh491
Copy link
Collaborator

It's a compile time optimization flag I think.

Do we need to force it like this everywhere or can we have cmake handle this ?

@mcbarton
Copy link
Collaborator Author

mcbarton commented Jul 1, 2025

It's a compile time optimization flag I think.

Do we need to force it like this everywhere or can we have cmake handle this ?

It is both a link and compile time optimisation. See https://emscripten.org/docs/tools_reference/emcc.html#emcc-oz .

@mcbarton mcbarton force-pushed the Reduce-Emscripten-shared-library-size branch from ee75d01 to d9f6630 Compare July 1, 2025 15:25
@anutosh491
Copy link
Collaborator

Yeah but we still don't want users to look into/pass flags correct during the build correct?

We should be able to have cmake handle this.

@mcbarton
Copy link
Collaborator Author

mcbarton commented Jul 1, 2025

Yeah but we still don't want users to look into/pass flags correct during the build correct?

We should be able to have cmake handle this.

I really don't understand the point your trying to make. Using EMCC_CFLAGS in this way to perfectly legitimate way of doing the Emscripten build. Emscripten is designed to be effected by modifying this flag (see https://emscripten.org/docs/tools_reference/emcc.html#environment-variables). If a user changes what we have in the documentation for this flag, and messes something up, then that is on them. I don't understand how putting it in a cmake option changes that.

@anutosh491
Copy link
Collaborator

My point is simple. We already have compile time and link time flags being passed through cmake, I don't see a need to pass this externally.

@vgvassilev
Copy link
Contributor

I think what @anutosh491 means is that we need to make this flag as part of our default compiler flags in the CMakeLists.txt.

@mcbarton
Copy link
Collaborator Author

mcbarton commented Jul 1, 2025

@vgvassilev @anutosh491 I first tried locally using -DCMAKE_CXX_FLAGS="-Dwait4=__syscall_wait4 -Oz" -DCMAKE_C_FLAGS="-Oz" -DCMAKE_STATIC_LINKER_FLAGS="-Oz" (since I thought this might be the closest thing to EMCC_CFLAGS) and the llvm build crashed 15% into the build. I then tried -DCMAKE_CXX_FLAGS="-Dwait4=__syscall_wait4 -Oz" -DCMAKE_C_FLAGS="-Oz" which although I could build llvm with this option, had no effect on the size of CppInterOps shared library, or the llvm libraries. I do not know how to proceed now. If you have any other cmake options you would like me to try, let me know. If you don't, I think we should proceed using EMCC_CFLAGS.

@mcbarton
Copy link
Collaborator Author

mcbarton commented Jul 1, 2025

Putting a note here in case I forget. Using EMCC_CFLAGS="-flto" in the llvm build doesn't end up effecting the shared library size on its own. Used in conjuncation with Oz then the size falls slightly to 45/46 Mb.

@mcbarton mcbarton force-pushed the Reduce-Emscripten-shared-library-size branch from db280e0 to de7101c Compare July 2, 2025 10:38
@mcbarton mcbarton changed the title Use EMCC_CFLAGS="-Oz" during Emscripten llvm build to reduce CppInterOp shared library size to 48Mb Reduce CppInterOp Emscripten shared library size to 48Mb Jul 2, 2025
@mcbarton mcbarton changed the title Reduce CppInterOp Emscripten shared library size to 48Mb Reduce CppInterOp Emscripten shared library size Jul 2, 2025
@mcbarton
Copy link
Collaborator Author

mcbarton commented Jul 2, 2025

I have updated this PR to use the flto flag as well, and to compile+link CppInterOp shared library with these flags too. This reduces the shared library size to 44/45 Mb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants