Skip to content

Commit d3a7b90

Browse files
author
Zachary Whitley
committed
refactor: workspace-wide clippy and idiom cleanup
Non-functional lint-driven cleanups across the workspace: - use is_multiple_of / checked_div instead of manual modulo and zero guards - derive Default for CacheSlot instead of a hand-written impl - add a default MemoryRegion::is_empty - drop needless borrows (wat::parse_str(wrap(...))) and collapse a redundant if/else in the copy-BST emitter - minor tidy-ups in benches, tests, and the wasmtime host/imported paths Also gate the rust-cache step on !ACT so the local act runner skips it.
1 parent 5680960 commit d3a7b90

27 files changed

Lines changed: 95 additions & 114 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
components: clippy, rustfmt
1616
targets: wasm32-unknown-unknown
1717
- uses: Swatinem/rust-cache@v2
18+
if: ${{ !env.ACT }} # act's runner image has no node on PATH; cache is a no-op locally
1819
- name: cargo fmt --check
1920
run: cargo fmt --all -- --check
2021
- name: cargo clippy

bench-framework/runner/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ fn run_tvm_mm_sequential(size: u32, data: &[u8]) -> anyhow::Result<Sample> {
331331
let engine = enable_multi_memory()?;
332332
let module = Module::new(&engine, MM_WAT)?;
333333
// Pre-create two memories sized to comfortably hold our test data.
334-
let pages_needed = ((size as u64 + 65535) / 65536).max(1) as u32;
334+
let pages_needed = (size as u64).div_ceil(65536).max(1) as u32;
335335
let mut store = Store::new(&engine, ());
336336
let mem0 = Memory::new(
337337
&mut store,
@@ -361,7 +361,7 @@ fn run_tvm_mm_list(size: u32, bytes: &[u8], head_offset: u32) -> anyhow::Result<
361361
use wasmtime::{Memory, MemoryType};
362362
let engine = enable_multi_memory()?;
363363
let module = Module::new(&engine, MM_WAT)?;
364-
let pages_needed = ((size as u64 + 65535) / 65536).max(1) as u32;
364+
let pages_needed = (size as u64).div_ceil(65536).max(1) as u32;
365365
let mut store = Store::new(&engine, ());
366366
let mem0 = Memory::new(
367367
&mut store,
@@ -397,7 +397,7 @@ fn run_tvm_mm_columnar(
397397
use wasmtime::{Memory, MemoryType};
398398
let engine = enable_multi_memory()?;
399399
let module = Module::new(&engine, MM_WAT)?;
400-
let pages_needed = ((size as u64 + 65535) / 65536).max(1) as u32;
400+
let pages_needed = (size as u64).div_ceil(65536).max(1) as u32;
401401
let mut store = Store::new(&engine, ());
402402
// Each column gets its own imported memory.
403403
let mem_a = Memory::new(
@@ -1156,7 +1156,7 @@ fn bench_spill_driven(
11561156
let h = store.data_mut().directory.alloc(r, resident_budget)?;
11571157
let packed = (h.region_id as i64) << 48 | (h.generation as i64) << 32 | (h.offset as i64);
11581158
// Write a few bytes so the spill has something real to persist.
1159-
store.data_mut().directory.write(h, &vec![0xAA; 64])?;
1159+
store.data_mut().directory.write(h, &[0xAA; 64])?;
11601160
handles.push(packed);
11611161
}
11621162

@@ -1299,10 +1299,10 @@ fn bench_large_ws(
12991299
handles_bytes.extend_from_slice(&p.to_le_bytes());
13001300
}
13011301
// Grow memory if needed.
1302-
let needed_pages = ((handles_offset as usize + handles_bytes.len()) + 65535) / 65536;
1302+
let needed_pages = (handles_offset as usize + handles_bytes.len()).div_ceil(65536);
13031303
let current_size = memory.data_size(&store);
13041304
if current_size < needed_pages * 65536 {
1305-
let extra = (needed_pages * 65536 - current_size + 65535) / 65536;
1305+
let extra = (needed_pages * 65536 - current_size).div_ceil(65536);
13061306
memory.grow(&mut store, extra as u64)?;
13071307
}
13081308
memory.write(&mut store, handles_offset as usize, &handles_bytes)?;

crates/tvm-core/src/allocator.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,7 @@ pub struct SlabAllocator {
239239

240240
impl SlabAllocator {
241241
pub fn new(capacity: u32, class_size: u32) -> Self {
242-
let n_slots = if class_size == 0 {
243-
0
244-
} else {
245-
capacity / class_size
246-
};
242+
let n_slots = capacity.checked_div(class_size).unwrap_or(0);
247243
// Push slots in reverse so `pop()` hands out offset 0 first.
248244
let free_slots: Vec<u32> = (0..n_slots).rev().map(|i| i * class_size).collect();
249245
Self {
@@ -283,7 +279,7 @@ impl SlabAllocator {
283279
pub fn dealloc(&mut self, offset: u32) -> Result<u32> {
284280
if self.class_size == 0
285281
|| offset >= self.n_slots * self.class_size
286-
|| offset % self.class_size != 0
282+
|| !offset.is_multiple_of(self.class_size)
287283
{
288284
return Err(TvmError::OutOfBounds);
289285
}

crates/tvm-core/src/cache.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::residency::Residency;
1313
const CACHE_SLOTS: usize = 8;
1414
const CACHE_MASK: u16 = (CACHE_SLOTS - 1) as u16;
1515

16-
#[derive(Clone, Copy, Debug)]
16+
#[derive(Clone, Copy, Debug, Default)]
1717
struct CacheSlot {
1818
valid: bool,
1919
region_id: u16,
@@ -28,20 +28,6 @@ struct CacheSlot {
2828
data_len: u32,
2929
}
3030

31-
impl Default for CacheSlot {
32-
fn default() -> Self {
33-
Self {
34-
valid: false,
35-
region_id: 0,
36-
generation: 0,
37-
capacity: 0,
38-
residency_hot: false,
39-
data_ptr: 0,
40-
data_len: 0,
41-
}
42-
}
43-
}
44-
4531
#[derive(Debug, Default)]
4632
pub struct ResolveCache {
4733
slots: [CacheSlot; CACHE_SLOTS],

crates/tvm-core/src/concurrent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ impl<M: MemoryRegion + Send> ConcurrentDirectory<M> {
438438
warm.sort_by(cmp);
439439
hot.sort_by(cmp);
440440

441-
for (id, used) in warm.into_iter().chain(hot.into_iter()) {
441+
for (id, used) in warm.into_iter().chain(hot) {
442442
if current <= target {
443443
break;
444444
}

crates/tvm-core/src/directory.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl<M: MemoryRegion> RegionDirectory<M> {
587587
let entry = self.entry_mut(region_id)?;
588588
let cap = entry.meta.capacity;
589589
let used = entry.allocator.used();
590-
let offset = entry.allocator.alloc(size, align).map_err(|e| {
590+
let offset = entry.allocator.alloc(size, align).inspect_err(|_e| {
591591
crate::error::set_last_error_context(crate::error::ErrorContext {
592592
region_id: Some(region_id),
593593
len: Some(size),
@@ -596,7 +596,6 @@ impl<M: MemoryRegion> RegionDirectory<M> {
596596
..Default::default()
597597
});
598598
let _ = used;
599-
e
600599
})?;
601600
entry.meta.used = entry.allocator.used();
602601
entry.metrics.record_alloc(size as u64);

crates/tvm-core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod context {
3535

3636
std::thread_local! {
3737
static LAST_ERROR_CONTEXT: std::cell::RefCell<Option<ErrorContext>> =
38-
std::cell::RefCell::new(None);
38+
const { std::cell::RefCell::new(None) };
3939
}
4040

4141
/// Set the per-thread last-error context. Called internally by

crates/tvm-core/src/memory_region.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use crate::error::Result;
1616
/// guest-side adapters that wrap a wasm linear memory range.
1717
pub trait MemoryRegion {
1818
fn len(&self) -> u32;
19+
fn is_empty(&self) -> bool {
20+
self.len() == 0
21+
}
1922
fn read(&self, offset: u32, buf: &mut [u8]) -> Result<()>;
2023
fn write(&mut self, offset: u32, buf: &[u8]) -> Result<()>;
2124
fn snapshot(&self) -> Vec<u8>;

crates/tvm-guest-mm/benches/full_matrix.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ const HOST_MM_WAT: &str = r#"
222222
"#;
223223

224224
fn run_host_mm(size: u32, data: &[u8]) -> anyhow::Result<Vec<Duration>> {
225-
let pages = ((size as u64 + 65535) / 65536).max(1) as u32;
225+
let pages = (size as u64).div_ceil(65536).max(1) as u32;
226226
let mut config = Config::new();
227227
config.wasm_multi_memory(true);
228228
let engine = Engine::new(&config)?;
@@ -260,7 +260,7 @@ fn run_guest_mm_bulk(size: u32, data: &[u8]) -> anyhow::Result<Vec<Duration>> {
260260
"#;
261261
let p = ModuleParams {
262262
n_pools: 4,
263-
initial_pages_per_pool: ((size as u64 + 65535) / 65536).max(1) as u32,
263+
initial_pages_per_pool: (size as u64).div_ceil(65536).max(1) as u32,
264264
max_pages_per_pool: 16,
265265
user_body: user_body.to_string(),
266266
};
@@ -305,7 +305,7 @@ fn run_guest_mm_bulk_specialized(size: u32, data: &[u8]) -> anyhow::Result<Vec<D
305305
"#;
306306
let p = ModuleParams {
307307
n_pools: 4,
308-
initial_pages_per_pool: ((size as u64 + 65535) / 65536).max(1) as u32,
308+
initial_pages_per_pool: (size as u64).div_ceil(65536).max(1) as u32,
309309
max_pages_per_pool: 16,
310310
user_body: user_body.to_string(),
311311
};
@@ -335,7 +335,7 @@ fn run_guest_mm_popcount_simd(size: u32, data: &[u8]) -> anyhow::Result<Vec<Dura
335335
"#;
336336
let p = ModuleParams {
337337
n_pools: 4,
338-
initial_pages_per_pool: ((size as u64 + 65535) / 65536).max(1) as u32,
338+
initial_pages_per_pool: (size as u64).div_ceil(65536).max(1) as u32,
339339
max_pages_per_pool: 16,
340340
user_body: user_body.to_string(),
341341
};
@@ -375,7 +375,7 @@ fn run_guest_mm_popcount_scalar(size: u32, data: &[u8]) -> anyhow::Result<Vec<Du
375375
"#;
376376
let p = ModuleParams {
377377
n_pools: 4,
378-
initial_pages_per_pool: ((size as u64 + 65535) / 65536).max(1) as u32,
378+
initial_pages_per_pool: (size as u64).div_ceil(65536).max(1) as u32,
379379
max_pages_per_pool: 16,
380380
user_body: user_body.to_string(),
381381
};
@@ -409,7 +409,7 @@ fn run_guest_mm_simd(size: u32, data: &[u8]) -> anyhow::Result<Vec<Duration>> {
409409
"#;
410410
let p = ModuleParams {
411411
n_pools: 4,
412-
initial_pages_per_pool: ((size as u64 + 65535) / 65536).max(1) as u32,
412+
initial_pages_per_pool: (size as u64).div_ceil(65536).max(1) as u32,
413413
max_pages_per_pool: 16,
414414
user_body: user_body.to_string(),
415415
};

crates/tvm-guest-mm/benches/guest_mm_dispatch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn time_loop<F: FnMut() -> anyhow::Result<()>>(mut f: F) -> anyhow::Result<Vec<D
4747
Ok(t)
4848
}
4949

50-
fn report(label: &str, size: u32, t: &mut Vec<Duration>) {
50+
fn report(label: &str, size: u32, t: &mut [Duration]) {
5151
t.sort();
5252
let mean_ns = t.iter().map(|d| d.as_nanos() as f64).sum::<f64>() / t.len() as f64;
5353
let p99 = stats::pct(t, 99.0);
@@ -111,7 +111,7 @@ fn run_guest_mm_bulk(size: u32, data: &[u8], n_pools: u32) -> anyhow::Result<Vec
111111
"#;
112112
let p = ModuleParams {
113113
n_pools,
114-
initial_pages_per_pool: ((size as u32 + 65535) / 65536).max(1),
114+
initial_pages_per_pool: size.div_ceil(65536).max(1),
115115
max_pages_per_pool: 16,
116116
user_body: user_body.to_string(),
117117
};
@@ -154,7 +154,7 @@ fn run_guest_mm(size: u32, data: &[u8], n_pools: u32) -> anyhow::Result<Vec<Dura
154154
"#;
155155
let p = ModuleParams {
156156
n_pools,
157-
initial_pages_per_pool: ((size as u32 + 65535) / 65536).max(1),
157+
initial_pages_per_pool: size.div_ceil(65536).max(1),
158158
max_pages_per_pool: 16,
159159
user_body: user_body.to_string(),
160160
};
@@ -274,7 +274,7 @@ fn run_dispatch_named(
274274
);
275275
let p = ModuleParams {
276276
n_pools,
277-
initial_pages_per_pool: ((size as u32 + 65535) / 65536).max(1),
277+
initial_pages_per_pool: size.div_ceil(65536).max(1),
278278
max_pages_per_pool: 16,
279279
user_body,
280280
};

0 commit comments

Comments
 (0)