Skip to content

Optimization: share a single napi_threadsafe_function for all EventQueues #727

@jrose-signal

Description

@jrose-signal

Today each EventQueue has its own napi_threadsafe_function to send tasks to the Node event loop. However, nodejs/node#38506 (by @indutny-signal) makes it faster to run multiple calls on the same napi_threadsafe_function than to hop between them. If Neon saves a single napi_threadsafe_function in its instance data, it can be shared among all EventQueues for a particular instance.

One complication with this is the reference/unref/has_ref API on EventQueue, which uses the lifetime of the napi_threadsafe_function to keep the main event loop alive. This would have to be implemented with refcounting alongside the single napi_threadsafe_function:

  1. The napi_threadsafe_function starts as unref with a refcount of 0.
  2. Creating an EventQueue adds one to the refcount.
  3. Going from 0 to 1 calls napi_ref_threadsafe_function.
  4. reference and unref modify the refcount only if they change the local has_ref flag.
  5. Going from 1 to 0 calls napi_unref_threadsafe_function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions