From 31b5ff5ab973d1b1ca0efda0bacb8403309336b2 Mon Sep 17 00:00:00 2001 From: luaneko Date: Thu, 26 Jun 2025 16:54:58 +1000 Subject: [PATCH] Make all modules feature-gated to improve compile time --- Cargo.lock | 61 -------------------------------------------- Cargo.toml | 13 +++++++--- crates/lb/Cargo.toml | 9 ++++--- crates/lb/src/lib.rs | 11 +++++--- src/lib.rs | 20 ++++++++++----- src/main.rs | 23 +++++++++-------- 6 files changed, 50 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6f4fe0..0edbc65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1031,14 +1031,12 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" name = "lb" version = "0.0.1" dependencies = [ - "camino", "derive_more", "luaffi", "luajit", "sysexits", "thiserror", "tokio", - "tracing", ] [[package]] @@ -1103,16 +1101,6 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956" -[[package]] -name = "lock_api" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.27" @@ -1326,29 +1314,6 @@ version = "4.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48dd4f4a2c8405440fd0462561f0e5806bd0f77e86f51c761481bdd4018b545e" -[[package]] -name = "parking_lot" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets 0.52.6", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1523,15 +1488,6 @@ dependencies = [ "getrandom 0.2.16", ] -[[package]] -name = "redox_syscall" -version = "0.5.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d04b7d0ee6b4a0207a0a7adb104d23ecb0b47d6beae7152d0fa34b692b29fd6" -dependencies = [ - "bitflags", -] - [[package]] name = "regex" version = "1.11.1" @@ -1622,12 +1578,6 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "semver" version = "1.0.26" @@ -1684,15 +1634,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "signal-hook-registry" -version = "1.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9203b8055f63a2a00e2f593bb0510367fe707d7ff1e5c872de2f537b339e5410" -dependencies = [ - "libc", -] - [[package]] name = "simdutf8" version = "0.1.5" @@ -1849,9 +1790,7 @@ dependencies = [ "bytes", "libc", "mio", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", "tokio-macros", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 073b51d..6a1d926 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,17 +30,24 @@ repository.workspace = true dev.panic = "abort" release.panic = "abort" +[features] +default = ["task", "fs", "net"] +task = ["lb/task"] +fs = ["lb/fs"] +net = ["lb/net"] +tokio-console = ["dep:console-subscriber"] + [dependencies] clap = { version = "4.5.40", features = ["derive", "env"] } -console-subscriber = "0.4.1" +console-subscriber = { version = "0.4.1", optional = true } lb = { path = "crates/lb" } luajit = { path = "crates/luajit", features = ["runtime"] } mimalloc = "0.1.47" owo-colors = "4.2.1" sysexits = "0.9.0" -tokio = { version = "1.45.1", features = ["full", "tracing"] } +tokio = { version = "1.45.1", features = ["rt", "rt-multi-thread"] } tracing = "0.1.41" -tracing-subscriber = "0.3.19" +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } [build-dependencies] vergen-git2 = { version = "1.0.7", features = ["cargo", "rustc"] } diff --git a/crates/lb/Cargo.toml b/crates/lb/Cargo.toml index 2f29ace..e51a74c 100644 --- a/crates/lb/Cargo.toml +++ b/crates/lb/Cargo.toml @@ -7,12 +7,15 @@ authors.workspace = true homepage.workspace = true repository.workspace = true +[features] +task = [] +fs = ["tokio/fs"] +net = ["tokio/net"] + [dependencies] -camino = "1.1.10" derive_more = { version = "2.0.1", features = ["full"] } luaffi = { path = "../luaffi" } luajit = { path = "../luajit" } sysexits = "0.9.0" thiserror = "2.0.12" -tokio = { version = "1.45.1", features = ["rt", "time", "fs", "net", "process", "signal", "tracing"] } -tracing = "0.1.41" +tokio = { version = "1.45.1", features = ["rt"] } diff --git a/crates/lb/src/lib.rs b/crates/lb/src/lib.rs index 448448a..fb6a5a1 100644 --- a/crates/lb/src/lib.rs +++ b/crates/lb/src/lib.rs @@ -1,5 +1,10 @@ -pub mod chan; -pub mod fs; -pub mod net; pub mod runtime; + +#[cfg(feature = "task")] +pub mod chan; +#[cfg(feature = "fs")] +pub mod fs; +#[cfg(feature = "net")] +pub mod net; +#[cfg(feature = "task")] pub mod task; diff --git a/src/lib.rs b/src/lib.rs index f4e8bba..9b6ba8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,20 @@ +#[cfg(feature = "task")] pub use lb::chan; +#[cfg(feature = "fs")] pub use lb::fs; +#[cfg(feature = "net")] pub use lb::net; +#[cfg(feature = "task")] pub use lb::task; #[doc(hidden)] -pub fn load_modules(runtime: &mut lb::runtime::Builder) { - // core modules - runtime - .module::("lb:task") - .module::("lb:channel") - .module::("lb:fs") - .module::("lb:net"); +pub fn open(#[allow(unused)] rt: &mut lb::runtime::Builder) { + #[cfg(feature = "task")] + rt.module::("lb:task"); + #[cfg(feature = "task")] + rt.module::("lb:channel"); + #[cfg(feature = "fs")] + rt.module::("lb:fs"); + #[cfg(feature = "net")] + rt.module::("lb:net"); } diff --git a/src/main.rs b/src/main.rs index 4ee66ac..eda23b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ use clap::Parser; use mimalloc::MiMalloc; use owo_colors::OwoColorize; -use std::{ - backtrace::Backtrace, fmt::Display, net::SocketAddr, num::NonZero, panic, process, thread, -}; +use std::{backtrace::Backtrace, fmt::Display, num::NonZero, panic, process, thread}; use sysexits::ExitCode; #[global_allocator] @@ -84,10 +82,12 @@ struct Args { blocking_threads: NonZero, /// Enable tokio-console integration. + #[cfg(feature = "tokio-console")] #[clap(long, help_heading = "Debugging")] enable_console: bool, /// tokio-console publish address. + #[cfg(feature = "tokio-console")] #[clap( long, help_heading = "Debugging", @@ -95,7 +95,7 @@ struct Args { default_value = "127.0.0.1:6669", requires = "enable_console" )] - console_addr: SocketAddr, + console_addr: std::net::SocketAddr, /// Dump internal data. #[clap( @@ -162,7 +162,7 @@ fn unwrap_exit(code: ExitCode) -> impl FnOnce(E) -> T { fn init_logger(args: &Args) { use tracing::level_filters::LevelFilter; - use tracing_subscriber::{Layer, util::*}; + use tracing_subscriber::util::*; let log = tracing_subscriber::fmt() .compact() @@ -176,16 +176,19 @@ fn init_logger(args: &Args) { .with_target(false) .finish(); - if args.enable_console { + #[cfg(feature = "tokio-console")] + { + use tracing_subscriber::Layer; console_subscriber::ConsoleLayer::builder() .with_default_env() .server_addr(args.console_addr) .spawn() .with_subscriber(log) - .init() - } else { - log.init() + .init(); } + + #[cfg(not(feature = "tokio-console"))] + log.init(); } fn init_tokio(args: &Args) -> tokio::runtime::Runtime { @@ -207,7 +210,7 @@ fn init_tokio(args: &Args) -> tokio::runtime::Runtime { fn init_lua(args: &Args) -> lb::runtime::Runtime { let mut rt = lb::runtime::Builder::new(); - luby::load_modules(&mut rt); + luby::open(&mut rt); if args.dump.iter().find(|s| *s == "cdef").is_some() { print!("{}", rt.registry());