From 06bf6a17c01c729ac67464aedf9122f1ed3698a2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 6 Mar 2018 13:36:05 -0800 Subject: [PATCH] Optimize SourceId::crates_io Turns out this gets called a lot in large projects as almost all dependencies come from crates.io and parsed manifests use this. Let's cache the result as it's always the same! --- src/cargo/core/source/source_id.rs | 32 ++++++++++++++++-------------- src/cargo/util/config.rs | 11 +++++++++- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 2bac7cd01..58558771b 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -168,21 +168,23 @@ impl SourceId { /// This is the main cargo registry by default, but it can be overridden in /// a `.cargo/config`. pub fn crates_io(config: &Config) -> CargoResult { - let cfg = ops::registry_configuration(config, None)?; - let url = if let Some(ref index) = cfg.index { - static WARNED: AtomicBool = ATOMIC_BOOL_INIT; - if !WARNED.swap(true, SeqCst) { - config.shell().warn("custom registry support via \ - the `registry.index` configuration is \ - being removed, this functionality \ - will not work in the future")?; - } - &index[..] - } else { - CRATES_IO - }; - let url = url.to_url()?; - SourceId::for_registry(&url) + config.crates_io_source_id(|| { + let cfg = ops::registry_configuration(config, None)?; + let url = if let Some(ref index) = cfg.index { + static WARNED: AtomicBool = ATOMIC_BOOL_INIT; + if !WARNED.swap(true, SeqCst) { + config.shell().warn("custom registry support via \ + the `registry.index` configuration is \ + being removed, this functionality \ + will not work in the future")?; + } + &index[..] + } else { + CRATES_IO + }; + let url = url.to_url()?; + SourceId::for_registry(&url) + }) } pub fn alt_registry(config: &Config, key: &str) -> CargoResult { diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 588ad3592..927d0e132 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -19,7 +19,7 @@ use toml; use lazycell::LazyCell; use core::shell::Verbosity; -use core::{Shell, CliUnstable}; +use core::{Shell, CliUnstable, SourceId}; use ops; use url::Url; use util::ToUrl; @@ -63,6 +63,8 @@ pub struct Config { cli_flags: CliUnstable, /// A handle on curl easy mode for http calls easy: LazyCell>, + /// Cache of the `SourceId` for crates.io + crates_io_source_id: LazyCell, } impl Config { @@ -100,6 +102,7 @@ impl Config { }, cli_flags: CliUnstable::default(), easy: LazyCell::new(), + crates_io_source_id: LazyCell::new(), } } @@ -659,6 +662,12 @@ impl Config { } Ok(http) } + + pub fn crates_io_source_id(&self, f: F) -> CargoResult + where F: FnMut() -> CargoResult + { + Ok(self.crates_io_source_id.try_borrow_with(f)?.clone()) + } } #[derive(Eq, PartialEq, Clone, Copy)] -- 2.30.2