perf: replace dyn trait objects with concrete types to eliminate vtable overhead
Remove async-trait dependency and use native Rust async fn in traits. Replace Arc<dyn Trait> with Arc<ConcreteType> throughout the codebase to enable monomorphization and eliminate dynamic dispatch overhead. Key changes: - Remove write-behind cache (no implementation existed) - Fix should_transcode static method call - Use ContactStorageAdapter directly instead of dyn AddressBookUseCase - Clean up unused trait imports across services and DI https://claude.ai/code/session_01EbAFEfyJNLRmJHmmYDX3Tt
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
//! 3. HEAD /api/uploads/:id → Check progress
|
||||
//! 4. POST /api/uploads/:id/complete → Finalize and assemble
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use dashmap::DashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -755,7 +754,6 @@ impl ChunkedUploadService {
|
||||
|
||||
// ─── Port implementation ─────────────────────────────────────────────────────
|
||||
|
||||
#[async_trait]
|
||||
impl ChunkedUploadPort for ChunkedUploadService {
|
||||
async fn create_session(
|
||||
&self,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use flate2::Compression;
|
||||
use flate2::bufread::GzDecoder;
|
||||
@@ -43,7 +42,6 @@ impl From<CompressionLevel> for Compression {
|
||||
const COMPRESSION_SIZE_THRESHOLD: u64 = 1024 * 50; // 50KB
|
||||
|
||||
/// Interface for compression services
|
||||
#[async_trait]
|
||||
pub trait CompressionService: Send + Sync {
|
||||
/// Compresses data in memory
|
||||
async fn compress_data(&self, data: &[u8], level: CompressionLevel) -> io::Result<Vec<u8>>;
|
||||
@@ -88,7 +86,6 @@ impl GzipCompressionService {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CompressionService for GzipCompressionService {
|
||||
/// Compresses data in memory using Gzip
|
||||
async fn compress_data(&self, data: &[u8], level: CompressionLevel) -> io::Result<Vec<u8>> {
|
||||
@@ -258,7 +255,6 @@ impl From<PortCompressionLevel> for CompressionLevel {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CompressionPort for GzipCompressionService {
|
||||
async fn compress_data(
|
||||
&self,
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
//! - 30-50% storage reduction typical
|
||||
//! - Faster uploads for existing content (instant dedup)
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use futures::stream::{self, StreamExt};
|
||||
use futures::{Stream, TryStreamExt};
|
||||
@@ -802,7 +801,6 @@ impl DedupService {
|
||||
|
||||
// ─── Port implementation ─────────────────────────────────────────────────────
|
||||
|
||||
#[async_trait]
|
||||
impl DedupPort for DedupService {
|
||||
async fn store_bytes(
|
||||
&self,
|
||||
|
||||
@@ -198,9 +198,7 @@ pub type SharedFileContentCache = Arc<FileContentCache>;
|
||||
// ─── ContentCachePort implementation ─────────────────────────
|
||||
|
||||
use crate::application::ports::cache_ports::ContentCachePort;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
impl ContentCachePort for FileContentCache {
|
||||
fn should_cache(&self, size: usize) -> bool {
|
||||
FileContentCache::should_cache(self, size)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use async_trait::async_trait;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
@@ -63,7 +62,6 @@ impl FileSystemI18nService {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl I18nService for FileSystemI18nService {
|
||||
async fn translate(&self, key: &str, locale: Locale) -> I18nResult<String> {
|
||||
// Check if translations are cached
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
//! - Supports JPEG, PNG, GIF → WebP conversion
|
||||
//! - Falls back to original if conversion fails or result is larger
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use image::ImageFormat;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -392,7 +391,6 @@ impl From<PortOutputFormat> for OutputFormat {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ImageTranscodePort for ImageTranscodeService {
|
||||
fn can_transcode(&self, mime_type: &str) -> bool {
|
||||
ImageTranscodeService::can_transcode(mime_type)
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
//! Supports both RSA (RS256, RS384, RS512) and EC (ES256, ES384) algorithms.
|
||||
//! Compatible with Authentik, Keycloak, and any standard OIDC provider.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::Deserialize;
|
||||
use std::sync::RwLock;
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -293,7 +292,6 @@ impl OidcService {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl OidcServicePort for OidcService {
|
||||
async fn get_authorize_url(
|
||||
&self,
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
use argon2::password_hash::SaltString;
|
||||
use argon2::{Algorithm, Argon2, Params, PasswordHash, PasswordHasher, PasswordVerifier, Version};
|
||||
use async_trait::async_trait;
|
||||
use rand_core::OsRng;
|
||||
|
||||
use crate::application::ports::auth_ports::PasswordHasherPort;
|
||||
@@ -57,7 +56,6 @@ impl Argon2PasswordHasher {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PasswordHasherPort for Argon2PasswordHasher {
|
||||
async fn hash_password(&self, password: &str) -> Result<String, DomainError> {
|
||||
let pwd = password.to_owned();
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
//!
|
||||
//! StoragePath (Value Object) remains in domain/services/path_service.rs
|
||||
|
||||
use async_trait::async_trait;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tokio::fs;
|
||||
|
||||
@@ -116,7 +115,6 @@ impl PathService {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl StoragePort for PathService {
|
||||
fn resolve_path(&self, storage_path: &StoragePath) -> PathBuf {
|
||||
let mut path = self.root_path.clone();
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use image::{ImageFormat, imageops::FilterType};
|
||||
/**
|
||||
@@ -458,7 +457,6 @@ impl From<PortThumbnailSize> for ThumbnailSize {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ThumbnailPort for ThumbnailService {
|
||||
fn is_supported_image(&self, mime_type: &str) -> bool {
|
||||
ThumbnailService::is_supported_image(mime_type)
|
||||
|
||||
@@ -5,6 +5,7 @@ use tracing::{debug, error, info, instrument};
|
||||
|
||||
use crate::common::errors::Result;
|
||||
use crate::domain::repositories::trash_repository::TrashRepository;
|
||||
use crate::infrastructure::repositories::pg::trash_db_repository::TrashDbRepository;
|
||||
|
||||
/// Service for automatic cleanup of expired items in the trash.
|
||||
///
|
||||
@@ -12,12 +13,12 @@ use crate::domain::repositories::trash_repository::TrashRepository;
|
||||
/// in **2 SQL statements inside a single transaction**, instead of the
|
||||
/// previous N+1 pattern that issued 3 queries per expired item.
|
||||
pub struct TrashCleanupService {
|
||||
trash_repository: Arc<dyn TrashRepository>,
|
||||
trash_repository: Arc<TrashDbRepository>,
|
||||
cleanup_interval_hours: u64,
|
||||
}
|
||||
|
||||
impl TrashCleanupService {
|
||||
pub fn new(trash_repository: Arc<dyn TrashRepository>, cleanup_interval_hours: u64) -> Self {
|
||||
pub fn new(trash_repository: Arc<TrashDbRepository>, cleanup_interval_hours: u64) -> Self {
|
||||
Self {
|
||||
trash_repository,
|
||||
cleanup_interval_hours: cleanup_interval_hours.max(1), // Minimum 1 hour
|
||||
@@ -57,7 +58,7 @@ impl TrashCleanupService {
|
||||
|
||||
/// Bulk-delete all expired trash items in a single transaction.
|
||||
#[instrument(skip(trash_repository))]
|
||||
async fn cleanup_expired_items(trash_repository: Arc<dyn TrashRepository>) -> Result<()> {
|
||||
async fn cleanup_expired_items(trash_repository: Arc<TrashDbRepository>) -> Result<()> {
|
||||
debug!("Starting bulk cleanup of expired trash items");
|
||||
|
||||
let (files, folders) = trash_repository.delete_expired_bulk().await?;
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::{
|
||||
application::ports::zip_ports::ZipPort,
|
||||
common::errors::{DomainError, ErrorKind, Result},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use async_zip::base::write::ZipFileWriter;
|
||||
use async_zip::{Compression, ZipEntryBuilder};
|
||||
use futures::StreamExt;
|
||||
@@ -17,6 +16,8 @@ use thiserror::Error;
|
||||
use tokio::io::BufWriter;
|
||||
use tokio_util::compat::Compat;
|
||||
use tracing::*;
|
||||
use crate::application::services::file_retrieval_service::FileRetrievalService;
|
||||
use crate::application::services::folder_service::FolderService;
|
||||
|
||||
/// Error related to ZIP file creation
|
||||
#[derive(Debug, Error)]
|
||||
@@ -53,15 +54,15 @@ type AsyncZipWriter = ZipFileWriter<Compat<BufWriter<tokio::fs::File>>>;
|
||||
/// `tokio::io::BufWriter` → `tokio::fs::File`, so **no Tokio worker is ever
|
||||
/// blocked** by disk I/O or compression.
|
||||
pub struct ZipService {
|
||||
file_service: Arc<dyn FileRetrievalUseCase>,
|
||||
folder_service: Arc<dyn FolderUseCase>,
|
||||
file_service: Arc<FileRetrievalService>,
|
||||
folder_service: Arc<FolderService>,
|
||||
}
|
||||
|
||||
impl ZipService {
|
||||
/// Creates a new instance of the ZIP service
|
||||
pub fn new(
|
||||
file_service: Arc<dyn FileRetrievalUseCase>,
|
||||
folder_service: Arc<dyn FolderUseCase>,
|
||||
file_service: Arc<FileRetrievalService>,
|
||||
folder_service: Arc<FolderService>,
|
||||
) -> Self {
|
||||
Self {
|
||||
file_service,
|
||||
@@ -239,7 +240,6 @@ impl ZipService {
|
||||
|
||||
// ─── Port implementation ─────────────────────────────────────────────────────
|
||||
|
||||
#[async_trait]
|
||||
impl ZipPort for ZipService {
|
||||
async fn create_folder_zip(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user