fix: resolve rustfmt configuration duplicate key error and apply consistent code formatting across all source files

This commit is contained in:
floor-licker
2025-12-05 19:09:06 -05:00
parent 5576d765ee
commit 9993e51c7f
29 changed files with 2540 additions and 1673 deletions
+95 -56
View File
@@ -54,100 +54,125 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Comparing with polymarket-rs-client baseline:");
println!(" 88,053 allocs, 81,823 frees, 15,945,966 bytes allocated");
println!();
// Load environment variables
dotenv::dotenv().ok();
let client = ClobClient::new_internet("https://clob.polymarket.com");
// Test 1: Market Data Fetching Memory Usage
println!("📊 Test 1: Market Data Fetching Memory");
println!("=====================================");
// Reset and measure market data fetching
reset_counters();
let start_stats = get_memory_stats();
let start_time = Instant::now();
let result = client.get_sampling_simplified_markets(None).await;
let duration = start_time.elapsed();
let end_stats = get_memory_stats();
match result {
Ok(markets) => {
println!("✅ Fetched {} markets in {:?}", markets.data.len(), duration);
println!(
"✅ Fetched {} markets in {:?}",
markets.data.len(),
duration
);
let (bytes_allocated, allocs, deallocs) = (
end_stats.0 - start_stats.0,
end_stats.1 - start_stats.1,
end_stats.2 - start_stats.2,
);
println!("📈 polyfill-rs memory usage:");
println!(" {} allocs, {} frees, {} bytes allocated", allocs, deallocs, bytes_allocated);
println!("📊 vs baseline (15,945,966 bytes): {:.1}x less memory",
15_945_966.0 / bytes_allocated as f64);
println!("📊 vs baseline ({} allocs): {:.1}x fewer allocations",
88_053, 88_053.0 / allocs as f64);
}
println!(
" {} allocs, {} frees, {} bytes allocated",
allocs, deallocs, bytes_allocated
);
println!(
"📊 vs baseline (15,945,966 bytes): {:.1}x less memory",
15_945_966.0 / bytes_allocated as f64
);
println!(
"📊 vs baseline ({} allocs): {:.1}x fewer allocations",
88_053,
88_053.0 / allocs as f64
);
},
Err(e) => {
println!("❌ Error: {}", e);
println!("⚠️ Still measuring memory usage of error handling...");
let (bytes_allocated, allocs, deallocs) = (
end_stats.0 - start_stats.0,
end_stats.1 - start_stats.1,
end_stats.2 - start_stats.2,
);
println!("📈 Memory usage (even with error):");
println!(" {} allocs, {} frees, {} bytes allocated", allocs, deallocs, bytes_allocated);
}
println!(
" {} allocs, {} frees, {} bytes allocated",
allocs, deallocs, bytes_allocated
);
},
}
// Test 2: Order Book Memory Efficiency
println!("\n📊 Test 2: Order Book Memory Efficiency");
println!("======================================");
reset_counters();
let start_stats = get_memory_stats();
// Create order book and populate it
let mut book = OrderBookImpl::new("test_token".to_string(), 100);
// Add many orders to test memory efficiency
for i in 0..1000 {
let price = Decimal::from_str(&format!("0.{:04}", 5000 + (i % 100))).unwrap();
let size = Decimal::from_str("100.0").unwrap();
let delta = polyfill_rs::OrderDelta {
token_id: "test_token".to_string(),
timestamp: chrono::Utc::now(),
side: if i % 2 == 0 { polyfill_rs::Side::BUY } else { polyfill_rs::Side::SELL },
side: if i % 2 == 0 {
polyfill_rs::Side::BUY
} else {
polyfill_rs::Side::SELL
},
price,
size,
sequence: i as u64,
};
let _ = book.apply_delta(delta);
}
let end_stats = get_memory_stats();
let (bytes_allocated, allocs, deallocs) = (
end_stats.0 - start_stats.0,
end_stats.1 - start_stats.1,
end_stats.2 - start_stats.2,
);
println!("📈 Order book (1000 updates):");
println!(" {} allocs, {} frees, {} bytes allocated", allocs, deallocs, bytes_allocated);
println!("📊 Per update: {:.1} bytes/update", bytes_allocated as f64 / 1000.0);
println!(
" {} allocs, {} frees, {} bytes allocated",
allocs, deallocs, bytes_allocated
);
println!(
"📊 Per update: {:.1} bytes/update",
bytes_allocated as f64 / 1000.0
);
// Test 3: JSON Parsing Memory
println!("\n📊 Test 3: JSON Parsing Memory Usage");
println!("===================================");
let sample_json = r#"{
"data": [
{
@@ -168,69 +193,83 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
]
}"#;
reset_counters();
let start_stats = get_memory_stats();
// Parse JSON 1000 times to measure memory usage
for _ in 0..1000 {
let _: Result<serde_json::Value, _> = serde_json::from_str(sample_json);
}
let end_stats = get_memory_stats();
let (bytes_allocated, allocs, deallocs) = (
end_stats.0 - start_stats.0,
end_stats.1 - start_stats.1,
end_stats.2 - start_stats.2,
);
println!("📈 JSON parsing (1000 operations):");
println!(" {} allocs, {} frees, {} bytes allocated", allocs, deallocs, bytes_allocated);
println!("📊 Per parse: {:.1} bytes/parse", bytes_allocated as f64 / 1000.0);
println!(
" {} allocs, {} frees, {} bytes allocated",
allocs, deallocs, bytes_allocated
);
println!(
"📊 Per parse: {:.1} bytes/parse",
bytes_allocated as f64 / 1000.0
);
// Test 4: Fixed-point vs Decimal Memory
println!("\n📊 Test 4: Fixed-point vs Decimal Memory");
println!("=======================================");
// Test Decimal operations
reset_counters();
let start_stats = get_memory_stats();
let mut decimals = Vec::new();
for i in 0..1000 {
let decimal = Decimal::from_str(&format!("0.{:04}", i)).unwrap();
decimals.push(decimal);
}
let end_stats = get_memory_stats();
let decimal_memory = end_stats.0 - start_stats.0;
let decimal_allocs = end_stats.1 - start_stats.1;
println!("📈 Decimal operations (1000 values):");
println!(" {} allocs, {} bytes allocated", decimal_allocs, decimal_memory);
println!(
" {} allocs, {} bytes allocated",
decimal_allocs, decimal_memory
);
// Test fixed-point operations
reset_counters();
let start_stats = get_memory_stats();
let mut fixed_points = Vec::new();
for i in 0..1000 {
let fixed_point = (i as u32) * 10000; // Scale factor of 10000
fixed_points.push(fixed_point);
}
let end_stats = get_memory_stats();
let fixed_memory = end_stats.0 - start_stats.0;
let fixed_allocs = end_stats.1 - start_stats.1;
println!("📈 Fixed-point operations (1000 values):");
println!(" {} allocs, {} bytes allocated", fixed_allocs, fixed_memory);
println!(
" {} allocs, {} bytes allocated",
fixed_allocs, fixed_memory
);
if decimal_memory > 0 && fixed_memory > 0 {
println!("📊 Fixed-point vs Decimal: {:.1}x less memory",
decimal_memory as f64 / fixed_memory as f64);
println!(
"📊 Fixed-point vs Decimal: {:.1}x less memory",
decimal_memory as f64 / fixed_memory as f64
);
}
println!("\n🎯 Memory Benchmark Summary");
println!("==========================");
println!("Key Findings:");
@@ -238,8 +277,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!(" • Fixed-point arithmetic: Significantly less memory than Decimal");
println!(" • JSON parsing: Efficient deserialization");
println!(" • Network operations: Memory usage dominated by response size");
println!("\nNote: These are ACTUAL measured values, not estimates!");
Ok(())
}