From 2cb49c68759da1c5e49fe2815f77177b69123670 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 7 Aug 2019 20:41:01 +0200 Subject: [PATCH] Use arc to minimize clone overhead --- backend/src/cache.rs | 10 ++++++++-- backend/src/data.rs | 8 ++++---- backend/src/main.rs | 6 ++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/src/cache.rs b/backend/src/cache.rs index 9ca559d..dd0a36c 100644 --- a/backend/src/cache.rs +++ b/backend/src/cache.rs @@ -1,6 +1,7 @@ use std::{ collections::HashMap, hash::Hash, + sync::Arc, time::{Duration, Instant}, }; @@ -67,7 +68,7 @@ impl CacheEntry { } #[derive(Eq, PartialEq, Hash, Debug)] -pub(crate) struct Key(Service, String, String, String); +pub(crate) struct Key(Service, Arc, Arc, Arc); #[derive(Eq, PartialEq, Hash, Debug)] pub(crate) enum Service { @@ -77,7 +78,12 @@ pub(crate) enum Service { } impl Key { - pub(crate) fn new(service: Service, user: String, repo: String, branch: String) -> Self { + pub(crate) fn new( + service: Service, + user: Arc, + repo: Arc, + branch: Arc, + ) -> Self { Key(service, user, repo, branch) } } diff --git a/backend/src/data.rs b/backend/src/data.rs index 5439e68..7f36ab0 100644 --- a/backend/src/data.rs +++ b/backend/src/data.rs @@ -8,10 +8,10 @@ pub(crate) type State = Arc>>; #[derive(Deserialize, Debug)] pub(crate) struct FilePath { - pub(crate) user: String, - pub(crate) repo: String, - pub(crate) commit: String, - pub(crate) file: String, + pub(crate) user: Arc, + pub(crate) repo: Arc, + pub(crate) commit: Arc, + pub(crate) file: Arc, } impl FilePath { diff --git a/backend/src/main.rs b/backend/src/main.rs index 36a7177..20bd518 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -7,8 +7,6 @@ extern crate serde_derive; #[macro_use] extern crate structopt; -// TODO: cow instead of string - mod cache; mod cdn; mod config; @@ -51,7 +49,7 @@ fn proxy_file( .from_err() .and_then(move |response| match response.status() { StatusCode::OK => { - let mime = mime_guess::guess_mime_type(&data.file); + let mime = mime_guess::guess_mime_type(&*data.file); Ok(HttpResponse::Ok() .content_type(mime.to_string().as_str()) .set(CacheControl(vec![ @@ -134,7 +132,7 @@ fn serve_gist( .from_err() .and_then(move |response| match response.status() { StatusCode::OK => { - let mime = mime_guess::guess_mime_type(&data.file); + let mime = mime_guess::guess_mime_type(&*data.file); Ok(HttpResponse::Ok() .content_type(mime.to_string().as_str()) .set(CacheControl(vec![