Skip to content

Commit

Permalink
Update benchmarking and collecting for scwq, swwq and srwq opco…
Browse files Browse the repository at this point in the history
…des.
  • Loading branch information
xgreenx committed Oct 17, 2023
1 parent ff7cf60 commit c6b2de5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 28 deletions.
72 changes: 51 additions & 21 deletions benches/benches/set/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,54 +97,80 @@ pub fn run(c: &mut Criterion) {
run_group_ref(&mut c.benchmark_group("srw"), "srw", input);
}

{
let mut key = Bytes32::zeroed();
let mut scwq = c.benchmark_group("scwq");

key.as_mut()[..8].copy_from_slice(&1u64.to_be_bytes());
let data = key.iter().copied().collect::<Vec<_>>();
for i in linear.clone() {
let start_key = Bytes32::zeroed();
let data = start_key.iter().copied().collect::<Vec<_>>();

let post_call = vec![
op::gtf_args(0x10, 0x00, GTFArgs::ScriptData),
op::addi(0x11, 0x10, ContractId::LEN.try_into().unwrap()),
op::addi(0x11, 0x11, WORD_SIZE.try_into().unwrap()),
op::addi(0x11, 0x11, WORD_SIZE.try_into().unwrap()),
op::movi(0x12, i as u32),
];
let mut bench = VmBench::contract(rng, op::scwq(0x11, 0x29, RegId::ONE))
let mut bench = VmBench::contract(rng, op::scwq(0x11, 0x29, 0x12))
.expect("failed to prepare contract")
.with_post_call(post_call)
.with_prepare_db(move |mut db| {
db.merkle_contract_state_insert(&contract, &key, &key)?;
let slots = (0u64..i).map(|key_number| {
let mut key = Bytes32::zeroed();
key.as_mut()[..8].copy_from_slice(&key_number.to_be_bytes());
(key, key)
});
db.database_mut()
.init_contract_state(&contract, slots)
.unwrap();

Ok(db)
});
bench.data.extend(data);
run_group_ref(&mut c.benchmark_group("scwq"), "scwq", bench);

scwq.throughput(Throughput::Bytes(i));

run_group_ref(&mut scwq, format!("{i}"), bench);
}

{
let mut key = Bytes32::zeroed();
scwq.finish();

key.as_mut()[..8].copy_from_slice(&1u64.to_be_bytes());
let data = key.iter().copied().collect::<Vec<_>>();
let mut swwq = c.benchmark_group("swwq");

for i in linear.clone() {
let start_key = Bytes32::zeroed();
let data = start_key.iter().copied().collect::<Vec<_>>();

let post_call = vec![
op::gtf_args(0x10, 0x00, GTFArgs::ScriptData),
op::addi(0x11, 0x10, ContractId::LEN.try_into().unwrap()),
op::addi(0x11, 0x11, WORD_SIZE.try_into().unwrap()),
op::addi(0x11, 0x11, WORD_SIZE.try_into().unwrap()),
op::movi(0x12, i as u32),
];
let mut bench = VmBench::contract(rng, op::swwq(0x10, 0x11, 0x20, RegId::ONE))
let mut bench = VmBench::contract(rng, op::swwq(0x10, 0x11, 0x20, 0x12))
.expect("failed to prepare contract")
.with_post_call(post_call)
.with_prepare_db(move |mut db| {
db.merkle_contract_state_insert(&contract, &key, &key)?;
let slots = (0u64..i).map(|key_number| {
let mut key = Bytes32::zeroed();
key.as_mut()[..8].copy_from_slice(&key_number.to_be_bytes());
(key, key)
});
db.database_mut()
.init_contract_state(&contract, slots)
.unwrap();

Ok(db)
});
bench.data.extend(data);
run_group_ref(&mut c.benchmark_group("swwq"), "swwq", bench);

swwq.throughput(Throughput::Bytes(i));

run_group_ref(&mut swwq, format!("{i}"), bench);
}

swwq.finish();

let mut call = c.benchmark_group("call");

for i in linear.clone() {
Expand Down Expand Up @@ -477,13 +503,11 @@ pub fn run(c: &mut Criterion) {
let mut srwq = c.benchmark_group("srwq");

for i in linear.clone() {
let mut key = Bytes32::zeroed();

key.as_mut()[..8].copy_from_slice(&i.to_be_bytes());
let data = key.iter().copied().collect::<Vec<_>>();
let start_key = Bytes32::zeroed();
let data = start_key.iter().copied().collect::<Vec<_>>();

let post_call = vec![
op::movi(0x16, i.try_into().unwrap()),
op::movi(0x16, i as u32),
op::movi(0x17, 2000),
op::move_(0x15, 0x16),
op::muli(0x15, 0x15, 32),
Expand All @@ -495,8 +519,14 @@ pub fn run(c: &mut Criterion) {
.expect("failed to prepare contract")
.with_post_call(post_call)
.with_prepare_db(move |mut db| {
let values = vec![key; i as usize];
db.merkle_contract_state_insert_range(&contract, &key, &values)?;
let slots = (0u64..i).map(|key_number| {
let mut key = Bytes32::zeroed();
key.as_mut()[..8].copy_from_slice(&key_number.to_be_bytes());
(key, key)
});
db.database_mut()
.init_contract_state(&contract, slots)
.unwrap();

Ok(db)
});
Expand Down
23 changes: 16 additions & 7 deletions benches/src/bin/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl State {
time: *y,
})
.collect();
let (_, dep_per_unit) = dependent_cost(x_y);
let (_, dep_per_unit) = dependent_cost(&name, x_y);
(
name,
Cost::DependentAll {
Expand All @@ -544,7 +544,7 @@ impl State {
let iter = dependant_groups.into_iter().map(|(name, x_y)| {
groups.remove(&name);

let (base, dep_per_unit) = dependent_cost(x_y);
let (base, dep_per_unit) = dependent_cost(&name, x_y);
(name, Cost::Dependent { base, dep_per_unit })
});
costs.0.extend(iter);
Expand Down Expand Up @@ -618,7 +618,7 @@ fn linear_regression(x_y: Vec<(u64, u64)>) -> f64 {
sq_x / sum_x_y
}

fn dependent_cost(x_y: Vec<(u64, u64)>) -> (u64, u64) {
fn dependent_cost(name: &String, x_y: Vec<(u64, u64)>) -> (u64, u64) {
const NEAR_LINEAR: f64 = 0.1;

enum Type {
Expand All @@ -633,7 +633,7 @@ fn dependent_cost(x_y: Vec<(u64, u64)>) -> (u64, u64) {
Exp,
}

#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
struct Point {
/// Number of elements for the opcode.
x: u64,
Expand All @@ -659,7 +659,7 @@ fn dependent_cost(x_y: Vec<(u64, u64)>) -> (u64, u64) {

let linear_regression = linear_regression(x_y.clone());

let x_y = x_y
let mut x_y = x_y
.into_iter()
.map(|(x, y)| Point { x, y })
.collect::<Vec<_>>();
Expand Down Expand Up @@ -695,6 +695,7 @@ fn dependent_cost(x_y: Vec<(u64, u64)>) -> (u64, u64) {
Type::Logarithm => {
// The logarithm function slows down fast, and the point where it becomes more
// linear is the base point. After this point we use linear strategy.
let last = x_y.last().unwrap().amount();
let mut x_y = x_y.into_iter();

let mut base = x_y.next().unwrap();
Expand All @@ -710,11 +711,19 @@ fn dependent_cost(x_y: Vec<(u64, u64)>) -> (u64, u64) {
})
.map(|p| p.amount())
.min_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
.unwrap_or(last);
(base.y, amount as u64)
}
Type::Exp => {
panic!("We don't support exponential chart")
println!(
"The {} is exponential. We don't support exponential chart. \
The opcode should be limited with upper bound. \n {:?}",
name, x_y
);

x_y.sort_unstable_by_key(|a| a.amount() as u64);
let base = x_y.last().unwrap();
(base.y, 0)
}
}
}
Expand Down

0 comments on commit c6b2de5

Please sign in to comment.