Optimize SourceId::crates_io
authorAlex Crichton <alex@alexcrichton.com>
Tue, 6 Mar 2018 21:36:05 +0000 (13:36 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 6 Mar 2018 21:36:05 +0000 (13:36 -0800)
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
src/cargo/util/config.rs

index 2bac7cd015dee33313a5f133a00bb4ad435aef5e..58558771bc8555d7d26192f975a79b4b6aca0013 100644 (file)
@@ -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<SourceId> {
-        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<SourceId> {
index 588ad3592f29b13f5dce476a384ae77c67c5f4d2..927d0e132f19ec2d20109d8819623a9a58e95f05 100644 (file)
@@ -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<RefCell<Easy>>,
+    /// Cache of the `SourceId` for crates.io
+    crates_io_source_id: LazyCell<SourceId>,
 }
 
 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<F>(&self, f: F) -> CargoResult<SourceId>
+        where F: FnMut() -> CargoResult<SourceId>
+    {
+        Ok(self.crates_io_source_id.try_borrow_with(f)?.clone())
+    }
 }
 
 #[derive(Eq, PartialEq, Clone, Copy)]