Skip to content

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
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