From 482bcf36fdb5ff56462067f4cc67a74d05c0de18 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Sat, 16 May 2026 14:18:25 -0400 Subject: [PATCH] rustix/windows-sys for page size; drop maplit The `page_size` crate is a simple libc wrapper for Unix and uses `winapi-rs` for Windows. `windows-sys` and `windows-rs` are the modern alternatives for the unmaintained `winapi-rs`. Both crates are maintained by Microsoft - they're official. Getting the page size is a simple call for both Unix and Windows. Besides Unix and Windows, I also added the page size for wasm32 which the `page_size` crate did not support. `wasm32`'s page size is a constant that is defined by the spec, so I hard coded it without adding additional dependencies. Finally, I dropped `maplit` which is seven years old and only used in one place. Calling `collect()` with a single item iterator is idiomatic as well as better in this case because Rust can optimize it. `maplit` called `HashMap::insert` which over allocates to amortize future allocs. --- Cargo.lock | 8 ---- Cargo.toml | 4 +- crates/derive-impl/Cargo.toml | 1 - crates/derive-impl/src/compile_bytecode.rs | 8 ++-- crates/derive-impl/src/lib.rs | 3 -- crates/host_env/Cargo.toml | 4 +- crates/host_env/src/os.rs | 43 ++++++++++++++++++++++ crates/stdlib/Cargo.toml | 1 - crates/stdlib/src/mmap.rs | 5 +-- 9 files changed, 52 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3cd8d11551d..aa3c6d912dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2141,12 +2141,6 @@ dependencies = [ "quote", ] -[[package]] -name = "maplit" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" - [[package]] name = "md-5" version = "0.10.6" @@ -3331,7 +3325,6 @@ name = "rustpython-derive-impl" version = "0.5.0" dependencies = [ "itertools 0.14.0", - "maplit", "proc-macro2", "quote", "rustpython-compiler-core", @@ -3541,7 +3534,6 @@ dependencies = [ "openssl", "openssl-probe", "openssl-sys", - "page_size", "parking_lot", "paste", "pbkdf2", diff --git a/Cargo.toml b/Cargo.toml index a8942dc86a5..e343cf80e24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -233,7 +233,6 @@ mac_address = "1.1.3" malachite-bigint = "0.9.1" malachite-q = "0.9.1" malachite-base = "0.9.1" -maplit = "1.0.2" md-5 = "0.10.1" memchr = "2.8.0" memmap2 = "0.9.10" @@ -248,7 +247,6 @@ openssl = "0.10.79" openssl-sys = "0.9.110" openssl-probe = "0.2.1" optional = "0.5" -page_size = "0.6" parking_lot = "0.12.3" paste = "1.0.15" pbkdf2 = "0.12" @@ -263,7 +261,7 @@ radium = "1.1.1" rand = "0.9" rand_core = { version = "0.9", features = ["os_rng"] } result-like = "0.5.0" -rustix = { version = "1.1", features = ["event", "system"] } +rustix = { version = "1.1", features = ["event", "param", "system"] } rustls = { version = "0.23.39", default-features = false } rustls-native-certs = "0.8" rustls-pemfile = "2.2" diff --git a/crates/derive-impl/Cargo.toml b/crates/derive-impl/Cargo.toml index 383bf229171..7197c51ecfa 100644 --- a/crates/derive-impl/Cargo.toml +++ b/crates/derive-impl/Cargo.toml @@ -16,7 +16,6 @@ rustpython-doc = { workspace = true } itertools = { workspace = true } syn = { workspace = true, features = ["full", "extra-traits"] } -maplit = { workspace = true } proc-macro2 = { workspace = true } quote = { workspace = true } syn-ext = { workspace = true, features = ["full"] } diff --git a/crates/derive-impl/src/compile_bytecode.rs b/crates/derive-impl/src/compile_bytecode.rs index a640d59c577..05197440e69 100644 --- a/crates/derive-impl/src/compile_bytecode.rs +++ b/crates/derive-impl/src/compile_bytecode.rs @@ -83,12 +83,14 @@ impl CompilationSource { CompilationSourceKind::Dir { base, rel_path } => { self.compile_dir(base, &base.join(rel_path), "", mode, compiler) } - _ => Ok(hashmap! { - module_name.to_string() => CompiledModule { + _ => Ok(core::iter::once(( + module_name.to_string(), + CompiledModule { code: self.compile_single(mode, module_name, compiler)?, package: false, }, - }), + )) + .collect()), } } diff --git a/crates/derive-impl/src/lib.rs b/crates/derive-impl/src/lib.rs index 91f606bba1f..3d4b7991511 100644 --- a/crates/derive-impl/src/lib.rs +++ b/crates/derive-impl/src/lib.rs @@ -4,9 +4,6 @@ extern crate proc_macro; -#[macro_use] -extern crate maplit; - #[macro_use] mod error; #[macro_use] diff --git a/crates/host_env/Cargo.toml b/crates/host_env/Cargo.toml index 9903aae026e..197ae45a719 100644 --- a/crates/host_env/Cargo.toml +++ b/crates/host_env/Cargo.toml @@ -19,10 +19,8 @@ paste = { workspace = true } [target.'cfg(unix)'.dependencies] nix = { workspace = true } -uname = "0.1.1" - -[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "redox"))'.dependencies] rustix = { workspace = true } +uname = "0.1.1" [target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] num_cpus = "1.17.0" diff --git a/crates/host_env/src/os.rs b/crates/host_env/src/os.rs index 77849895052..41e535760ed 100644 --- a/crates/host_env/src/os.rs +++ b/crates/host_env/src/os.rs @@ -25,6 +25,7 @@ use { Storage::FileSystem::{ FILE_FLAG_BACKUP_SEMANTICS, INVALID_SET_FILE_POINTER, SetFilePointer, SetFileTime, }, + System::SystemInformation::{GetSystemInfo, SYSTEM_INFO}, }, }; @@ -132,6 +133,48 @@ pub fn cpu_count() -> usize { 1 } +#[cfg(unix)] +pub fn page_size() -> usize { + rustix::param::page_size() +} + +#[cfg(target_arch = "wasm32")] +pub const fn page_size() -> usize { + // WebAssembly's page size is a constant defined by the spec. + 1024 * 64 +} + +#[cfg(windows)] +pub fn page_size() -> usize { + let mut info = SYSTEM_INFO::default(); + unsafe { + GetSystemInfo(&mut info); + } + info.dwPageSize as _ +} + +#[cfg(unix)] +pub fn alloc_granularity() -> usize { + // On Unix-likes, the page size is the smallest allocation unit rather than a separate concept + // of allocation granularity. + page_size() +} + +#[cfg(target_arch = "wasm32")] +pub const fn alloc_granularity() -> usize { + // Like Unix, WebAssembly doesn't separate page size and alloc granularity. + page_size() +} + +#[cfg(windows)] +pub fn alloc_granularity() -> usize { + let mut info = SYSTEM_INFO::default(); + unsafe { + GetSystemInfo(&mut info); + } + info.dwAllocationGranularity as _ +} + pub fn device_encoding(_fd: i32) -> Option { #[cfg(any( target_os = "android", diff --git a/crates/stdlib/Cargo.toml b/crates/stdlib/Cargo.toml index 2b2279fb1ee..f77d467d63e 100644 --- a/crates/stdlib/Cargo.toml +++ b/crates/stdlib/Cargo.toml @@ -102,7 +102,6 @@ uuid = { workspace = true, features = ["v1"] } # mmap + socket dependencies [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -page_size = { workspace = true } gethostname = { workspace = true } socket2 = { workspace = true, features = ["all"] } dns-lookup = { workspace = true } diff --git a/crates/stdlib/src/mmap.rs b/crates/stdlib/src/mmap.rs index d6bb0d89aca..2332ee0e1ce 100644 --- a/crates/stdlib/src/mmap.rs +++ b/crates/stdlib/src/mmap.rs @@ -131,16 +131,15 @@ mod mmap { #[pyattr] const ACCESS_COPY: u32 = AccessMode::Copy as u32; - #[cfg(not(target_arch = "wasm32"))] #[pyattr(name = "PAGESIZE", once)] fn page_size(_vm: &VirtualMachine) -> usize { - page_size::get() + rustpython_host_env::os::page_size() } #[cfg(not(target_arch = "wasm32"))] #[pyattr(name = "ALLOCATIONGRANULARITY", once)] fn granularity(_vm: &VirtualMachine) -> usize { - page_size::get_granularity() + rustpython_host_env::os::alloc_granularity() } #[pyattr(name = "error", once)]