Skip to content

Commit

Permalink
[cargo-nextest] make version computation less expensive
Browse files Browse the repository at this point in the history
Realized that os_info would do a ton of calls that aren't necessary, slowing
down nextest quite measurably. It's hard to integrate lazy version evaluation
into clap (clap-rs/clap#5543) so let's just not print
OS info for now.

If we do want OS info in the future it'll probably be as part of a support
tarball or similar.
  • Loading branch information
sunshowers committed Sep 29, 2024
1 parent 2ff3dfb commit 5edd32b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ nextest-metadata = { version = "0.12.1", path = "nextest-metadata" }
nextest-workspace-hack = "0.1.0"
nix = { version = "0.29.0", default-features = false, features = ["signal"] }
once_cell = "1.19.0"
os_info = "3.8.2"
owo-colors = "4.1.0"
pathdiff = { version = "0.2.1", features = ["camino"] }
pin-project-lite = "0.2.14"
Expand Down
3 changes: 1 addition & 2 deletions cargo-nextest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version.workspace = true
[dependencies]
camino.workspace = true
cfg-if.workspace = true
clap = { workspace = true, features = ["derive", "env", "string", "unicode", "wrap_help"] }
clap = { workspace = true, features = ["derive", "env", "unicode", "wrap_help"] }
color-eyre.workspace = true
dialoguer.workspace = true
duct.workspace = true
Expand All @@ -31,7 +31,6 @@ nextest-metadata = { version = "=0.12.1", path = "../nextest-metadata" }
nextest-runner = { version = "=0.61.0", path = "../nextest-runner" }
nextest-workspace-hack.workspace = true
once_cell.workspace = true
os_info.workspace = true
owo-colors.workspace = true
pathdiff.workspace = true
quick-junit.workspace = true
Expand Down
11 changes: 5 additions & 6 deletions cargo-nextest/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::{
cargo_cli::{CargoCli, CargoOptions},
output::{should_redact, OutputContext, OutputOpts, OutputWriter, StderrStyles},
reuse_build::{make_path_mapper, ArchiveFormatOpt, ReuseBuildOpts},
version::VersionInfo,
ExpectedError, Result, ReuseBuildKind,
version, ExpectedError, Result, ReuseBuildKind,
};
use camino::{Utf8Path, Utf8PathBuf};
use clap::{builder::BoolishValueParser, ArgAction, Args, Parser, Subcommand, ValueEnum};
Expand Down Expand Up @@ -66,8 +65,8 @@ use swrite::{swrite, SWrite};
/// this message will not be seen), not `cargo-nextest`.
#[derive(Debug, Parser)]
#[command(
version = VersionInfo::new().to_short_string(),
long_version = VersionInfo::new().to_long_string(),
version = version::short(),
long_version = version::long(),
bin_name = "cargo",
styles = crate::output::clap_styles::style(),
max_term_width = 100,
Expand Down Expand Up @@ -122,8 +121,8 @@ enum NextestSubcommand {

#[derive(Debug, Args)]
#[clap(
version = VersionInfo::new().to_short_string(),
long_version = VersionInfo::new().to_long_string(),
version = version::short(),
long_version = version::long(),
display_name = "cargo-nextest",
)]
struct AppOpts {
Expand Down
46 changes: 37 additions & 9 deletions cargo-nextest/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use once_cell::sync::Lazy;
use std::fmt::Write;

pub(crate) struct VersionInfo {
pub(crate) fn short() -> &'static str {
&VERSION_INFO.short
}

pub(crate) fn long() -> &'static str {
&VERSION_INFO.long
}

// All the data here is static so we can use a singleton.
static VERSION_INFO: Lazy<VersionInfo> = Lazy::new(|| {
let inner = VersionInfoInner::new();
let short = inner.to_short();
let long = inner.to_long();
VersionInfo { short, long }
});

struct VersionInfo {
short: String,
long: String,
}

struct VersionInfoInner {
/// Nextest's version.
version: &'static str,

/// Information about the Git repository that nextest was built from.
/// Information about the repository that nextest was built from.
///
/// `None` if the Git repository information is not available.
/// `None` if the repository information is not available.
commit_info: Option<CommitInfo>,
}

impl VersionInfo {
pub(crate) const fn new() -> Self {
impl VersionInfoInner {
const fn new() -> Self {
Self {
version: env!("CARGO_PKG_VERSION"),
commit_info: CommitInfo::from_env(),
}
}

pub(crate) fn to_short_string(&self) -> String {
pub(crate) fn to_short(&self) -> String {
let mut s = self.version.to_string();

if let Some(commit_info) = &self.commit_info {
Expand All @@ -36,16 +58,22 @@ impl VersionInfo {
s
}

pub(crate) fn to_long_string(&self) -> String {
let mut s = self.to_short_string();
pub(crate) fn to_long(&self) -> String {
let mut s = self.to_short();
write!(s, "\nrelease: {}", self.version).unwrap();

if let Some(commit_info) = &self.commit_info {
write!(s, "\ncommit-hash: {}", commit_info.commit_hash).unwrap();
write!(s, "\ncommit-date: {}", commit_info.commit_date).unwrap();
}
write!(s, "\nhost: {}", env!("NEXTEST_BUILD_HOST_TARGET")).unwrap();
write!(s, "\nos: {}", os_info::get()).unwrap();

// rustc and cargo's version also prints host info here. Unfortunately, clap 4.0's version
// support only supports a static string rather than a callback, so version info computation
// can't be deferred. OS info is also quite expensive to compute.
//
// For now, just don't do this -- everything else can be statically computed. We'll probably
// add a dedicated command to collect support data if it's necessary.

s
}
Expand Down
6 changes: 3 additions & 3 deletions workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ publish = false
[dependencies]
backtrace = { version = "0.3.71", features = ["gimli-symbolize"] }
camino = { version = "1.1.9", default-features = false, features = ["serde1"] }
clap = { version = "4.5.18", features = ["derive", "env", "string", "unicode", "wrap_help"] }
clap_builder = { version = "4.5.18", default-features = false, features = ["color", "env", "std", "string", "suggestions", "unicode", "usage", "wrap_help"] }
clap = { version = "4.5.18", features = ["derive", "env", "unicode", "wrap_help"] }
clap_builder = { version = "4.5.18", default-features = false, features = ["color", "env", "std", "suggestions", "unicode", "usage", "wrap_help"] }
console = { version = "0.15.8" }
either = { version = "1.13.0" }
getrandom = { version = "0.2.15", default-features = false, features = ["std"] }
Expand Down Expand Up @@ -74,6 +74,6 @@ futures-sink = { version = "0.3.30", default-features = false, features = ["std"
smallvec = { version = "1.13.2", default-features = false, features = ["const_new"] }
tokio = { version = "1.40.0", default-features = false, features = ["io-std", "net"] }
windows-sys-73dcd821b1037cfd = { package = "windows-sys", version = "0.59.0", features = ["Win32_Globalization", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Console", "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_Threading"] }
windows-sys-b21d60becc0929df = { package = "windows-sys", version = "0.52.0", features = ["Wdk_Foundation", "Wdk_Storage_FileSystem", "Wdk_System_IO", "Win32_Foundation", "Win32_Networking_WinSock", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Com", "Win32_System_Console", "Win32_System_Environment", "Win32_System_IO", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Pipes", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_SystemServices", "Win32_System_Threading", "Win32_System_WindowsProgramming", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] }
windows-sys-b21d60becc0929df = { package = "windows-sys", version = "0.52.0", features = ["Wdk_Foundation", "Wdk_Storage_FileSystem", "Wdk_System_IO", "Win32_Foundation", "Win32_Networking_WinSock", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Com", "Win32_System_Console", "Win32_System_Environment", "Win32_System_IO", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Pipes", "Win32_System_SystemServices", "Win32_System_Threading", "Win32_System_WindowsProgramming", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Shell"] }

### END HAKARI SECTION

0 comments on commit 5edd32b

Please sign in to comment.