Skip to content
Open
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
170 changes: 170 additions & 0 deletions benches/swap_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use criterion::{criterion_group, criterion_main, Criterion};
use soroban_sdk::{contract, contractimpl, symbol_short, testutils::Address as _, Address, Env, Symbol, Vec};

const ACTIVE_PATH_LEN: usize = 5;

#[contract]
pub struct SwapPathBenchContract;

#[contractimpl]
impl SwapPathBenchContract {
pub fn std_vec(env: Env, a0: Address, a1: Address, a2: Address, a3: Address, a4: Address) -> Symbol {
let path = std::vec![a0, a1, a2, a3, a4];
inspect_std_vec(&env, &path)
}

pub fn sdk_vec(env: Env, a0: Address, a1: Address, a2: Address, a3: Address, a4: Address) -> Symbol {
let path = Vec::from_array(&env, [a0, a1, a2, a3, a4]);
inspect_sdk_vec(&path)
}

pub fn fixed_array(
env: Env,
a0: Address,
a1: Address,
a2: Address,
a3: Address,
a4: Address,
) -> Symbol {
let path = [Some(a0), Some(a1), Some(a2), Some(a3), Some(a4)];
inspect_fixed_array(&env, &path)
}
}

fn inspect_std_vec(_env: &Env, path: &[Address]) -> Symbol {
if path.len() != ACTIVE_PATH_LEN {
return symbol_short!("bad");
}

let mut count = 0usize;
let mut first = None;
let mut last = None;
for hop in path {
if count == 0 {
first = Some(hop.clone());
}
last = Some(hop.clone());
count += 1;
}

if first == last {
symbol_short!("same")
} else {
symbol_short!("diff")
}
}

fn inspect_sdk_vec(path: &Vec<Address>) -> Symbol {
if path.len() as usize != ACTIVE_PATH_LEN {
return symbol_short!("bad");
}

let mut count = 0u32;
let mut first = None;
let mut last = None;
while count < path.len() {
let hop = path.get(count).unwrap();
if count == 0 {
first = Some(hop.clone());
}
last = Some(hop);
count += 1;
}

if first == last {
symbol_short!("same")
} else {
symbol_short!("diff")
}
}

fn inspect_fixed_array(env: &Env, path: &[Option<Address>; ACTIVE_PATH_LEN]) -> Symbol {
let mut count = 0usize;
let mut first = None;
let mut last = None;
for hop in path {
match hop {
Some(address) => {
if count == 0 {
first = Some(address.clone());
}
last = Some(address.clone());
count += 1;
}
None => break,
}
}

if count != ACTIVE_PATH_LEN {
return symbol_short!("bad");
}

if first == last {
symbol_short!("same")
} else {
let _ = env;
symbol_short!("diff")
}
}

fn setup() -> (Env, SwapPathBenchContractClient<'static>, [Address; ACTIVE_PATH_LEN]) {
let env = Env::default();
env.cost_estimate().budget().reset_unlimited();

let contract_id = env.register(SwapPathBenchContract, ());
let client = SwapPathBenchContractClient::new(&env, &contract_id);
let addrs = [
Address::generate(&env),
Address::generate(&env),
Address::generate(&env),
Address::generate(&env),
Address::generate(&env),
];

(env, client, addrs)
}

fn benchmark_cpu_costs() {
let cases: [(&str, fn(&SwapPathBenchContractClient<'_>, &[Address; ACTIVE_PATH_LEN]) -> Symbol); 3] = [
("std::vec::Vec<Address>", |client, addrs| {
client.std_vec(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4])
}),
("soroban_sdk::Vec<Address>", |client, addrs| {
client.sdk_vec(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4])
}),
("[Option<Address>; 5]", |client, addrs| {
client.fixed_array(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4])
}),
];

for (label, run_case) in cases {
let (env, client, addrs) = setup();
env.cost_estimate().budget().reset_default();
let _ = run_case(&client, &addrs);
let cpu = env.cost_estimate().budget().cpu_instruction_cost();
let mem = env.cost_estimate().budget().memory_bytes_cost();
eprintln!("[bench] {label:<28} cpu={cpu} mem={mem}");
}
}

fn bench_swap_path(c: &mut Criterion) {
let (env, client, addrs) = setup();
let mut group = c.benchmark_group("swap_path");

group.bench_function("std_vec", |b| {
b.iter(|| client.std_vec(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4]))
});
group.bench_function("sdk_vec", |b| {
b.iter(|| client.sdk_vec(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4]))
});
group.bench_function("fixed_array", |b| {
b.iter(|| client.fixed_array(&addrs[0], &addrs[1], &addrs[2], &addrs[3], &addrs[4]))
});

let _ = env;
benchmark_cpu_costs();
group.finish();
}

criterion_group!(benches, bench_swap_path);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions contracts/adl_handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod tests {
use deposit_vault::{DepositVault, DepositVaultClient as DVClient};
use gmx_keys::roles;
use gmx_math::FLOAT_PRECISION;
use gmx_types::{CreateDepositParams, CreateOrderParams, OrderType, TokenPrice};
use gmx_types::{CreateDepositParams, CreateOrderParams, OrderType, SwapPath, TokenPrice};
use market_token::{MarketToken, MarketTokenClient as MtClient};
use oracle::{Oracle, OracleClient as OClient};
use order_handler::{OrderHandler, OrderHandlerClient as OHClient};
Expand Down Expand Up @@ -504,7 +504,7 @@ mod tests {
receiver: trader.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.long_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: SwapPath::new(),
size_delta_usd: size_usd,
collateral_delta_amount: collateral,
trigger_price: 0,
Expand Down
6 changes: 3 additions & 3 deletions contracts/exchange_router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ mod tests {
receiver: user.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.long_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: gmx_types::SwapPath::new(),
size_delta_usd: size_usd,
collateral_delta_amount: collateral_tokens,
trigger_price: 0,
Expand Down Expand Up @@ -1122,7 +1122,7 @@ mod tests {
receiver: trader.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.long_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: gmx_types::SwapPath::new(),
size_delta_usd: position.size_in_usd,
collateral_delta_amount: 0, // ignored by decrease_position logic
trigger_price: 0,
Expand Down Expand Up @@ -1436,7 +1436,7 @@ mod tests {
receiver: trader.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.long_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: gmx_types::SwapPath::new(),
size_delta_usd: 4_000 * fp,
collateral_delta_amount: collateral,
trigger_price: 0,
Expand Down
6 changes: 3 additions & 3 deletions contracts/liquidation_handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod tests {
use data_store::{DataStore, DataStoreClient as DsClient};
use gmx_keys::roles;
use gmx_math::FLOAT_PRECISION;
use gmx_types::{CreateOrderParams, OrderType, TokenPrice};
use gmx_types::{CreateOrderParams, OrderType, SwapPath, TokenPrice};
use market_token::{MarketToken, MarketTokenClient as MtClient};
use oracle::{Oracle, OracleClient as OClient};
use order_handler::{OrderHandler, OrderHandlerClient as OHClient};
Expand Down Expand Up @@ -501,7 +501,7 @@ mod tests {
receiver: w.user.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.long_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: SwapPath::new(),
size_delta_usd: size_usd,
collateral_delta_amount: collateral_tokens,
trigger_price: 0,
Expand Down Expand Up @@ -533,7 +533,7 @@ mod tests {
receiver: w.user.clone(),
market: w.market_tk.clone(),
initial_collateral_token: w.short_tk.clone(),
swap_path: soroban_sdk::Vec::new(&w.env),
swap_path: SwapPath::new(),
size_delta_usd: size_usd,
collateral_delta_amount: collateral_tokens,
trigger_price: 0,
Expand Down
5 changes: 5 additions & 0 deletions contracts/order_handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "order_execution"
harness = false

[[bench]]
name = "swap_path"
path = "../../benches/swap_path.rs"
harness = false
Loading