You are an Expert RUST DEVELOPER and PERFORMANCE OPTIMIZATION SPECIALIST. Your task is to REVISE and ENHANCE a codebase using Rust Nightly 1.85, ensuring that you UTILIZE the LATEST and FASTEST libraries available.

Follow these steps to MAXIMIZE PERFORMANCE:

1. REVIEW the current codebase thoroughly to IDENTIFY areas that may benefit from optimization.
2. UPDATE all dependencies to their LATEST versions compatible with Rust Nightly 1.85, focusing on those known for their SPEED and EFFICIENCY.
3. ANALYZE opportunities for implementing PARALLELISM in the code, taking advantage of Rust's robust concurrency features.
4. INTEGRATE advanced CACHING mechanisms where applicable to reduce redundant computations and improve response times.
5. EXPLORE other performance ENHANCEMENT techniques such as minimizing memory allocations, optimizing data structures, and refining algorithms.
6. TEST the updated code extensively to ENSURE that each optimization indeed contributes positively to overall performance without introducing bugs.
7. DOCUMENT your changes clearly, explaining WHY and HOW they improve performance, aiding future maintenance or further optimizations.

You MUST ensure that all changes maintain or improve the STABILITY and RELIABILITY of the code.

Remember, I’m going to tip $300K for a BETTER SOLUTION!

Now Take a Deep Breath.

```rust

use dashmap::DashMap;
use lingua::Language::{
    Arabic, Chinese, English, French, German, Italian, Japanese, Korean, Latvian, Lithuanian,
    Russian, Spanish,
};
use lingua::{LanguageDetector, LanguageDetectorBuilder};
use ntex::web::types::Query;
use ntex::web::{self, App, HttpResponse, HttpServer, Responder};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use sysinfo::System;

static TOTAL_REQUESTS: AtomicUsize = AtomicUsize::new(0);
static CACHE_HITS: AtomicUsize = AtomicUsize::new(0);
static TOTAL_CACHE_TIME: AtomicU64 = AtomicU64::new(0);
static TOTAL_DETECTION_TIME: AtomicU64 = AtomicU64::new(0);
static DETECTION_COUNT: AtomicUsize = AtomicUsize::new(0);

static SYSTEM: Lazy<std::sync::Mutex<System>> = Lazy::new(|| {
    let mut sys = System::new_all();
    sys.refresh_all();
    std::sync::Mutex::new(sys)
});

#[derive(Deserialize)]
struct DetectQuery {
    text: String,
}

#[derive(Clone)]
struct CacheEntry {
    lang_code: String,
    timestamp: Instant,
}

#[derive(Serialize)]
struct CpuInfo {
    model: String,
    usage: f32,
    frequency: u64,
    cores: usize,
}

#[derive(Serialize)]
struct Stats {
    cpu: CpuInfo,
    memory_used_mb: u64,
    total_memory_mb: u64,
    total_requests: usize,
    cache_hits: usize,
    cache_hit_ratio: f64,
    cache_entries: usize,
    uptime_secs: u64,
    avg_cache_time_micros: u64,
    avg_detection_time_micros: u64,
}

static CACHE: Lazy<DashMap<String, CacheEntry>> = Lazy::new(DashMap::new);
static CACHE_DURATION: Duration = Duration::from_secs(43200); // 12 hours
static DETECTOR: Lazy<LanguageDetector> = Lazy::new(|| {
    eprintln!("Initializing language detector");
    LanguageDetectorBuilder::from_languages(&[
        Arabic, Chinese, English, French, German, Italian, Japanese, Korean, Russian, Spanish,
        Lithuanian, Latvian,
    ])
    .with_preloaded_language_models()
    .build()
});

fn build_response(body: String) -> HttpResponse {
    HttpResponse::Ok()
        .header("Access-Control-Allow-Origin", "*")
        .header("Cache-Control", "max-age=3600")
        .header("Access-Control-Allow-Headers", "*")
        .body(body)
}

async fn detect_and_respond(text: &str) -> impl Responder {
    let start_time = Instant::now();
    TOTAL_REQUESTS.fetch_add(1, Ordering::SeqCst);

    let now = Instant::now();
    CACHE.retain(|_, entry| now.duration_since(entry.timestamp) < CACHE_DURATION);

    if let Some(entry) = CACHE.get(text) {
        eprintln!("Cache hit for text: {}", text);
        CACHE_HITS.fetch_add(1, Ordering::SeqCst);
        let cache_time = start_time.elapsed().as_micros() as u64;
        TOTAL_CACHE_TIME.fetch_add(cache_time, Ordering::SeqCst);
        return build_response(entry.lang_code.clone());
    }

    eprintln!("Detecting language for text: {}", text);
    let detection_start = Instant::now();
    let lang_code = DETECTOR
        .detect_language_of(text)
        .map(|lang| lang.iso_code_639_1().to_string())
        .unwrap_or_else(|| "auto".to_string());

    let detection_time = detection_start.elapsed().as_micros() as u64;
    TOTAL_DETECTION_TIME.fetch_add(detection_time, Ordering::SeqCst);
    DETECTION_COUNT.fetch_add(1, Ordering::SeqCst);

    CACHE.insert(
        text.to_string(),
        CacheEntry {
            lang_code: lang_code.clone(),
            timestamp: now,
        },
    );

    build_response(lang_code)
}

async fn detect_language_handler(body: String) -> impl Responder {
    eprintln!("Received POST request");
    detect_and_respond(&body).await
}

async fn detect_language_get(query: Query<DetectQuery>) -> impl Responder {
    eprintln!("Received GET request");
    detect_and_respond(&query.text).await
}

async fn stats_handler() -> impl Responder {
    let mut sys = SYSTEM.lock().unwrap();
    sys.refresh_all();

    let total_requests = TOTAL_REQUESTS.load(Ordering::SeqCst);
    let cache_hits = CACHE_HITS.load(Ordering::SeqCst);
    let cache_hit_ratio = if total_requests > 0 {
        (cache_hits as f64 / total_requests as f64) * 100.0
    } else {
        0.0
    };

    let cpu_info = CpuInfo {
        model: sys.cpus()[0].brand().to_string(),
        usage: sys.global_cpu_usage(),
        frequency: sys.cpus()[0].frequency(),
        cores: sys.cpus().len(),
    };

    let avg_cache_time = if cache_hits > 0 {
        TOTAL_CACHE_TIME.load(Ordering::SeqCst) / cache_hits as u64
    } else {
        0
    };

    let detection_count = DETECTION_COUNT.load(Ordering::SeqCst);
    let avg_detection_time = if detection_count > 0 {
        TOTAL_DETECTION_TIME.load(Ordering::SeqCst) / detection_count as u64
    } else {
        0
    };

    let stats = Stats {
        cpu: cpu_info,
        memory_used_mb: sys.used_memory() / 1024 / 1024,
        total_memory_mb: sys.total_memory() / 1024 / 1024,
        total_requests,
        cache_hits,
        cache_hit_ratio,
        cache_entries: CACHE.len(),
        uptime_secs: System::uptime(),
        avg_cache_time_micros: avg_cache_time,
        avg_detection_time_micros: avg_detection_time,
    };

    HttpResponse::Ok()
        .content_type("application/json")
        .json(&stats)
}

#[ntex::main]
async fn main() -> std::io::Result<()> {
    env_logger::init();
    eprintln!("Starting language detection server");

    HttpServer::new(|| {
        App::new()
            .service(
                web::resource("/detect")
                    .route(web::post().to(detect_language_handler))
                    .route(web::get().to(detect_language_get)),
            )
            .service(web::resource("/stats").route(web::get().to(stats_handler)))
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

```