Skip to content

Commit 7c548f4

Browse files
committed
Auto merge of #3560 - jtgeibel:avoid-cloning-config, r=Turbo87
Avoid cloning `Config` struct This struct contains a lot of owned data and cloning the entire struct is unnecessary. r? `@Turbo87`
2 parents 3ad849e + b91aa29 commit 7c548f4

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/admin/render_readmes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
schema::{crates, readme_renderings, versions},
66
Config,
77
};
8-
use std::{io::Read, path::Path, thread};
8+
use std::{io::Read, path::Path, sync::Arc, thread};
99

1010
use chrono::{TimeZone, Utc};
1111
use clap::Clap;
@@ -38,7 +38,7 @@ pub struct Opts {
3838
}
3939

4040
pub fn run(opts: Opts) {
41-
let config = Config::default();
41+
let config = Arc::new(Config::default());
4242
let conn = db::connect_now().unwrap();
4343

4444
let start_time = Utc::now();

src/bin/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3535
tracing_subscriber::fmt::init();
3636

3737
let config = cargo_registry::Config::default();
38+
let env = config.env;
3839
let client = Client::new();
39-
let app = Arc::new(App::new(config.clone(), Some(client)));
40+
let app = Arc::new(App::new(config, Some(client)));
4041

4142
// Start the background thread periodically persisting download counts to the database.
4243
downloads_counter_thread(app.clone());
@@ -63,7 +64,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6364
let threads = dotenv::var("SERVER_THREADS")
6465
.map(|s| s.parse().expect("SERVER_THREADS was not a valid number"))
6566
.unwrap_or_else(|_| {
66-
if config.env == Env::Development {
67+
if env == Env::Development {
6768
5
6869
} else {
6970
// A large default because this can be easily changed via env and in production we

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::publish_rate_limit::PublishRateLimit;
22
use crate::{env, uploaders::Uploader, Env, Replica};
33

4-
#[derive(Clone, Debug)]
4+
#[derive(Debug)]
55
pub struct Config {
66
pub uploader: Uploader,
77
pub session_key: String,
@@ -24,7 +24,7 @@ pub struct Config {
2424
pub metrics_authorization_token: Option<String>,
2525
}
2626

27-
#[derive(Clone, Debug)]
27+
#[derive(Debug)]
2828
pub struct DbPoolConfig {
2929
pub url: String,
3030
pub read_only_mode: bool,

src/middleware.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ use crate::{App, Env};
4040

4141
pub fn build_middleware(app: Arc<App>, endpoints: RouteBuilder) -> MiddlewareBuilder {
4242
let mut m = MiddlewareBuilder::new(endpoints);
43-
let config = app.config.clone();
44-
let env = config.env;
43+
let env = app.config.env;
44+
let blocked_traffic = app.config.blocked_traffic.clone();
4545

4646
if env != Env::Test {
4747
m.add(ensure_well_formed_500::EnsureWellFormed500);
@@ -111,7 +111,7 @@ pub fn build_middleware(app: Arc<App>, endpoints: RouteBuilder) -> MiddlewareBui
111111

112112
m.around(Head::default());
113113

114-
for (header, blocked_values) in config.blocked_traffic {
114+
for (header, blocked_values) in blocked_traffic {
115115
m.around(block_traffic::BlockTraffic::new(header, blocked_values));
116116
}
117117

0 commit comments

Comments
 (0)