From ee99c59319a53279426ec9e7dfaf8f846bd3a4b6 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 7 Aug 2019 20:40:39 +0200 Subject: [PATCH 1/2] Enable rc feature --- backend/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index e9b5c95..b294c0c 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -15,7 +15,7 @@ lazy_static = "1.3.0" mime_guess = "1.8.7" openssl-probe = "0.1.2" pretty_env_logger = "0.3.0" -serde = "1.0.97" +serde = { version = "1.0.97", features = ["rc"] } serde_derive = "1.0.97" serde_json = "1.0.40" structopt = "0.2.18" From 2cb49c68759da1c5e49fe2815f77177b69123670 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 7 Aug 2019 20:41:01 +0200 Subject: [PATCH 2/2] 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![