Skip to content
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

Add possibility for disabling inlining wasm in opencv.js #23344

Merged
merged 1 commit into from Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/js_tutorials/js_setup/js_setup/js_setup.markdown
Expand Up @@ -83,6 +83,9 @@ Building OpenCV.js from Source
It requires `python` and `cmake` installed in your development environment.

-# The build script builds asm.js version by default. To build WebAssembly version, append `--build_wasm` switch.
By default everything is bundled into one JavaScript file by `base64` encoding the WebAssembly code. For production
builds you can add `--disable_single_file` which will reduce total size by writing the WebAssembly code
to a dedicated `.wasm` file which the generated JavaScript file will automatically load.
Copy link
Member

Choose a reason for hiding this comment

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

Can this approach fix this issue as well? #21431. Which size reduction do you observe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Single file: 8.74 MB .js
  • Separate .wasm file: 170 kB .js + 6.43 MB .wasm

I.e. bundling the generated webassembly code with base64 is ~32-33 % larger in total compared with separate .wasm file (consistent with typical base64 encoding size increases).


For example, to build wasm version in `build_wasm` directory:
@code{.bash}
Expand Down
2 changes: 1 addition & 1 deletion modules/js/CMakeLists.txt
Expand Up @@ -70,7 +70,7 @@ if(COMPILE_FLAGS)
endif()

set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} --memory-init-file 0 -s TOTAL_MEMORY=128MB -s WASM_MEM_MAX=1GB -s ALLOW_MEMORY_GROWTH=1")
set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s MODULARIZE=1 -s SINGLE_FILE=1")
set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s MODULARIZE=1")
set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s EXPORT_NAME=\"'cv'\" -s DEMANGLE_SUPPORT=1")
set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s FORCE_FILESYSTEM=1 --use-preload-plugins --bind --post-js ${JS_HELPER} ${COMPILE_FLAGS}")
set_target_properties(${the_module} PROPERTIES LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS}")
Expand Down
3 changes: 3 additions & 0 deletions platforms/js/build_js.py
Expand Up @@ -180,6 +180,8 @@ def get_build_flags(self):
flags += "-s WASM=1 "
elif self.options.disable_wasm:
flags += "-s WASM=0 "
if not self.options.disable_single_file:
flags += "-s SINGLE_FILE=1 "
if self.options.threads:
flags += "-s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 "
else:
Expand Down Expand Up @@ -233,6 +235,7 @@ def build_loader(self):
parser.add_argument('--emscripten_dir', default=emscripten_dir, help="Path to Emscripten to use for build (deprecated in favor of 'emcmake' launcher)")
parser.add_argument('--build_wasm', action="store_true", help="Build OpenCV.js in WebAssembly format")
parser.add_argument('--disable_wasm', action="store_true", help="Build OpenCV.js in Asm.js format")
parser.add_argument('--disable_single_file', action="store_true", help="Do not merge JavaScript and WebAssembly into one single file")
Copy link
Member

Choose a reason for hiding this comment

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

I think it make sense highlight wasm in the flag naming. Something like --disable_wasm_merge or --build_standalone_wasm.

Copy link
Member

Choose a reason for hiding this comment

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

However, origin option is SINGLE_FILE so it's also logical to keep --disable_single_file 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, pros and cons, no strong opinion from my side on argument name 🙂 If we are to move away from single_file in the argument name I think I personally like your second suggestion the best, i.e. --build_standalone_wasm (we then avoid the word disable, which maybe makes the argument name slightly easier to interpret by removing a negation).

Happy to modify the PR if you decide on some other name instead of --disable_single_file.

parser.add_argument('--threads', action="store_true", help="Build OpenCV.js with threads optimization")
parser.add_argument('--simd', action="store_true", help="Build OpenCV.js with SIMD optimization")
parser.add_argument('--build_test', action="store_true", help="Build tests")
Expand Down