some optimization using rustc-hash, parking_lot and tikv-jemallocator

This commit is contained in:
Jun Kurihara 2022-06-28 16:55:07 -04:00
commit 3e850e8410
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
5 changed files with 27 additions and 25 deletions

View file

@ -12,8 +12,7 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
default = ["tls"] default = []
tls = ["tokio-rustls", "rustls-pemfile"]
[dependencies] [dependencies]
anyhow = "1.0.58" anyhow = "1.0.58"
@ -27,7 +26,6 @@ hyper = { version = "0.14.19", default-features = false, features = [
"stream", "stream",
] } ] }
log = "0.4.17" log = "0.4.17"
mimalloc = { version = "0.1.29", default-features = false }
tokio = { version = "1.19.2", features = [ tokio = { version = "1.19.2", features = [
"net", "net",
"rt-multi-thread", "rt-multi-thread",
@ -36,13 +34,12 @@ tokio = { version = "1.19.2", features = [
"sync", "sync",
"macros", "macros",
] } ] }
tokio-rustls = { version = "0.23.4", features = [ tokio-rustls = { version = "0.23.4", features = ["early-data"] }
"early-data", rustls-pemfile = { version = "1.0.0" }
], optional = true }
rustls-pemfile = { version = "1.0.0", optional = true }
rustls = "0.20.6" rustls = "0.20.6"
rand = "0.8.5" rand = "0.8.5"
toml = "0.5.9" toml = "0.5.9"
rustc-hash = "1.1.0"
serde = { version = "1.0.137", features = ["derive"] } serde = { version = "1.0.137", features = ["derive"] }
hyper-rustls = { version = "0.23.0", default-features = false, features = [ hyper-rustls = { version = "0.23.0", default-features = false, features = [
"tokio-runtime", "tokio-runtime",
@ -50,6 +47,7 @@ hyper-rustls = { version = "0.23.0", default-features = false, features = [
"http1", "http1",
"http2", "http2",
] } ] }
parking_lot = "0.12.1"
# hyper-trust-dns = { version = "0.4.2", default-features = false, features = [ # hyper-trust-dns = { version = "0.4.2", default-features = false, features = [
# "rustls-http2", # "rustls-http2",
# "dnssec-ring", # "dnssec-ring",
@ -59,6 +57,10 @@ hyper-rustls = { version = "0.23.0", default-features = false, features = [
# "native-tls", # "native-tls",
# ] } # ] }
[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5.0"
[dev-dependencies] [dev-dependencies]

View file

@ -1,13 +1,14 @@
use crate::log::*; use crate::log::*;
use parking_lot::Mutex;
use rand::Rng; use rand::Rng;
use rustc_hash::FxHashMap as HashMap;
use std::{ use std::{
collections::HashMap,
fs::File, fs::File,
io::{self, BufReader, Cursor, Read}, io::{self, BufReader, Cursor, Read},
path::PathBuf, path::PathBuf,
sync::{ sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Mutex, Arc,
}, },
}; };
use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig}; use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig};
@ -88,12 +89,10 @@ impl Upstream {
impl Backend { impl Backend {
pub fn get_tls_server_config(&self) -> Option<ServerConfig> { pub fn get_tls_server_config(&self) -> Option<ServerConfig> {
let lock = self.server_config.lock(); let lock = self.server_config.lock();
if let Ok(opt) = lock { let opt_clone = lock.clone();
let opt_clone = opt.clone();
if let Some(sc) = opt_clone { if let Some(sc) = opt_clone {
return Some(sc); return Some(sc);
} }
}
None None
} }
pub async fn update_server_config(&self) -> io::Result<()> { pub async fn update_server_config(&self) -> io::Result<()> {
@ -183,11 +182,8 @@ impl Backend {
})?; })?;
server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
if let Ok(mut config_store) = self.server_config.lock() { let mut config_store = self.server_config.lock();
*config_store = Some(server_config); *config_store = Some(server_config);
} else {
error!("Some thing wrong to write into mutex")
}
// server_config; // server_config;
Ok(()) Ok(())

View file

@ -1,8 +1,9 @@
use super::toml::{ConfigToml, ReverseProxyOption}; use super::toml::{ConfigToml, ReverseProxyOption};
use crate::{backend::*, constants::*, error::*, globals::*, log::*}; use crate::{backend::*, constants::*, error::*, globals::*, log::*};
use clap::Arg; use clap::Arg;
use parking_lot::Mutex;
use rustc_hash::FxHashMap as HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::{collections::HashMap, sync::Mutex};
// #[cfg(feature = "tls")] // #[cfg(feature = "tls")]
use std::path::PathBuf; use std::path::PathBuf;
@ -147,7 +148,7 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()>
} }
fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy> { fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy> {
let mut upstream: HashMap<String, Upstream> = HashMap::new(); let mut upstream: HashMap<String, Upstream> = HashMap::default();
let mut default_upstream: Option<Upstream> = None; let mut default_upstream: Option<Upstream> = None;
rp_settings.iter().for_each(|rpo| { rp_settings.iter().for_each(|rpo| {
let elem = Upstream { let elem = Upstream {

View file

@ -1,6 +1,7 @@
use crate::error::*; use crate::error::*;
use rustc_hash::FxHashMap as HashMap;
use serde::Deserialize; use serde::Deserialize;
use std::{collections::HashMap, fs}; use std::fs;
#[derive(Deserialize, Debug, Default)] #[derive(Deserialize, Debug, Default)]
pub struct ConfigToml { pub struct ConfigToml {

View file

@ -1,5 +1,6 @@
#[cfg(not(target_env = "msvc"))]
#[global_allocator] #[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
mod backend; mod backend;
mod config; mod config;
@ -21,7 +22,8 @@ use crate::{
use futures::future::select_all; use futures::future::select_all;
use hyper::Client; use hyper::Client;
// use hyper_trust_dns::TrustDnsResolver; // use hyper_trust_dns::TrustDnsResolver;
use std::{collections::HashMap, io::Write, sync::Arc}; use rustc_hash::FxHashMap as HashMap;
use std::{io::Write, sync::Arc};
use tokio::time::Duration; use tokio::time::Duration;
fn main() { fn main() {
@ -63,7 +65,7 @@ fn main() {
let mut backends = Backends { let mut backends = Backends {
default_app: None, default_app: None,
apps: HashMap::<String, Backend>::new(), apps: HashMap::<String, Backend>::default(),
}; };
let _ = parse_opts(&mut globals, &mut backends).expect("Invalid configuration"); let _ = parse_opts(&mut globals, &mut backends).expect("Invalid configuration");