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:
Claude
2026-03-03 15:36:42 +00:00
parent efcf88c4d7
commit 1b49135ca9
101 changed files with 346 additions and 576 deletions
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::Utc;
use sqlx::{PgPool, Row, types::Uuid};
use std::sync::Arc;
@@ -19,7 +18,6 @@ impl AddressBookPgRepository {
}
}
#[async_trait]
impl AddressBookRepository for AddressBookPgRepository {
async fn create_address_book(
&self,
@@ -3,7 +3,6 @@
use crate::application::ports::auth_ports::AppPasswordStoragePort;
use crate::common::errors::DomainError;
use crate::domain::entities::app_password::AppPassword;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use std::sync::Arc;
@@ -22,7 +21,6 @@ impl AppPasswordPgRepository {
}
}
#[async_trait]
impl AppPasswordStoragePort for AppPasswordPgRepository {
async fn create(&self, ap: AppPassword) -> Result<AppPassword, DomainError> {
sqlx::query(
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sqlx::{PgPool, Row, types::Uuid};
use std::sync::Arc;
@@ -19,7 +18,6 @@ impl CalendarEventPgRepository {
}
}
#[async_trait]
impl CalendarEventRepository for CalendarEventPgRepository {
async fn create_event(
&self,
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::Utc;
use sqlx::{PgPool, Row, types::Uuid};
use std::sync::Arc;
@@ -19,7 +18,6 @@ impl CalendarPgRepository {
}
}
#[async_trait]
impl CalendarRepository for CalendarPgRepository {
async fn create_calendar(&self, calendar: Calendar) -> CalendarRepositoryResult<Calendar> {
let row = sqlx::query(
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::Utc;
use serde_json::Value as JsonValue;
use sqlx::{PgPool, Row, types::Uuid};
@@ -24,7 +23,6 @@ impl ContactGroupPgRepository {
}
}
#[async_trait]
impl ContactGroupRepository for ContactGroupPgRepository {
async fn create_group(&self, group: ContactGroup) -> ContactRepositoryResult<ContactGroup> {
sqlx::query(
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::Utc;
use serde_json::Value as JsonValue;
use sqlx::{PgPool, Row, types::Uuid};
@@ -63,7 +62,6 @@ impl ContactPgRepository {
}
}
#[async_trait]
impl ContactRepository for ContactPgRepository {
async fn create_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact> {
// Convert domain entities to persistence DTOs for JSONB serialization
@@ -1,6 +1,5 @@
//! PostgreSQL repository for Device Authorization Grant (RFC 8628) codes.
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use std::sync::Arc;
@@ -49,7 +48,6 @@ impl DeviceCodePgRepository {
}
}
#[async_trait]
impl DeviceCodeStoragePort for DeviceCodePgRepository {
async fn create_device_code(&self, dc: DeviceCode) -> Result<DeviceCode, DomainError> {
sqlx::query(
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use std::sync::Arc;
use tracing::error;
@@ -19,7 +18,6 @@ impl FavoritesPgRepository {
}
}
#[async_trait]
impl FavoritesRepositoryPort for FavoritesPgRepository {
async fn get_favorites(&self, user_id: &str) -> Result<Vec<FavoriteItemDto>> {
let user_uuid = Uuid::parse_str(user_id)?;
@@ -7,7 +7,6 @@
//! File paths are resolved by JOINing with `storage.folders.path` (the
//! materialized path column), so no recursive CTEs or N+1 queries are needed.
use async_trait::async_trait;
use bytes::Bytes;
use futures::{Stream, TryStreamExt};
use moka::sync::Cache;
@@ -17,16 +16,16 @@ use std::sync::Arc;
use std::time::Duration;
use crate::application::dtos::search_dto::SearchCriteriaDto;
use crate::application::ports::dedup_ports::DedupPort;
use crate::application::ports::storage_ports::FileReadPort;
use crate::common::errors::DomainError;
use crate::domain::entities::file::File;
use crate::domain::services::path_service::StoragePath;
use crate::infrastructure::services::dedup_service::DedupService;
/// File read repository backed by PostgreSQL metadata + blob storage.
pub struct FileBlobReadRepository {
pool: Arc<PgPool>,
dedup: Arc<dyn DedupPort>,
dedup: Arc<DedupService>,
/// Lock-free cache: file_id → blob_hash.
/// Populated by `get_file()` and `resolve_blob_hash()` (slow path).
/// Entries persist until TTI expiry (30 s idle) or capacity eviction —
@@ -37,7 +36,7 @@ pub struct FileBlobReadRepository {
impl FileBlobReadRepository {
pub fn new(
pool: Arc<PgPool>,
dedup: Arc<dyn DedupPort>,
dedup: Arc<DedupService>,
_folder_repo: Arc<super::folder_db_repository::FolderDbRepository>,
) -> Self {
Self {
@@ -115,7 +114,6 @@ impl FileBlobReadRepository {
}
}
#[async_trait]
impl FileReadPort for FileBlobReadRepository {
async fn get_file(&self, id: &str) -> Result<File, DomainError> {
let row = sqlx::query_as::<
@@ -890,14 +888,13 @@ impl FileReadPort for FileBlobReadRepository {
mod tests {
use super::*;
use crate::common::stubs::StubDedupPort;
use crate::infrastructure::repositories::pg::folder_db_repository::FolderDbRepository;
/// Helper: build a `FileBlobReadRepository` without a real PgPool.
/// Only the moka `hash_cache` is exercised — no SQL is executed.
fn make_repo() -> FileBlobReadRepository {
let _folder_repo = Arc::new(FolderDbRepository::new_stub());
// StubDedupPort satisfies the trait but is never called in cache-only tests
let dedup: Arc<dyn DedupPort> = Arc::new(StubDedupPort);
let dedup: Arc<DedupService> = Arc::new(StubDedupPort);
// PgPool is required by the struct but we won't hit any SQL in these tests.
// We create a repo with a stub pool placeholder — only hash_cache is tested.
FileBlobReadRepository {
@@ -7,30 +7,29 @@
//! File paths are resolved by querying the materialized `storage.folders.path`
//! column (O(1) per lookup), so no recursive CTEs are needed.
use async_trait::async_trait;
use sqlx::PgPool;
use std::path::PathBuf;
use std::sync::Arc;
use crate::application::ports::dedup_ports::DedupPort;
use crate::application::ports::storage_ports::{CopyFolderTreeResult, FileWritePort};
use crate::common::errors::DomainError;
use crate::domain::entities::file::File;
use crate::domain::services::path_service::StoragePath;
use super::folder_db_repository::FolderDbRepository;
use crate::infrastructure::services::dedup_service::DedupService;
/// File write repository backed by PostgreSQL metadata + blob storage.
pub struct FileBlobWriteRepository {
pool: Arc<PgPool>,
dedup: Arc<dyn DedupPort>,
dedup: Arc<DedupService>,
folder_repo: Arc<FolderDbRepository>,
}
impl FileBlobWriteRepository {
pub fn new(
pool: Arc<PgPool>,
dedup: Arc<dyn DedupPort>,
dedup: Arc<DedupService>,
folder_repo: Arc<FolderDbRepository>,
) -> Self {
Self {
@@ -180,7 +179,6 @@ impl FileBlobWriteRepository {
}
}
#[async_trait]
impl FileWritePort for FileBlobWriteRepository {
async fn save_file_from_temp(
&self,
@@ -8,7 +8,6 @@
//! database triggers, so reading a folder's full path is always O(1) — no
//! recursive CTEs or N+1 queries.
use async_trait::async_trait;
use sqlx::PgPool;
use std::sync::Arc;
@@ -72,7 +71,6 @@ impl FolderDbRepository {
}
}
#[async_trait]
impl FolderRepository for FolderDbRepository {
async fn create_folder(
&self,
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use std::sync::Arc;
use tracing::error;
@@ -19,7 +18,6 @@ impl RecentItemsPgRepository {
}
}
#[async_trait]
impl RecentItemsRepositoryPort for RecentItemsPgRepository {
async fn get_recent_items(&self, user_id: &str, limit: i32) -> Result<Vec<RecentItemDto>> {
let user_uuid = Uuid::parse_str(user_id)?;
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use chrono::Utc;
use futures::future::BoxFuture;
use sqlx::{PgPool, Row};
@@ -39,7 +38,6 @@ impl SessionPgRepository {
}
}
#[async_trait]
impl SessionRepository for SessionPgRepository {
/// Creates a new session using a transaction
async fn create_session(&self, session: Session) -> SessionRepositoryResult<Session> {
@@ -291,7 +289,6 @@ impl SessionRepository for SessionPgRepository {
}
// Implementation of the storage port for the application layer
#[async_trait]
impl SessionStoragePort for SessionPgRepository {
async fn create_session(&self, session: Session) -> Result<Session, DomainError> {
SessionRepository::create_session(self, session)
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use sqlx::PgPool;
use std::collections::HashMap;
use std::sync::Arc;
@@ -16,7 +15,6 @@ impl SettingsPgRepository {
}
}
#[async_trait]
impl SettingsRepository for SettingsPgRepository {
async fn get(&self, key: &str) -> Result<Option<String>, DomainError> {
let row =
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use std::sync::Arc;
@@ -72,7 +71,6 @@ impl SharePgRepository {
}
}
#[async_trait]
impl ShareStoragePort for SharePgRepository {
async fn save_share(&self, share: &Share) -> Result<Share, DomainError> {
let row = sqlx::query(
@@ -4,7 +4,6 @@
//! and `storage.folders`. There is no separate trash table — trashed items
//! are files/folders with `is_trashed = TRUE`.
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use std::sync::Arc;
@@ -65,7 +64,6 @@ impl TrashDbRepository {
}
}
#[async_trait]
impl TrashRepository for TrashDbRepository {
async fn add_to_trash(&self, _item: &TrashedItem) -> Result<()> {
// No-op: the actual flagging is done by FileWritePort::move_to_trash
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use futures::future::BoxFuture;
use sqlx::{PgPool, Row};
use std::sync::Arc;
@@ -44,7 +43,6 @@ impl UserPgRepository {
}
}
#[async_trait]
impl UserRepository for UserPgRepository {
/// Creates a new user using a transaction
async fn create_user(&self, user: User) -> UserRepositoryResult<User> {
@@ -619,7 +617,6 @@ impl UserRepository for UserPgRepository {
}
// Storage port implementation for the application layer
#[async_trait]
impl UserStoragePort for UserPgRepository {
async fn create_user(&self, user: User) -> Result<User, DomainError> {
UserRepository::create_user(self, user)