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
+53 -54
View File
@@ -19,70 +19,69 @@ pub enum DexType {
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;
impl TradeFactory {
/// 创建指定协议的交易执行器
/// 创建指定协议的交易执行器(零开销单例)
pub fn create_executor(dex_type: DexType) -> Arc<dyn TradeExecutor> {
match dex_type {
DexType::PumpFun => {
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
}
DexType::PumpSwap => {
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"))
}
DexType::PumpFun => Self::pumpfun_executor(),
DexType::PumpSwap => Self::pumpswap_executor(),
DexType::Bonk => Self::bonk_executor(),
DexType::RaydiumCpmm => Self::raydium_cpmm_executor(),
DexType::RaydiumAmmV4 => Self::raydium_amm_v4_executor(),
}
}
/// 获取所有支持的协议
pub fn supported_dex_types() -> Vec<DexType> {
vec![DexType::PumpFun, DexType::PumpSwap, DexType::Bonk, DexType::RaydiumCpmm]
// Static instances created at compile time - zero runtime overhead
#[inline]
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()
}
/// 检查协议是否支持
pub fn is_supported(dex_type: &DexType) -> bool {
Self::supported_dex_types().contains(dex_type)
#[inline]
fn pumpswap_executor() -> Arc<dyn TradeExecutor> {
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()
}
}