Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bad fibonacci #25

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "btreemap_vs_hashmap"
path = "btreemap_vs_hashmap/src/main.rs"

[dependencies]
canbench = { path = "../canbench-rs", optional = true }
canbench = { path = "../canbench-rs" }
candid.workspace = true
ic-cdk.workspace= true
ic-cdk-macros.workspace = true
2 changes: 1 addition & 1 deletion examples/btreemap_vs_hashmap/canbench.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build_cmd:
cargo build --release --target wasm32-unknown-unknown --features canbench
cargo build --release --target wasm32-unknown-unknown

wasm_path:
../../target/wasm32-unknown-unknown/release/btreemap_vs_hashmap.wasm
70 changes: 15 additions & 55 deletions examples/btreemap_vs_hashmap/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use canbench::bench;
use candid::{CandidType, Encode};
use ic_cdk_macros::pre_upgrade;
use std::cell::RefCell;
Expand All @@ -9,8 +10,6 @@ struct User {

#[derive(Default, CandidType)]
struct State {
// TIP: try replacing the `BTreeMap` below with a `HashMap` and run `canbench`.
// Notice how the performance changes.
users: std::collections::BTreeMap<u64, User>,
}

Expand All @@ -21,66 +20,27 @@ thread_local! {
#[pre_upgrade]
fn pre_upgrade() {
// Serialize state.
let bytes = {
#[cfg(feature = "canbench")]
let _p = canbench::bench_scope("serialize_state");
STATE.with(|s| Encode!(s).unwrap())
};
let bytes = STATE.with(|s| Encode!(s).unwrap());

// Write to stable memory.
#[cfg(feature = "canbench")]
let _p = canbench::bench_scope("writing_to_stable_memory");
ic_cdk::api::stable::StableWriter::default()
.write(&bytes)
.unwrap();
}

#[cfg(feature = "canbench")]
mod benches {
use super::*;
use canbench::bench;

// Benchmarks inserting 1 million users into the state.
#[bench]
fn insert_users() {
STATE.with(|s| {
let mut s = s.borrow_mut();
for i in 0..1_000_000 {
s.users.insert(
i,
User {
name: "foo".to_string(),
},
);
}
});
}

// Benchmarks removing 1 million users from the state.
#[bench(raw)]
fn remove_users() -> canbench::BenchResult {
insert_users();

// Only benchmark removing users. Inserting users isn't
// included in the results of our benchmark.
canbench::bench_fn(|| {
STATE.with(|s| {
let mut s = s.borrow_mut();
for i in 0..1_000_000 {
s.users.remove(&i);
}
})
})
}

#[bench(raw)]
fn pre_upgrade_bench() -> canbench::BenchResult {
insert_users();

// Only benchmark the pre_upgrade. Inserting users isn't
// included in the results of our benchmark.
canbench::bench_fn(pre_upgrade)
}
#[bench]
fn insert_users() {
STATE.with(|s| {
let mut s = s.borrow_mut();
for i in 0..1_000_000 {
s.users.insert(
i,
User {
name: "foo".to_string(),
},
);
}
});
}

fn main() {}
2 changes: 1 addition & 1 deletion examples/fibonacci/canbench.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build_cmd:
cargo build --release --target wasm32-unknown-unknown --features canbench
cargo build --release --target wasm32-unknown-unknown

wasm_path:
../../target/wasm32-unknown-unknown/release/fibonacci.wasm
49 changes: 8 additions & 41 deletions examples/fibonacci/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,20 @@
// A version of fibonacci that's efficient.
#[ic_cdk::query]
fn fibonacci(n: u32) -> u32 {
if n == 0 {
return 0;
} else if n == 1 {
return 1;
}

let mut a = 0;
let mut b = 1;
let mut result = 0;

for _ in 2..=n {
result = a + b;
a = b;
b = result;
}

result
}

// Try this inefficient version instead and run `canbench`.
// `canbench` will detect and report the regression.
/*
#[ic_cdk::query]
fn fibonacci(n: u32) -> u32 {
match n {
0 => 1,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}*/

#[cfg(feature = "canbench")]
mod benches {
use super::*;
use canbench::bench;
}

#[bench]
fn fibonacci_20() {
// NOTE: the result is printed to prevent the compiler from optimizing the call away.
println!("{:?}", fibonacci(20));
}
#[canbench::bench]
fn fibonacci_20() {
println!("{:?}", fibonacci(20));
}

#[bench]
fn fibonacci_45() {
// NOTE: the result is printed to prevent the compiler from optimizing the call away.
println!("{:?}", fibonacci(45));
}
#[canbench::bench]
fn fibonacci_45() {
println!("{:?}", fibonacci(45));
}

fn main() {}
2 changes: 1 addition & 1 deletion scripts/ci_run_benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fi
# Detect if canbench results file is up to date.
pushd "$CANISTER_PATH"
canbench --less-verbose > $CANBENCH_OUTPUT
if grep -q "(regress \|(improved by \|(new)" "$CANBENCH_OUTPUT"; then
if grep -q "(regress\|(improved by \|(new)" "$CANBENCH_OUTPUT"; then
UPDATED_MSG="**\`$CANBENCH_RESULTS_FILE\` is not up to date ❌**
If the performance change is expected, run \`canbench --persist\` to save the updated benchmark results.";

Expand Down
Loading