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

Reduce monomorphization load of various functions #2143

Merged
merged 4 commits into from
Jun 18, 2024

Conversation

seritools
Copy link
Contributor

@seritools seritools commented Jun 17, 2024

Outside of the stdlib and tokio, these functions were the highest-up in cargo-llvm-lines in our project that uses napi-rs extensively. These changes aim to reduce the amount of generated code (and hopefully also improve compile times because of it).

Before:

    LLVM Lines             Instantiations     Function name
    71253 (2.1%,  7.5%)    286 (0.3%,  4.5%)  napi::bindgen_runtime::js_values::object::<impl napi::js_values::object::JsObject>::set
    54972 (1.6%, 12.7%)    164 (0.2%,  9.0%)  napi::bindgen_runtime::js_values::object::<impl napi::js_values::object::JsObject>::get
    27324 (0.8%, 29.0%)    132 (0.1%, 18.4%)  napi::js_values::deferred::JsDeferred<Data,Resolver>::new
    25965 (0.7%, 31.2%)     32 (0.0%, 19.8%)  napi::threadsafe_function::call_js_cb

After:

    LLVM Lines             Instantiations     Function name
    17771 (0.5%, 33.8%)    286 (0.3%, 29.8%)  napi::bindgen_runtime::js_values::object::<impl napi::js_values::object::JsObject>::set
    18185 (0.5%, 32.7%)    164 (0.2%, 28.8%)  napi::bindgen_runtime::js_values::object::<impl napi::js_values::object::JsObject>::get
     8580 (0.3%, 41.6%)    132 (0.1%, 38.8%)  napi::js_values::deferred::JsDeferred<Data,Resolver>::new
    11405 (0.3%, 39.6%)     32 (0.0%, 36.0%)  napi::threadsafe_function::call_js_cb

After these changes, outside of napi::tokio_runtime::execute_tokio_future::{{closure}} there is no napi anymore in the top ~40 functions with the most lines generated.

Binary size savings in our project (release, fully-optimized build):

  • JsObject::{get, set}: 200 KiB
  • JsDeferred::new: 60 KiB
  • call_js_cb: 140 KiB (though with the new Return generic I wasn't able to outline as much in the 3.x version, so this might be lower now)

In total, it saved ~120K LLVM lines.

Note that these changes were first done on napi 2.6, but I've ported them over to main. All size measurements were done on the 2.6 version, but other than a slightly less effective change on call_js_cb this should be just as good on 3.x.

@Brooooooklyn Brooooooklyn merged commit bd5b86e into napi-rs:main Jun 18, 2024
49 of 52 checks passed
Brooooooklyn added a commit that referenced this pull request Jun 25, 2024
* refactor(napi): reduce monomorphization load of `JsObject::{get, set}`

Outside of `std::panicking::try` and `tokio`, these functions were the highest-up in `cargo-llvm-lines`.

Moves most of the logic out into non-generic functions.

* refactor(napi): reduce monomorphization load of `JsDeferred::new`

* refactor(napi): reduce monomorphization load of `call_js_cb`

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
@seritools seritools deleted the monomorphization-improvements branch August 29, 2024 11:47
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.

2 participants