Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25
components: rustfmt

- name: Check code format
Expand All @@ -30,7 +30,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25
components: clippy

- name: Rust Cache
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2
Expand All @@ -67,7 +67,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: Deploy
on:
push:
branches:
- '**'
- "**"
tags-ignore:
- 'v*'
- 'v*-pre.*'
- "v*"
- "v*-pre.*"
pull_request:

permissions:
Expand All @@ -15,7 +15,7 @@ permissions:
id-token: write

concurrency:
group: 'pages'
group: "pages"
cancel-in-progress: false

jobs:
Expand All @@ -34,16 +34,16 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Rust 1.93.0
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.93.0"
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2

- name: Build benchmarks
run: cargo +1.93.0 bench --features bench --no-run
run: cargo bench --no-run

build-doc:
name: Build documentation
Expand All @@ -56,7 +56,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: Release
on:
push:
tags:
- 'v*.*.*'
- 'v*.*.*-pre.*'
- "v*.*.*"
- "v*.*.*-pre.*"

permissions:
contents: write
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2025-04-01
toolchain: nightly-2026-02-25

- name: Rust Cache
uses: Swatinem/rust-cache@v2
Expand Down
20 changes: 8 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@ categories = ["memory-management", "no-std", "embedded"]
default = []
log = ["dep:log"]
tracking = []
# Enable benchmarking (requires criterion and rand as regular dependencies)
bench = ["dep:criterion", "dep:rand"]

# Deprecated
bench = []

[dependencies]
log = { version = "0.4", optional = true }
cfg-if = "1.0"
criterion = { version = "0.4", features = ["html_reports"], optional = true }
rand = { version = "0.8", features = ["small_rng"], optional = true }
log = { version = "0.4", optional = true }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
rand = { version = "0.8", features = ["small_rng"] }

[[bench]]
name = "global_allocator"
harness = false
required-features = ["bench"]

[[bench]]
name = "buddy_allocator"
harness = false
required-features = ["bench"]

[[bench]]
name = "slab_allocator"
harness = false
required-features = ["bench"]

[[bench]]
name = "stability"
harness = false
required-features = ["bench"]



2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ This project includes comprehensive benchmarks to evaluate performance and stabi

```bash
# Run all benchmarks
cargo bench --features bench
cargo bench
```

For detailed usage instructions, refer to `benches/README.md`.
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ cargo test --package buddy-slab-allocator --features tracking

```bash
# 运行所有 benchmark
cargo bench --features bench
cargo bench
```

详细使用方法请参考 `benches/README_CN.md`。
Expand Down
59 changes: 13 additions & 46 deletions benches/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,42 @@

本目录包含 buddy-slab-allocator 项目的性能测试和稳定性测试。

## 前置条件

运行 benchmark 需要启用 `bench` feature,该 feature 会添加 `criterion` 和 `rand` 依赖。

**重要提示**:由于 benchmark 依赖的 `zerocopy` 库需要较新的 Rust 特性,请使用 **Rust 1.93.0 或更高版本**运行 benchmark:

```bash
# 安装 Rust 1.93.0
rustup install 1.93.0

# 使用指定版本运行 benchmark
cargo +1.93.0 bench --features bench
```

**注意**:主项目使用 `nightly-2025-04-01` 工具链进行开发和 CI,但 benchmark 需要使用 1.93.0+ 以避免依赖兼容性问题。

## 运行 Benchmark

**推荐使用 Rust 1.93.0+**:

```bash
# 一次性使用(推荐)
cargo +1.93.0 bench --features bench
```

### 运行所有 benchmark

```bash
cargo +1.93.0 bench --features bench
cargo bench
```

### 运行特定 benchmark suite

```bash
# 测试全局分配器
cargo +1.93.0 bench --features bench --bench global_allocator
cargo bench --bench global_allocator

# 测试 Buddy 页分配器
cargo +1.93.0 bench --features bench --bench buddy_allocator
cargo bench --bench buddy_allocator

# 测试 Slab 字节分配器
cargo +1.93.0 bench --features bench --bench slab_allocator
cargo bench --bench slab_allocator

# 测试稳定性
cargo +1.93.0 bench --features bench --bench stability
cargo bench --bench stability
```

### 高级选项

```bash
# 启用 memory tracking 功能运行 benchmark
cargo +1.93.0 bench --features "bench tracking"
cargo bench --features tracking

# 保存基线用于后续对比
cargo +1.93.0 bench --features bench -- --save-baseline main
cargo bench -- --save-baseline main

# 与基线对比
cargo +1.93.0 bench --features bench -- --baseline main
cargo bench -- --baseline main

# 只运行特定 benchmark
cargo +1.93.0 bench --features bench global_alloc_small
cargo bench global_alloc_small
```

## Benchmark Suite 说明
Expand Down Expand Up @@ -264,18 +239,10 @@ criterion_main!(benches);

### Q: Benchmark 编译失败怎么办?

A: 最常见的原因是 Rust 版本不兼容。请确保使用 **Rust 1.93.0 或更高版本**:

```bash
# 安装 Rust 1.93.0
rustup install 1.93.0

# 使用指定版本
cargo +1.93.0 bench --features bench
```
A: 最常见的原因是 Rust 版本不兼容。请确保使用 **Rust 1.93.0 或更高版本**.

如果仍然失败:
1. 清理并重新构建:`cargo clean && cargo +1.93.0 build --features bench`
1. 清理并重新构建:`cargo clean && cargo build`
2. 检查网络连接和依赖下载
3. 更新 rustup:`rustup update`

Expand All @@ -291,10 +258,10 @@ A: 可以:
A: 使用 Criterion 的基线功能:
```bash
# 第一次运行,保存基线
cargo +1.93.0 bench --features bench -- --save-baseline v1
cargo bench -- --save-baseline v1

# 修改代码后,对比基线
cargo +1.93.0 bench --features bench -- --baseline v1
cargo bench -- --baseline v1
```

### Q: Benchmark 结果不稳定怎么办?
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2025-04-01"
channel = "nightly-2026-02-25"
components = ["rust-src", "rustfmt", "clippy"]
6 changes: 3 additions & 3 deletions src/buddy/buddy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<const PAGE_SIZE: usize> BuddySet<PAGE_SIZE> {
let mut max_order_by_alignment = 0;
for test_order in 0..=self.max_order() {
let block_size = (1 << test_order) * PAGE_SIZE;
if current_addr % block_size == 0 {
if current_addr.is_multiple_of(block_size) {
max_order_by_alignment = test_order;
} else {
break;
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<const PAGE_SIZE: usize> BuddySet<PAGE_SIZE> {

// Verify alignment requirement
assert!(
block.addr % alignment == 0,
block.addr.is_multiple_of(alignment),
"Allocated address {:#x} is not aligned to {:#x} bytes ",
block.addr,
alignment
Expand Down Expand Up @@ -470,7 +470,7 @@ impl<const PAGE_SIZE: usize> BuddySet<PAGE_SIZE> {
}

// Check alignment requirement
if base % alignment != 0 {
if !base.is_multiple_of(alignment) {
error!(
"zone {}: Address {:#x} is not aligned to {:#x}",
self.zone_id, base, alignment
Expand Down
2 changes: 1 addition & 1 deletion src/slab/slab_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl SlabNode {
}

let offset = obj_addr - base;
if offset % self.size_class.size() != 0 {
if !offset.is_multiple_of(self.size_class.size()) {
error!("Invalid object address: {obj_addr:x}");
return None;
}
Expand Down