From edae65fb5fd9226d21d57fc2bef242cb0d013575 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Thu, 7 Aug 2025 01:23:20 -0300 Subject: [PATCH] global allocator should not be set for library, only for executables --- Cargo.lock | 10 +++++----- Cargo.toml | 1 + cli/Cargo.toml | 1 + cli/main.rs | 4 ++++ core/Cargo.toml | 1 - core/lib.rs | 4 ---- extensions/core/README.md | 21 +++++++++++++++++++++ 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 014ed79e4..977d56213 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2080,9 +2080,9 @@ checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" [[package]] name = "libmimalloc-sys" -version = "0.1.42" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec9d6fac27761dabcd4ee73571cdb06b7022dc99089acbe5435691edffaac0f4" +checksum = "bf88cd67e9de251c1781dbe2f641a1a3ad66eaae831b8a2c38fbdc5ddae16d4d" dependencies = [ "cc", "libc", @@ -2380,9 +2380,9 @@ dependencies = [ [[package]] name = "mimalloc" -version = "0.1.46" +version = "0.1.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "995942f432bbb4822a7e9c3faa87a695185b0d09273ba85f097b54f4e458f2af" +checksum = "b1791cbe101e95af5764f06f20f6760521f7158f69dbf9d6baf941ee1bf6bc40" dependencies = [ "libmimalloc-sys", ] @@ -4192,6 +4192,7 @@ dependencies = [ "libc", "limbo_completion", "miette", + "mimalloc", "nu-ansi-term 0.50.1", "rustyline", "schemars", @@ -4232,7 +4233,6 @@ dependencies = [ "lru", "memory-stats", "miette", - "mimalloc", "pack1", "parking_lot", "paste", diff --git a/Cargo.toml b/Cargo.toml index 1cea356af..40779d963 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ strum_macros = "0.26" serde = "1.0" serde_json = "1.0" anyhow = "1.0.98" +mimalloc = { version = "0.1.47", default-features = false } [profile.release] debug = "line-tables-only" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index cf64436a1..74a37cd47 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -46,6 +46,7 @@ serde = { workspace = true, features = ["derive"]} validator = {version = "0.20.0", features = ["derive"]} toml_edit = {version = "0.22.24", features = ["serde"]} serde_json = "1.0" +mimalloc = { workspace = true } [features] default = ["io_uring"] diff --git a/cli/main.rs b/cli/main.rs index f60aaaad5..a2df75cba 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -15,6 +15,10 @@ use std::{ sync::{atomic::Ordering, LazyLock}, }; +#[cfg(not(target_family = "wasm"))] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + fn rustyline_config() -> Config { Config::builder() .completion_type(rustyline::CompletionType::List) diff --git a/core/Cargo.toml b/core/Cargo.toml index 29afcd6e6..ae145386a 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -38,7 +38,6 @@ rustix = { version = "1.0.5", features = ["fs"] } libc = { version = "0.2.172" } [target.'cfg(not(target_family = "wasm"))'.dependencies] -mimalloc = { version = "0.1.46", default-features = false } libloading = "0.8.6" [dependencies] diff --git a/core/lib.rs b/core/lib.rs index 903e1b552..91ba7346d 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -38,10 +38,6 @@ pub mod numeric; #[cfg(not(feature = "fuzz"))] mod numeric; -#[cfg(not(target_family = "wasm"))] -#[global_allocator] -static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; - use crate::translate::optimizer::optimize_plan; use crate::translate::pragma::TURSO_CDC_DEFAULT_TABLE_NAME; #[cfg(all(feature = "fs", feature = "conn_raw_api"))] diff --git a/extensions/core/README.md b/extensions/core/README.md index 450113108..de7dedb6d 100644 --- a/extensions/core/README.md +++ b/extensions/core/README.md @@ -2,6 +2,27 @@ The `turso_ext` crate simplifies the creation and registration of libraries meant to extend the functionality of `Turso`, that can be loaded like traditional `sqlite3` extensions, but are able to be written in much more ergonomic Rust. + +**Attention** +If you wish to dynamic linking, currently you need to set your global_allocator to `MiMalloc` for non-wasm targets. Or you can clone the repo and change `macros/src/ext/mod.rs` to use your custom allocator: + +E.g +```diff +#[cfg(not(target_family = "wasm"))] +#[cfg(not(feature = "static"))] +#[global_allocator] +- static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; ++ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc +``` + +Then add the allocator to the extension you want to use + +```toml +[target.'cfg(not(target_family = "wasm"))'.dependencies] +tikv-jemallocator = "0.5" +``` + +Then you just the compile the dynamic library and extract the necessary `.so/.dylib/.dll` files from `.target` folder. ---