Skip to content

Commit

Permalink
chore: Rename Config to GlobalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Feb 20, 2024
1 parent 3209eae commit 305efa6
Show file tree
Hide file tree
Showing 171 changed files with 3,251 additions and 3,178 deletions.
59 changes: 29 additions & 30 deletions benches/benchsuite/benches/global_cache_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker};
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::interning::InternedString;
use cargo::util::Config;
use cargo::util::GlobalContext;
use criterion::{criterion_group, criterion_main, Criterion};
use std::fs;
use std::path::{Path, PathBuf};
Expand All @@ -25,63 +25,62 @@ fn cargo_home() -> PathBuf {
p
}

fn initialize_config() -> Config {
fn initialize_context() -> GlobalContext {
// Set up config.
let shell = cargo::core::Shell::new();
let homedir = cargo_home();
if !homedir.exists() {
fs::create_dir_all(&homedir).unwrap();
}
let cwd = homedir.clone();
let mut config = Config::new(shell, cwd, homedir);
config.nightly_features_allowed = true;
config.set_search_stop_path(root());
config
.configure(
0,
false,
None,
false,
false,
false,
&None,
&["gc".to_string()],
&[],
)
.unwrap();
let mut gctx = GlobalContext::new(shell, cwd, homedir);
gctx.nightly_features_allowed = true;
gctx.set_search_stop_path(root());
gctx.configure(
0,
false,
None,
false,
false,
false,
&None,
&["gc".to_string()],
&[],
)
.unwrap();
// Set up database sample.
let db_path = GlobalCacheTracker::db_path(&config).into_path_unlocked();
let db_path = GlobalCacheTracker::db_path(&gctx).into_path_unlocked();
if db_path.exists() {
fs::remove_file(&db_path).unwrap();
}
let sample = Path::new(env!("CARGO_MANIFEST_DIR")).join(GLOBAL_CACHE_SAMPLE);
fs::copy(sample, &db_path).unwrap();
config
gctx
}

/// Benchmarks how long it takes to initialize `GlobalCacheTracker` with an already
/// existing full database.
fn global_tracker_init(c: &mut Criterion) {
let config = initialize_config();
let _lock = config
let gctx = initialize_context();
let _lock = gctx
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)
.unwrap();
c.bench_function("global_tracker_init", |b| {
b.iter(|| {
GlobalCacheTracker::new(&config).unwrap();
GlobalCacheTracker::new(&gctx).unwrap();
})
});
}

/// Benchmarks how long it takes to save a `GlobalCacheTracker` when there are zero
/// updates.
fn global_tracker_empty_save(c: &mut Criterion) {
let config = initialize_config();
let _lock = config
let gctx = initialize_context();
let _lock = gctx
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)
.unwrap();
let mut deferred = DeferredGlobalLastUse::new();
let mut tracker = GlobalCacheTracker::new(&config).unwrap();
let mut tracker = GlobalCacheTracker::new(&gctx).unwrap();

c.bench_function("global_tracker_empty_save", |b| {
b.iter(|| {
Expand Down Expand Up @@ -112,12 +111,12 @@ fn load_random_sample() -> Vec<(InternedString, InternedString, u64)> {
/// This runs for different sizes of number of crates to update (selecting
/// from the random sample stored on disk).
fn global_tracker_update(c: &mut Criterion) {
let config = initialize_config();
let _lock = config
let gctx = initialize_context();
let _lock = gctx
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)
.unwrap();
let sample = Path::new(env!("CARGO_MANIFEST_DIR")).join(GLOBAL_CACHE_SAMPLE);
let db_path = GlobalCacheTracker::db_path(&config).into_path_unlocked();
let db_path = GlobalCacheTracker::db_path(&gctx).into_path_unlocked();

let random_sample = load_random_sample();

Expand All @@ -129,7 +128,7 @@ fn global_tracker_update(c: &mut Criterion) {

fs::copy(&sample, &db_path).unwrap();
let mut deferred = DeferredGlobalLastUse::new();
let mut tracker = GlobalCacheTracker::new(&config).unwrap();
let mut tracker = GlobalCacheTracker::new(&gctx).unwrap();
group.bench_with_input(size.to_string(), &size, |b, &size| {
b.iter(|| {
for (encoded_registry_name, name, size) in &random_sample[..size] {
Expand Down
22 changes: 11 additions & 11 deletions benches/benchsuite/benches/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ use cargo::core::resolver::features::{FeatureOpts, FeatureResolver};
use cargo::core::resolver::{CliFeatures, ForceAllTargets, HasDevUnits, ResolveBehavior};
use cargo::core::{PackageIdSpec, Workspace};
use cargo::ops::WorkspaceResolve;
use cargo::Config;
use cargo::GlobalContext;
use criterion::{criterion_group, criterion_main, Criterion};
use std::path::Path;

struct ResolveInfo<'cfg> {
ws: Workspace<'cfg>,
struct ResolveInfo<'gctx> {
ws: Workspace<'gctx>,
requested_kinds: [CompileKind; 1],
target_data: RustcTargetData<'cfg>,
target_data: RustcTargetData<'gctx>,
cli_features: CliFeatures,
specs: Vec<PackageIdSpec>,
has_dev_units: HasDevUnits,
force_all_targets: ForceAllTargets,
ws_resolve: WorkspaceResolve<'cfg>,
ws_resolve: WorkspaceResolve<'gctx>,
}

/// Helper for resolving a workspace. This will run the resolver once to
/// download everything, and returns all the data structures that are used
/// during resolution.
fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
fn do_resolve<'gctx>(gctx: &'gctx GlobalContext, ws_root: &Path) -> ResolveInfo<'gctx> {
let requested_kinds = [CompileKind::Host];
let ws = Workspace::new(&ws_root.join("Cargo.toml"), config).unwrap();
let ws = Workspace::new(&ws_root.join("Cargo.toml"), gctx).unwrap();
let mut target_data = RustcTargetData::new(&ws, &requested_kinds).unwrap();
let cli_features = CliFeatures::from_command_line(&[], false, true).unwrap();
let pkgs = cargo::ops::Packages::Default;
Expand Down Expand Up @@ -62,7 +62,7 @@ fn resolve_ws(c: &mut Criterion) {
let fixtures = fixtures!();
let mut group = c.benchmark_group("resolve_ws");
for (ws_name, ws_root) in fixtures.workspaces() {
let config = fixtures.make_config(&ws_root);
let gctx = fixtures.make_context(&ws_root);
// The resolver info is initialized only once in a lazy fashion. This
// allows criterion to skip this workspace if the user passes a filter
// on the command-line (like `cargo bench -- resolve_ws/tikv`).
Expand All @@ -81,7 +81,7 @@ fn resolve_ws(c: &mut Criterion) {
has_dev_units,
force_all_targets,
..
} = lazy_info.get_or_insert_with(|| do_resolve(&config, &ws_root));
} = lazy_info.get_or_insert_with(|| do_resolve(&gctx, &ws_root));
b.iter(|| {
cargo::ops::resolve_ws_with_opts(
ws,
Expand All @@ -104,7 +104,7 @@ fn feature_resolver(c: &mut Criterion) {
let fixtures = fixtures!();
let mut group = c.benchmark_group("feature_resolver");
for (ws_name, ws_root) in fixtures.workspaces() {
let config = fixtures.make_config(&ws_root);
let gctx = fixtures.make_context(&ws_root);
let mut lazy_info = None;
group.bench_function(&ws_name, |b| {
let ResolveInfo {
Expand All @@ -116,7 +116,7 @@ fn feature_resolver(c: &mut Criterion) {
has_dev_units,
ws_resolve,
..
} = lazy_info.get_or_insert_with(|| do_resolve(&config, &ws_root));
} = lazy_info.get_or_insert_with(|| do_resolve(&gctx, &ws_root));
b.iter(|| {
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, *has_dev_units);
FeatureResolver::resolve(
Expand Down
4 changes: 2 additions & 2 deletions benches/benchsuite/benches/workspace_initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ fn workspace_initialization(c: &mut Criterion) {
let fixtures = fixtures!();
let mut group = c.benchmark_group("workspace_initialization");
for (ws_name, ws_root) in fixtures.workspaces() {
let config = fixtures.make_config(&ws_root);
let gctx = fixtures.make_context(&ws_root);
// The resolver info is initialized only once in a lazy fashion. This
// allows criterion to skip this workspace if the user passes a filter
// on the command-line (like `cargo bench -- workspace_initialization/tikv`).
group.bench_function(ws_name, |b| {
b.iter(|| Workspace::new(&ws_root.join("Cargo.toml"), &config).unwrap())
b.iter(|| Workspace::new(&ws_root.join("Cargo.toml"), &gctx).unwrap())
});
}
group.finish();
Expand Down
35 changes: 17 additions & 18 deletions benches/benchsuite/src/bin/capture-last-use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker};
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::interning::InternedString;
use cargo::Config;
use cargo::GlobalContext;
use rand::prelude::SliceRandom;
use std::collections::HashMap;
use std::fs;
Expand All @@ -28,30 +28,29 @@ fn main() {
let shell = cargo::core::Shell::new();
let homedir = Path::new(env!("CARGO_MANIFEST_DIR")).join("global-cache-tracker");
let cwd = homedir.clone();
let mut config = Config::new(shell, cwd, homedir.clone());
config
.configure(
0,
false,
None,
false,
false,
false,
&None,
&["gc".to_string()],
&[],
)
.unwrap();
let db_path = GlobalCacheTracker::db_path(&config).into_path_unlocked();
let mut gctx = GlobalContext::new(shell, cwd, homedir.clone());
gctx.configure(
0,
false,
None,
false,
false,
false,
&None,
&["gc".to_string()],
&[],
)
.unwrap();
let db_path = GlobalCacheTracker::db_path(&gctx).into_path_unlocked();
if db_path.exists() {
fs::remove_file(&db_path).unwrap();
}

let _lock = config
let _lock = gctx
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)
.unwrap();
let mut deferred = DeferredGlobalLastUse::new();
let mut tracker = GlobalCacheTracker::new(&config).unwrap();
let mut tracker = GlobalCacheTracker::new(&gctx).unwrap();

let real_home = cargo::util::homedir(&std::env::current_dir().unwrap()).unwrap();

Expand Down
35 changes: 17 additions & 18 deletions benches/benchsuite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::disallowed_methods)]

use cargo::Config;
use cargo::GlobalContext;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -175,25 +175,24 @@ impl Fixtures {
.collect()
}

/// Creates a new Config.
pub fn make_config(&self, ws_root: &Path) -> Config {
/// Creates a new Context.
pub fn make_context(&self, ws_root: &Path) -> GlobalContext {
let shell = cargo::core::Shell::new();
let mut config = Config::new(shell, ws_root.to_path_buf(), self.cargo_home());
let mut gctx = GlobalContext::new(shell, ws_root.to_path_buf(), self.cargo_home());
// Configure is needed to set the target_dir which is needed to write
// the .rustc_info.json file which is very expensive.
config
.configure(
0,
false,
None,
false,
false,
false,
&Some(self.target_dir()),
&[],
&[],
)
.unwrap();
config
gctx.configure(
0,
false,
None,
false,
false,
false,
&Some(self.target_dir()),
&[],
&[],
)
.unwrap();
gctx
}
}
24 changes: 12 additions & 12 deletions crates/mdman/src/hbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl HelperDef for OptionHelper<'_> {
&self,
h: &Helper<'rc>,
r: &'reg Handlebars<'reg>,
ctx: &'rc Context,
gctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> HelperResult {
Expand Down Expand Up @@ -137,10 +137,10 @@ impl HelperDef for OptionHelper<'_> {
}
};
// Render the block.
let block = t.renders(r, ctx, rc)?;
let block = t.renders(r, gctx, rc)?;

// Get the name of this page.
let man_name = ctx
let man_name = gctx
.data()
.get("man_name")
.expect("expected man_name in context")
Expand All @@ -167,7 +167,7 @@ impl HelperDef for ManLinkHelper<'_> {
&self,
h: &Helper<'rc>,
_r: &'reg Handlebars<'reg>,
_ctx: &'rc Context,
_gctx: &'rc Context,
_rc: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> HelperResult {
Expand Down Expand Up @@ -200,7 +200,7 @@ impl HelperDef for ManLinkHelper<'_> {
fn set_decorator(
d: &Decorator<'_>,
_: &Handlebars<'_>,
_ctx: &Context,
_gctx: &Context,
rc: &mut RenderContext<'_, '_>,
) -> Result<(), RenderError> {
let data_to_set = d.hash();
Expand All @@ -212,25 +212,25 @@ fn set_decorator(

/// Sets a variable to a value within the context.
fn set_in_context(rc: &mut RenderContext<'_, '_>, key: &str, value: serde_json::Value) {
let mut ctx = match rc.context() {
let mut gctx = match rc.context() {
Some(c) => (*c).clone(),
None => Context::wraps(serde_json::Value::Object(serde_json::Map::new())).unwrap(),
};
if let serde_json::Value::Object(m) = ctx.data_mut() {
if let serde_json::Value::Object(m) = gctx.data_mut() {
m.insert(key.to_string(), value);
rc.set_context(ctx);
rc.set_context(gctx);
} else {
panic!("expected object in context");
}
}

/// Removes a variable from the context.
fn remove_from_context(rc: &mut RenderContext<'_, '_>, key: &str) {
let ctx = rc.context().expect("cannot remove from null context");
let mut ctx = (*ctx).clone();
if let serde_json::Value::Object(m) = ctx.data_mut() {
let gctx = rc.context().expect("cannot remove from null context");
let mut gctx = (*gctx).clone();
if let serde_json::Value::Object(m) = gctx.data_mut() {
m.remove(key);
rc.set_context(ctx);
rc.set_context(gctx);
} else {
panic!("expected object in context");
}
Expand Down
Loading

0 comments on commit 305efa6

Please sign in to comment.