Skip to content

Commit 475c344

Browse files
authored
Merge pull request #77 from sansing/perf/optimise-memory-allocations
Optimise memory allocations
2 parents 43437ac + 7188299 commit 475c344

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Cargo.lock

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ http = "0.2.9"
2222
hyper = { version = "0.14.26", features = ["full"] }
2323
itertools = "0.10.5"
2424
maplit = "1.0.2"
25+
mimalloc = "0.1.46"
2526
pact_matching = "~1.1.0"
2627
pact_verifier = "~1.0.0"
2728
pact_models = "~1.1.2"

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ use std::str::FromStr;
9090

9191
use clap::{Command, Arg, ArgMatches, ArgAction, command, crate_version};
9292
use clap::error::ErrorKind;
93+
use mimalloc::MiMalloc;
9394
use pact_models::prelude::*;
9495
use regex::Regex;
9596
use tracing::{debug, error, info, warn};
@@ -103,6 +104,9 @@ mod pact_support;
103104
mod server;
104105
mod loading;
105106

107+
#[global_allocator]
108+
static GLOBAL: MiMalloc = MiMalloc;
109+
106110
#[tokio::main]
107111
async fn main() -> Result<(), ExitCode> {
108112
let args: Vec<String> = env::args().collect();

src/server.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::future::{Ready, ready};
22
use std::pin::Pin;
33
use std::process::ExitCode;
4+
use std::sync::Arc;
45

56
use anyhow::anyhow;
67
use futures::executor::block_on;
@@ -29,6 +30,10 @@ use crate::{pact_support, PactSource};
2930

3031
#[derive(Clone)]
3132
pub struct ServerHandler {
33+
shared: Arc<Shared>,
34+
}
35+
36+
pub struct Shared {
3237
sources: Vec<(V4Pact, PactSource)>,
3338
auto_cors: bool,
3439
cors_referer: bool,
@@ -45,7 +50,7 @@ struct ServerHandlerFactory {
4550
impl ServerHandlerFactory {
4651
pub fn new(handler: ServerHandler) -> Self {
4752
ServerHandlerFactory {
48-
inner: handler
53+
inner: handler,
4954
}
5055
}
5156
}
@@ -79,12 +84,14 @@ impl ServerHandler {
7984
empty_provider_states: bool
8085
) -> ServerHandler {
8186
ServerHandler {
82-
sources,
83-
auto_cors,
84-
cors_referer,
85-
provider_state,
86-
provider_state_header_name,
87-
empty_provider_states
87+
shared: Arc::new(Shared {
88+
sources,
89+
auto_cors,
90+
cors_referer,
91+
provider_state,
92+
provider_state_header_name,
93+
empty_provider_states
94+
})
8895
}
8996
}
9097

@@ -118,12 +125,13 @@ impl Service<HyperRequest<Body>> for ServerHandler {
118125
}
119126

120127
fn call(&mut self, req: HyperRequest<Body>) -> Self::Future {
121-
let auto_cors = self.auto_cors;
122-
let cors_referer = self.cors_referer;
123-
let sources = self.sources.clone();
124-
let provider_state = self.provider_state.clone();
125-
let provider_state_header_name = self.provider_state_header_name.clone();
126-
let empty_provider_states = self.empty_provider_states;
128+
let shared = self.shared.as_ref();
129+
let auto_cors = shared.auto_cors;
130+
let cors_referer = shared.cors_referer;
131+
let sources = shared.sources.clone();
132+
let provider_state = shared.provider_state.clone();
133+
let provider_state_header_name = shared.provider_state_header_name.clone();
134+
let empty_provider_states = shared.empty_provider_states;
127135

128136
Box::pin(async move {
129137
let (parts, body) = req.into_parts();

0 commit comments

Comments
 (0)