Skip to content

Add a default_global_allocator feature so embedders can keep their own global allocator #804

Closed

Description

@fengmk2

oxc-node's Rust crate can be linked into another Node-API addon. Vite+ does this: it compiles oxc-node into its own native binding, next to rolldown_binding, so vpx script.ts can run oxc-node's hooks from the binary it already ships. That binary already has a global allocator, because rolldown_binding declares mimalloc. oxc-node declares one unconditionally (src/lib.rs#L35-L42), and a binary can only have one, so the build fails with rustc's error that the #[global_allocator] conflicts with the global allocator in another crate.

Proposal

Gate the allocator behind a feature, as rolldown_binding does with default_global_allocator:

#[cfg(all(
    not(target_arch = "x86"),
    not(target_arch = "arm"),
    not(target_family = "wasm"),
    not(all(target_os = "windows", target_arch = "aarch64")),
    not(feature = "default_global_allocator")
))]
#[global_allocator]
static ALLOC: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;
[features]
# Keep the system allocator, for hosts that declare their own.
default_global_allocator = []

Using the same name as Rolldown means an addon that links both enables the same feature on each. The published @oxc-node/core builds keep mimalloc, since the feature is off by default.

Today Vite+ removes the block in a patch.

Activity

  1. 17 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions