Massively enhance and improve this code use dashmap::DashSet; use rayon::prelude::*; use…
- User
Massively enhance and improve this code use dashmap::DashSet; use rayon::prelude::*; use std::{ collections::VecDeque, fs, path::Path, sync::{atomic::{AtomicUsize, Ordering}, Arc, Mutex}, time::{Duration, Instant}, }; use chrono::{DateTime, Local, Utc}; use indicatif::{ProgressBar, ProgressStyle}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use reqwest::{Client, StatusCode}; use thiserror::Error; use tokio::sync::Mutex as TokioMutex; use tokio::{io::AsyncWriteExt, sync::{Semaphore, mpsc}}; use thousands::Separable; use sha2::{Sha256, Digest}; use mimalloc::MiMalloc;
#[global_allocator] static GLOBAL: MiMalloc = MiMalloc;
const BASE_URL: &str = "https://litter.catbox.moe/"; const ALLOWED_EXTENSIONS: &[&str] = &["png"]; const CONCURRENT_REQUESTS: usize = 7500; const TOTAL_REQUESTS: usize = 50_000_000; const URL_LENGTH: usize = 6; const OUTPUT_DIR: &str = "output"; const NUM_WORKERS: usize = 7500; const CONNECTIONS_PER_WORKER: usize = 24;
#[derive(Error, Debug)] enum MyError { #[error("Request error: {0}")] Reqwest(#[from] reqwest::Error),
#[error("IO error: {0}")] Io(#[from] std::io::Error), #[error("Serialization error: {0}")] Serde(#[from] serde_json::Error), #[error("Join error: {0}")] Join(#[from] tokio::task::JoinError), #[error("Anyhow error: {0}")] Anyhow(#[from] anyhow::Error),} struct ConnectionPool { connections: Vec, }
impl ConnectionPool { fn new(size: usize) -> Self { let connections = (0..size) .map(|_| { Client::builder() .pool_max_idle_per_host(100) .pool_idle_timeout(Some(Duration::from_secs(30))) .tcp_keepalive(Some(Duration::from_secs(60))) .build() .unwrap() }) .collect(); ConnectionPool { connections } }
fn get_connection(&self) -> &Client { &self.connections[thread_rng().gen_range(0..self.connections.len())] }}
#[tokio::main(flavor = "multi_thread", worker_threads = 32)] async fn main() -> Result<(), MyError> { fs::create_dir_all(OUTPUT_DIR)?; sort_files_by_type().await?; process_files_in_batches()?;
let url_generation_start_time = Instant::now(); let urls = Arc::new(Mutex::new(generate_urls(TOTAL_REQUESTS))); let url_generation_time = url_generation_start_time.elapsed(); spdlog::info!("Generated {} URLs in {:?}", TOTAL_REQUESTS.separate_with_commas(), url_generation_time); let found_urls = Arc::new(DashSet::with_capacity(TOTAL_REQUESTS)); // Changed to DashSet let total_found = Arc::new(AtomicUsize::new(0)); let semaphore = Arc::new(Semaphore::new(CONCURRENT_REQUESTS)); let overall_progress_bar = create_progress_bar("Overall Progress", TOTAL_REQUESTS); let (task_sender, task_receiver) = mpsc::channel(TOTAL_REQUESTS); let task_receiver = Arc::new(TokioMutex::new(task_receiver)); let worker_handles: Vec<_> = (0..NUM_WORKERS) .map(|_| { let urls = Arc::clone(&urls); let found_urls = Arc::clone(&found_urls); let total_found = Arc::clone(&total_found); let semaphore = Arc::clone(&semaphore); let overall_progress_bar = overall_progress_bar.clone(); let task_receiver = Arc::clone(&task_receiver); let connection_pool = Arc::new(ConnectionPool::new(CONNECTIONS_PER_WORKER)); tokio::spawn(async move { worker_loop( urls, found_urls, total_found, semaphore, overall_progress_bar, task_receiver, connection_pool, ) .await }) }) .collect(); let request_start_time = Instant::now(); // Initial task distribution for _ in 0..TOTAL_REQUESTS { task_sender.send(()).await.unwrap(); } // Wait for all workers to complete for handle in worker_handles { handle.await??; } let request_time = request_start_time.elapsed(); overall_progress_bar.finish_with_message("Finished"); spdlog::info!( "Found {} unique urls in {:?}. URL generation took {:?}", total_found.load(Ordering::Relaxed), request_time, url_generation_time ); Ok(()) - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.