Skip to content

Commit 50ba52d

Browse files
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.
1 parent 4b9416a commit 50ba52d

8 files changed

Lines changed: 51 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ mac_address = "1.1.3"
233233
malachite-bigint = "0.9.1"
234234
malachite-q = "0.9.1"
235235
malachite-base = "0.9.1"
236-
maplit = "1.0.2"
237236
md-5 = "0.10.1"
238237
memchr = "2.8.0"
239238
memmap2 = "0.9.10"
@@ -248,7 +247,6 @@ openssl = "0.10.79"
248247
openssl-sys = "0.9.110"
249248
openssl-probe = "0.2.1"
250249
optional = "0.5"
251-
page_size = "0.6"
252250
parking_lot = "0.12.3"
253251
paste = "1.0.15"
254252
pbkdf2 = "0.12"
@@ -263,7 +261,7 @@ radium = "1.1.1"
263261
rand = "0.9"
264262
rand_core = { version = "0.9", features = ["os_rng"] }
265263
result-like = "0.5.0"
266-
rustix = { version = "1.1", features = ["event", "system"] }
264+
rustix = { version = "1.1", features = ["event", "param", "system"] }
267265
rustls = { version = "0.23.39", default-features = false }
268266
rustls-native-certs = "0.8"
269267
rustls-pemfile = "2.2"

crates/derive-impl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rustpython-doc = { workspace = true }
1616
itertools = { workspace = true }
1717
syn = { workspace = true, features = ["full", "extra-traits"] }
1818

19-
maplit = { workspace = true }
2019
proc-macro2 = { workspace = true }
2120
quote = { workspace = true }
2221
syn-ext = { workspace = true, features = ["full"] }

crates/derive-impl/src/compile_bytecode.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ impl CompilationSource {
8383
CompilationSourceKind::Dir { base, rel_path } => {
8484
self.compile_dir(base, &base.join(rel_path), "", mode, compiler)
8585
}
86-
_ => Ok(hashmap! {
87-
module_name.to_string() => CompiledModule {
86+
_ => Ok(core::iter::once((
87+
module_name.to_string(),
88+
CompiledModule {
8889
code: self.compile_single(mode, module_name, compiler)?,
8990
package: false,
9091
},
91-
}),
92+
))
93+
.collect()),
9294
}
9395
}
9496

crates/derive-impl/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
extern crate proc_macro;
66

7-
#[macro_use]
8-
extern crate maplit;
9-
107
#[macro_use]
118
mod error;
129
#[macro_use]

crates/host_env/src/os.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use {
2525
Storage::FileSystem::{
2626
FILE_FLAG_BACKUP_SEMANTICS, INVALID_SET_FILE_POINTER, SetFilePointer, SetFileTime,
2727
},
28+
System::SystemInformation::{GetSystemInfo, SYSTEM_INFO},
2829
},
2930
};
3031

@@ -132,6 +133,48 @@ pub fn cpu_count() -> usize {
132133
1
133134
}
134135

136+
#[cfg(unix)]
137+
pub fn page_size() -> usize {
138+
rustix::param::page_size()
139+
}
140+
141+
#[cfg(target_arch = "wasm32")]
142+
pub const fn page_size() -> usize {
143+
// WebAssembly's page size is a constant defined by the spec.
144+
1024 * 64
145+
}
146+
147+
#[cfg(windows)]
148+
pub fn page_size() -> usize {
149+
let info = SYSTEM_INFO::default();
150+
unsafe {
151+
GetSystemInfo(&mut info);
152+
}
153+
info.dwPageSize as _
154+
}
155+
156+
#[cfg(unix)]
157+
pub fn alloc_granularity() -> usize {
158+
// On Unix-likes, the page size is the smallest allocation unit rather than a separate concept
159+
// of allocation granularity.
160+
page_size()
161+
}
162+
163+
#[cfg(target_arch = "wasm32")]
164+
pub const fn alloc_granularity() -> usize {
165+
// Like Unix, WebAssembly doesn't separate page size and alloc granularity.
166+
page_size()
167+
}
168+
169+
#[cfg(windows)]
170+
pub fn page_size() -> usize {
171+
let info = SYSTEM_INFO::default();
172+
unsafe {
173+
GetSystemInfo(&mut info);
174+
}
175+
info.dwAllocationGranularity as _
176+
}
177+
135178
pub fn device_encoding(_fd: i32) -> Option<String> {
136179
#[cfg(any(
137180
target_os = "android",

crates/stdlib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ uuid = { workspace = true, features = ["v1"] }
102102

103103
# mmap + socket dependencies
104104
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
105-
page_size = { workspace = true }
106105
gethostname = { workspace = true }
107106
socket2 = { workspace = true, features = ["all"] }
108107
dns-lookup = { workspace = true }

crates/stdlib/src/mmap.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,15 @@ mod mmap {
131131
#[pyattr]
132132
const ACCESS_COPY: u32 = AccessMode::Copy as u32;
133133

134-
#[cfg(not(target_arch = "wasm32"))]
135134
#[pyattr(name = "PAGESIZE", once)]
136135
fn page_size(_vm: &VirtualMachine) -> usize {
137-
page_size::get()
136+
rustpython_host_env::os::page_size()
138137
}
139138

140139
#[cfg(not(target_arch = "wasm32"))]
141140
#[pyattr(name = "ALLOCATIONGRANULARITY", once)]
142141
fn granularity(_vm: &VirtualMachine) -> usize {
143-
page_size::get_granularity()
142+
rustpython_host_env::os::alloc_granularity()
144143
}
145144

146145
#[pyattr(name = "error", once)]

0 commit comments

Comments
 (0)