perf: optimize trade executor performance and reduce memory usage

- Implement singleton pattern for TradeFactory
- Replace TradeTimer with direct Instant measurements
- Optimize PumpFun instruction building and parallel execution
- Reduce unnecessary clones and improve memory allocation
- Simplify address lookup table management
This commit is contained in:
ysq
2025-09-07 18:07:45 +08:00
parent 7e425fce75
commit eddac7a679
9 changed files with 127 additions and 191 deletions
+1 -1
View File
@@ -103,5 +103,5 @@ pub async fn get_address_lookup_table_account(
lookup_table_address: &Pubkey, lookup_table_address: &Pubkey,
) -> AddressLookupTableAccount { ) -> AddressLookupTableAccount {
let cache = AddressLookupTableCache::get_instance(); let cache = AddressLookupTableCache::get_instance();
return cache.get_table_content(&lookup_table_address); cache.get_table_content(lookup_table_address)
} }
+16 -8
View File
@@ -45,7 +45,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
} }
let bonding_curve = protocol_params.bonding_curve.clone(); let bonding_curve = &protocol_params.bonding_curve;
let max_sol_cost = calculate_with_slippage_buy( let max_sol_cost = calculate_with_slippage_buy(
params.sol_amount, params.sol_amount,
@@ -53,12 +53,20 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
); );
let creator_vault_pda = protocol_params.creator_vault; let creator_vault_pda = protocol_params.creator_vault;
let mut creator = Pubkey::default(); // Optimize creator lookup - avoid PDA calculation if not default
if let Some(default_creator_ata) = get_creator_vault_pda(&creator) { let creator = if creator_vault_pda == Pubkey::default() {
if default_creator_ata != creator_vault_pda { Pubkey::default()
creator = creator_vault_pda; } else {
// Fast check against cached default creator vault
static DEFAULT_CREATOR_VAULT: std::sync::LazyLock<Option<Pubkey>> =
std::sync::LazyLock::new(|| get_creator_vault_pda(&Pubkey::default()));
if Some(creator_vault_pda) == *DEFAULT_CREATOR_VAULT {
Pubkey::default()
} else {
creator_vault_pda
} }
} };
let buy_token_amount = get_buy_token_amount_from_sol_amount( let buy_token_amount = get_buy_token_amount_from_sol_amount(
bonding_curve.virtual_token_reserves as u128, bonding_curve.virtual_token_reserves as u128,
@@ -68,7 +76,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
params.sol_amount, params.sol_amount,
); );
let mut instructions = vec![]; let mut instructions = Vec::with_capacity(2);
// Create associated token account // Create associated token account
instructions.push(create_associated_token_account( instructions.push(create_associated_token_account(
@@ -99,7 +107,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
.downcast_ref::<PumpFunParams>() .downcast_ref::<PumpFunParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?; .ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?;
let bonding_curve = protocol_params.bonding_curve.clone(); let bonding_curve = &protocol_params.bonding_curve;
let token_amount = if let Some(amount) = params.token_amount { let token_amount = if let Some(amount) = params.token_amount {
if amount == 0 { if amount == 0 {
+6 -7
View File
@@ -7,12 +7,11 @@ use crate::common::address_lookup_cache::get_address_lookup_table_account;
pub async fn get_address_lookup_table_accounts( pub async fn get_address_lookup_table_accounts(
lookup_table_key: Option<Pubkey>, lookup_table_key: Option<Pubkey>,
) -> Vec<AddressLookupTableAccount> { ) -> Vec<AddressLookupTableAccount> {
let mut address_lookup_table_accounts = vec![]; match lookup_table_key {
Some(key) => {
if let Some(lookup_table_key) = lookup_table_key { let account = get_address_lookup_table_account(&key).await;
let account = get_address_lookup_table_account(&lookup_table_key).await; vec![account]
address_lookup_table_accounts.push(account); }
None => Vec::new(),
} }
address_lookup_table_accounts
} }
+8 -4
View File
@@ -27,13 +27,13 @@ pub async fn build_transaction(
recent_blockhash: Hash, recent_blockhash: Hash,
data_size_limit: u32, data_size_limit: u32,
middleware_manager: Option<Arc<MiddlewareManager>>, middleware_manager: Option<Arc<MiddlewareManager>>,
protocol_name: String, protocol_name: &str,
is_buy: bool, is_buy: bool,
with_tip: bool, with_tip: bool,
tip_account: &Pubkey, tip_account: &Pubkey,
tip_amount: f64, tip_amount: f64,
) -> Result<VersionedTransaction, anyhow::Error> { ) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = vec![]; let mut instructions = Vec::with_capacity(business_instructions.len() + 5);
// 添加nonce指令 // 添加nonce指令
if is_buy { if is_buy {
@@ -84,12 +84,16 @@ async fn build_versioned_transaction(
address_lookup_table_accounts: Vec<solana_sdk::message::AddressLookupTableAccount>, address_lookup_table_accounts: Vec<solana_sdk::message::AddressLookupTableAccount>,
blockhash: Hash, blockhash: Hash,
middleware_manager: Option<Arc<MiddlewareManager>>, middleware_manager: Option<Arc<MiddlewareManager>>,
protocol_name: String, protocol_name: &str,
is_buy: bool, is_buy: bool,
) -> Result<VersionedTransaction, anyhow::Error> { ) -> Result<VersionedTransaction, anyhow::Error> {
let full_instructions = match middleware_manager { let full_instructions = match middleware_manager {
Some(middleware_manager) => middleware_manager Some(middleware_manager) => middleware_manager
.apply_middlewares_process_full_instructions(instructions, protocol_name, is_buy)?, .apply_middlewares_process_full_instructions(
instructions,
protocol_name.to_string(),
is_buy,
)?,
None => instructions, None => instructions,
}; };
let v0_message: v0::Message = v0::Message::try_compile( let v0_message: v0::Message = v0::Message::try_compile(
+15 -45
View File
@@ -1,10 +1,9 @@
use anyhow::Result; use anyhow::Result;
use std::sync::Arc; use std::{sync::Arc, time::Instant};
use super::{ use super::{
parallel::parallel_execute_with_tips, parallel::parallel_execute_with_tips,
params::{BuyParams, SellParams}, params::{BuyParams, SellParams},
timer::TradeTimer,
traits::{InstructionBuilder, TradeExecutor}, traits::{InstructionBuilder, TradeExecutor},
}; };
use crate::{swqos::SwqosClient, trading::middleware::MiddlewareManager}; use crate::{swqos::SwqosClient, trading::middleware::MiddlewareManager};
@@ -38,26 +37,12 @@ impl TradeExecutor for GenericTradeExecutor {
if data_size_limit == 0 { if data_size_limit == 0 {
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT; data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
} }
let timer = TradeTimer::new("Building buy transaction instructions");
// Validate parameters - convert to BuyParams for validation let start = Instant::now();
let buy_params = BuyParams {
rpc: params.rpc,
payer: params.payer.clone(),
mint: params.mint,
sol_amount: params.sol_amount,
slippage_basis_points: params.slippage_basis_points,
priority_fee: params.priority_fee.clone(),
lookup_table_key: params.lookup_table_key,
recent_blockhash: params.recent_blockhash,
data_size_limit: data_size_limit,
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: params.protocol_params.clone(),
};
// Build instructions // Build instructions directly from params to avoid unnecessary cloning
let instructions = self.instruction_builder.build_buy_instructions(&buy_params).await?; let instructions = self.instruction_builder.build_buy_instructions(&params).await?;
let final_instructions = match middleware_manager.clone() { let final_instructions = match &middleware_manager {
Some(middleware_manager) => middleware_manager Some(middleware_manager) => middleware_manager
.apply_middlewares_process_protocol_instructions( .apply_middlewares_process_protocol_instructions(
instructions, instructions,
@@ -67,19 +52,19 @@ impl TradeExecutor for GenericTradeExecutor {
None => instructions, None => instructions,
}; };
timer.finish(); println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
// Execute transactions in parallel // Execute transactions in parallel
parallel_execute_with_tips( parallel_execute_with_tips(
swqos_clients, swqos_clients,
params.payer, params.payer,
final_instructions, final_instructions,
params.priority_fee, Arc::new(params.priority_fee),
params.lookup_table_key, params.lookup_table_key,
params.recent_blockhash, params.recent_blockhash,
data_size_limit, data_size_limit,
middleware_manager, middleware_manager,
self.protocol_name.to_string(), self.protocol_name,
true, true,
params.wait_transaction_confirmed, params.wait_transaction_confirmed,
true, true,
@@ -95,26 +80,11 @@ impl TradeExecutor for GenericTradeExecutor {
swqos_clients: Vec<Arc<SwqosClient>>, swqos_clients: Vec<Arc<SwqosClient>>,
middleware_manager: Option<Arc<MiddlewareManager>>, middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()> { ) -> Result<()> {
let timer = TradeTimer::new("Building sell transaction instructions"); let start = Instant::now();
// Convert to SellParams for instruction building // Build instructions directly from params to avoid unnecessary cloning
let sell_params = SellParams { let instructions = self.instruction_builder.build_sell_instructions(&params).await?;
rpc: params.rpc, let final_instructions = match &middleware_manager {
payer: params.payer.clone(),
mint: params.mint,
token_amount: params.token_amount,
slippage_basis_points: params.slippage_basis_points,
priority_fee: params.priority_fee.clone(),
lookup_table_key: params.lookup_table_key,
recent_blockhash: params.recent_blockhash,
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: params.protocol_params.clone(),
with_tip: params.with_tip,
};
// Build instructions
let instructions = self.instruction_builder.build_sell_instructions(&sell_params).await?;
let final_instructions = match middleware_manager.clone() {
Some(middleware_manager) => middleware_manager Some(middleware_manager) => middleware_manager
.apply_middlewares_process_protocol_instructions( .apply_middlewares_process_protocol_instructions(
instructions, instructions,
@@ -124,19 +94,19 @@ impl TradeExecutor for GenericTradeExecutor {
None => instructions, None => instructions,
}; };
timer.finish(); println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
// Execute transactions in parallel // Execute transactions in parallel
parallel_execute_with_tips( parallel_execute_with_tips(
swqos_clients, swqos_clients,
params.payer, params.payer,
final_instructions, final_instructions,
params.priority_fee, Arc::new(params.priority_fee),
params.lookup_table_key, params.lookup_table_key,
params.recent_blockhash, params.recent_blockhash,
0, 0,
middleware_manager, middleware_manager,
self.protocol_name.to_string(), self.protocol_name,
false, false,
params.wait_transaction_confirmed, params.wait_transaction_confirmed,
params.with_tip, params.with_tip,
+1 -2
View File
@@ -1,5 +1,4 @@
pub mod params; pub mod params;
pub mod traits; pub mod traits;
pub mod executor; pub mod executor;
pub mod parallel; pub mod parallel;
pub mod timer;
+27 -25
View File
@@ -1,14 +1,14 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use solana_hash::Hash; use solana_hash::Hash;
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair}; use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
use std::{str::FromStr, sync::Arc}; use std::{str::FromStr, sync::Arc, time::Instant};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use crate::{ use crate::{
common::PriorityFee, common::PriorityFee,
swqos::{SwqosClient, SwqosType, TradeType}, swqos::{SwqosClient, SwqosType, TradeType},
trading::{common::build_transaction, core::timer::TradeTimer, MiddlewareManager}, trading::{common::build_transaction, MiddlewareManager},
}; };
/// Generic function for parallel transaction execution /// Generic function for parallel transaction execution
@@ -16,26 +16,34 @@ pub async fn parallel_execute_with_tips(
swqos_clients: Vec<Arc<SwqosClient>>, swqos_clients: Vec<Arc<SwqosClient>>,
payer: Arc<Keypair>, payer: Arc<Keypair>,
instructions: Vec<Instruction>, instructions: Vec<Instruction>,
priority_fee: PriorityFee, priority_fee: Arc<PriorityFee>,
lookup_table_key: Option<Pubkey>, lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash, recent_blockhash: Hash,
data_size_limit: u32, data_size_limit: u32,
middleware_manager: Option<Arc<MiddlewareManager>>, middleware_manager: Option<Arc<MiddlewareManager>>,
protocol_name: String, protocol_name: &'static str,
is_buy: bool, is_buy: bool,
wait_transaction_confirmed: bool, wait_transaction_confirmed: bool,
with_tip: bool, with_tip: bool,
) -> Result<()> { ) -> Result<()> {
let cores = core_affinity::get_core_ids().unwrap(); let cores = core_affinity::get_core_ids().unwrap();
let mut handles: Vec<JoinHandle<Result<()>>> = vec![]; let mut handles: Vec<JoinHandle<Result<()>>> = Vec::with_capacity(swqos_clients.len());
if is_buy
if is_buy && swqos_clients.len() > priority_fee.buy_tip_fees.len() { && (swqos_clients.len() > priority_fee.buy_tip_fees.len()
|| priority_fee.buy_tip_fees.is_empty())
{
return Err(anyhow!("Number of tip clients exceeds the configured buy tip fees")); return Err(anyhow!("Number of tip clients exceeds the configured buy tip fees"));
} }
if !is_buy && swqos_clients.len() > priority_fee.sell_tip_fees.len() { if !is_buy
&& !with_tip
&& (swqos_clients.len() > priority_fee.sell_tip_fees.len()
|| priority_fee.sell_tip_fees.is_empty())
{
return Err(anyhow!("Number of tip clients exceeds the configured sell tip fees")); return Err(anyhow!("Number of tip clients exceeds the configured sell tip fees"));
} }
let instructions = Arc::new(instructions);
for i in 0..swqos_clients.len() { for i in 0..swqos_clients.len() {
let swqos_client = swqos_clients[i].clone(); let swqos_client = swqos_clients[i].clone();
if !with_tip && !matches!(swqos_client.get_swqos_type(), SwqosType::Default) { if !with_tip && !matches!(swqos_client.get_swqos_type(), SwqosType::Default) {
@@ -47,43 +55,36 @@ pub async fn parallel_execute_with_tips(
let core_id = cores[i % cores.len()]; let core_id = cores[i % cores.len()];
let middleware_manager = middleware_manager.clone(); let middleware_manager = middleware_manager.clone();
let protocol_name = protocol_name.clone();
let handle = tokio::spawn(async move { let handle = tokio::spawn(async move {
core_affinity::set_for_current(core_id); core_affinity::set_for_current(core_id);
let mut timer = TradeTimer::new(format!( let swqos_type = swqos_client.get_swqos_type();
"Building transaction instructions: {:?}", let mut start = Instant::now();
swqos_client.get_swqos_type()
));
let tip_account = swqos_client.get_tip_account()?; let tip_account_str = swqos_client.get_tip_account()?;
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?); let tip_account = Arc::new(Pubkey::from_str(&tip_account_str).unwrap_or_default());
if priority_fee.buy_tip_fees.len() == 0 {
return Err(anyhow!("buy_tip_fees is empty"));
}
let tip_amount = priority_fee.buy_tip_fees[i]; let tip_amount = priority_fee.buy_tip_fees[i];
let transaction = build_transaction( let transaction = build_transaction(
payer, payer,
&priority_fee, &priority_fee,
instructions, (*instructions).clone(),
lookup_table_key, lookup_table_key,
recent_blockhash, recent_blockhash,
data_size_limit, data_size_limit,
middleware_manager, middleware_manager,
protocol_name, protocol_name,
is_buy, is_buy,
swqos_client.get_swqos_type() != SwqosType::Default, swqos_type != SwqosType::Default,
&tip_account, &tip_account,
tip_amount, tip_amount,
) )
.await?; .await?;
timer.stage(format!( println!("Building transaction instructions: {:?} {:?}", swqos_type, start.elapsed());
"Submitting transaction instructions: {:?}",
swqos_client.get_swqos_type() start = Instant::now();
));
swqos_client swqos_client
.send_transaction( .send_transaction(
@@ -92,7 +93,8 @@ pub async fn parallel_execute_with_tips(
) )
.await?; .await?;
timer.finish(); println!("Submitting transaction instructions: {:?} {:?}", swqos_type, start.elapsed());
Ok::<(), anyhow::Error>(()) Ok::<(), anyhow::Error>(())
}); });
-45
View File
@@ -1,45 +0,0 @@
use std::time::Instant;
/// Trade time measurement tool
#[derive(Clone)]
pub struct TradeTimer {
start_time: Instant,
stage: String,
}
impl TradeTimer {
/// Create a new timer
pub fn new(stage: impl Into<String>) -> Self {
Self { start_time: Instant::now(), stage: stage.into() }
}
/// Record current stage time and start a new stage
pub fn stage(&mut self, new_stage: impl Into<String>) {
let elapsed = self.start_time.elapsed();
println!(" {} time cost: {:?}", self.stage, elapsed);
self.start_time = Instant::now();
self.stage = new_stage.into();
}
/// Complete timing and output final time cost
pub fn finish(mut self) {
let elapsed = self.start_time.elapsed();
println!(" {} time cost: {:?}", self.stage, elapsed);
self.stage.clear(); // Clear stage to avoid duplicate printing in Drop
}
/// Get the elapsed time of current stage (without resetting the timer)
pub fn elapsed(&self) -> std::time::Duration {
self.start_time.elapsed()
}
}
impl Drop for TradeTimer {
fn drop(&mut self) {
if !self.stage.is_empty() {
let elapsed = self.start_time.elapsed();
println!(" {} time cost: {:?}", self.stage, elapsed);
}
}
}
+53 -54
View File
@@ -19,70 +19,69 @@ pub enum DexType {
RaydiumAmmV4, RaydiumAmmV4,
} }
impl std::fmt::Display for DexType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DexType::PumpFun => write!(f, "PumpFun"),
DexType::PumpSwap => write!(f, "PumpSwap"),
DexType::Bonk => write!(f, "Bonk"),
DexType::RaydiumCpmm => write!(f, "RaydiumCpmm"),
DexType::RaydiumAmmV4 => write!(f, "RaydiumAmmV4"),
}
}
}
impl std::str::FromStr for DexType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"pumpfun" => Ok(DexType::PumpFun),
"pumpswap" => Ok(DexType::PumpSwap),
"bonk" => Ok(DexType::Bonk),
"raydiumcpmm" => Ok(DexType::RaydiumCpmm),
"raydiumammv4" => Ok(DexType::RaydiumAmmV4),
_ => Err(anyhow!("Unsupported protocol: {}", s)),
}
}
}
/// 交易工厂 - 用于创建不同协议的交易执行器 /// 交易工厂 - 用于创建不同协议的交易执行器
pub struct TradeFactory; pub struct TradeFactory;
impl TradeFactory { impl TradeFactory {
/// 创建指定协议的交易执行器 /// 创建指定协议的交易执行器(零开销单例)
pub fn create_executor(dex_type: DexType) -> Arc<dyn TradeExecutor> { pub fn create_executor(dex_type: DexType) -> Arc<dyn TradeExecutor> {
match dex_type { match dex_type {
DexType::PumpFun => { DexType::PumpFun => Self::pumpfun_executor(),
let instruction_builder = Arc::new(PumpFunInstructionBuilder); DexType::PumpSwap => Self::pumpswap_executor(),
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun")) DexType::Bonk => Self::bonk_executor(),
} DexType::RaydiumCpmm => Self::raydium_cpmm_executor(),
DexType::PumpSwap => { DexType::RaydiumAmmV4 => Self::raydium_amm_v4_executor(),
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
}
DexType::Bonk => {
let instruction_builder = Arc::new(BonkInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "Bonk"))
}
DexType::RaydiumCpmm => {
let instruction_builder = Arc::new(RaydiumCpmmInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumCpmm"))
}
DexType::RaydiumAmmV4 => {
let instruction_builder = Arc::new(RaydiumAmmV4InstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumAmmV4"))
}
} }
} }
/// 获取所有支持的协议 // Static instances created at compile time - zero runtime overhead
pub fn supported_dex_types() -> Vec<DexType> { #[inline]
vec![DexType::PumpFun, DexType::PumpSwap, DexType::Bonk, DexType::RaydiumCpmm] fn pumpfun_executor() -> Arc<dyn TradeExecutor> {
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
std::sync::LazyLock::new(|| {
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
});
INSTANCE.clone()
} }
/// 检查协议是否支持 #[inline]
pub fn is_supported(dex_type: &DexType) -> bool { fn pumpswap_executor() -> Arc<dyn TradeExecutor> {
Self::supported_dex_types().contains(dex_type) static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
std::sync::LazyLock::new(|| {
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
});
INSTANCE.clone()
}
#[inline]
fn bonk_executor() -> Arc<dyn TradeExecutor> {
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
std::sync::LazyLock::new(|| {
let instruction_builder = Arc::new(BonkInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "Bonk"))
});
INSTANCE.clone()
}
#[inline]
fn raydium_cpmm_executor() -> Arc<dyn TradeExecutor> {
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
std::sync::LazyLock::new(|| {
let instruction_builder = Arc::new(RaydiumCpmmInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumCpmm"))
});
INSTANCE.clone()
}
#[inline]
fn raydium_amm_v4_executor() -> Arc<dyn TradeExecutor> {
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
std::sync::LazyLock::new(|| {
let instruction_builder = Arc::new(RaydiumAmmV4InstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumAmmV4"))
});
INSTANCE.clone()
} }
} }