Use arc to minimize clone overhead

This commit is contained in:
Valentin Brandl 2019-08-07 20:41:01 +02:00
parent ee99c59319
commit 2cb49c6875
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,7 @@
use std::{
collections::HashMap,
hash::Hash,
sync::Arc,
time::{Duration, Instant},
};
@ -67,7 +68,7 @@ impl<T> CacheEntry<T> {
}
#[derive(Eq, PartialEq, Hash, Debug)]
pub(crate) struct Key(Service, String, String, String);
pub(crate) struct Key(Service, Arc<String>, Arc<String>, Arc<String>);
#[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<String>,
repo: Arc<String>,
branch: Arc<String>,
) -> Self {
Key(service, user, repo, branch)
}
}

View File

@ -8,10 +8,10 @@ pub(crate) type State = Arc<RwLock<Cache<Key, String>>>;
#[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<String>,
pub(crate) repo: Arc<String>,
pub(crate) commit: Arc<String>,
pub(crate) file: Arc<String>,
}
impl FilePath {

View File

@ -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<T: Service>(
.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![