Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: try to speed up pixi global list #2609

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ inherits = "release"
lto = "fat"
opt-level = 3
strip = "symbols"
debug = false

[profile.ci]
codegen-units = 16
Expand All @@ -333,6 +334,8 @@ lto = false
opt-level = 3
strip = false

[profile.release]
debug = true

[dev-dependencies]
async-trait = { workspace = true }
Expand Down
38 changes: 38 additions & 0 deletions src/global/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@

/// Finds the package record from the `conda-meta` directory.
pub(crate) async fn find_package_records(conda_meta: &Path) -> miette::Result<Vec<PrefixRecord>> {
tracing::warn!("Loading package records from {}", conda_meta.display());
let mut read_dir = tokio_fs::read_dir(conda_meta).await.into_diagnostic()?;
let mut records = Vec::new();

Expand All @@ -202,6 +203,43 @@
Ok(records)
}

pub struct PackageIdentifier {
pub name: PackageName,
pub version: Version,
#[allow(dead_code)]
pub build_string: String,
}

// Find all JSON files and extract ArchiveIdentifiers from the filename
pub(crate) fn find_linked_packages(
conda_meta: &Path,
) -> Result<Vec<PackageIdentifier>, std::io::Error> {
let mut read_dir = fs::read_dir(conda_meta)?;
let mut records = Vec::new();

while let Some(entry) = read_dir.next() {

Check failure on line 220 in src/global/common.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

this loop could be written as a `for` loop
let entry = entry?;
// Check if the entry is a file and has a .json extension
if matches!(entry.file_type(), Ok(file_type) if file_type.is_file()) {
let file_name = entry.file_name();

if let Some(without_ext) = file_name.to_str().and_then(|s| s.strip_suffix(".json")) {
if let Some((build_string, version, name)) =
without_ext.rsplitn(3, '-').next_tuple()
{
records.push(PackageIdentifier {
name: PackageName::from_str(name).unwrap(),
version: Version::from_str(version).unwrap(),
build_string: build_string.to_owned(),
});
}
}
}
}

Ok(records)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum NotChangedReason {
AlreadyInstalled,
Expand Down
39 changes: 22 additions & 17 deletions src/global/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use pixi_consts::consts;
use pixi_spec::PixiSpec;
use rattler_conda_types::{PackageName, PackageRecord, PrefixRecord, Version};
use rattler_conda_types::{PackageName, PackageRecord, Version};
use serde::Serialize;
use std::io::Write;

use miette::{miette, IntoDiagnostic};

use crate::global::common::find_package_records;

use super::{project::ParsedEnvironment, EnvChanges, EnvState, EnvironmentName, Mapping, Project};
use super::{
common::{find_linked_packages, PackageIdentifier},
project::ParsedEnvironment,
EnvChanges, EnvState, EnvironmentName, Mapping, Project,
};

/// Sorting strategy for the package table
#[derive(clap::ValueEnum, Clone, Debug, Serialize, Default)]
Expand Down Expand Up @@ -147,7 +151,10 @@ pub async fn list_environment(
.environments()
.get(environment_name)
.ok_or_else(|| miette!("Environment {} not found", environment_name.fancy_display()))?;

println!(
"Listing packages in the {} environment",
environment_name.fancy_display()
);
let records = find_package_records(
&project
.env_root
Expand Down Expand Up @@ -214,6 +221,7 @@ pub async fn list_global_environments(
envs_changes: Option<&EnvChanges>,
regex: Option<String>,
) -> miette::Result<()> {
tracing::warn!("Foobar");
let mut project_envs = project.environments().clone();
project_envs.sort_by(|a, _, b, _| a.to_string().cmp(&b.to_string()));

Expand Down Expand Up @@ -244,7 +252,7 @@ pub async fn list_global_environments(
for (idx, (env_name, env)) in project_envs.iter().enumerate() {
let env_dir = project.env_root.path().join(env_name.as_str());
let conda_meta = env_dir.join(consts::CONDA_META_DIR);
let records = find_package_records(&conda_meta).await?;
let package_ids = find_linked_packages(&conda_meta).into_diagnostic()?;

let last = (idx + 1) == len;

Expand Down Expand Up @@ -273,15 +281,15 @@ pub async fn list_global_environments(
.iter()
.any(|(pkg_name, _spec)| pkg_name.as_normalized() != env_name.as_str())
{
if let Some(env_package) = records.iter().find(|rec| {
rec.repodata_record.package_record.name.as_normalized() == env_name.as_str()
}) {
if let Some(package) = package_ids
.iter()
.find(|pid| pid.name.as_normalized() == env_name.as_str())
{
// output the environment name and version
message.push_str(&format!(
" {}: {} {}",
env_name.fancy_display(),
console::style(env_package.repodata_record.package_record.version.clone())
.blue(),
console::style(package.version.clone()).blue(),
state
));
} else {
Expand All @@ -295,9 +303,9 @@ pub async fn list_global_environments(
if let Some(dep_message) = format_dependencies(
env_name.as_str(),
&env.dependencies,
&records,
&package_ids,
last,
!env.exposed.is_empty(),
!env.exposed().is_empty(),
) {
message.push_str(&dep_message);
}
Expand Down Expand Up @@ -340,7 +348,7 @@ fn display_dependency(name: &PackageName, version: Option<Version>) -> String {
fn format_dependencies(
env_name: &str,
dependencies: &IndexMap<PackageName, PixiSpec>,
records: &[PrefixRecord],
records: &[PackageIdentifier],
last: bool,
more: bool,
) -> Option<String> {
Expand All @@ -353,11 +361,8 @@ fn format_dependencies(
.map(|(name, _spec)| {
let version = records
.iter()
.find(|rec| {
rec.repodata_record.package_record.name.as_normalized()
== name.as_normalized()
})
.map(|rec| rec.repodata_record.package_record.version.version().clone());
.find(|pid| pid.name.as_normalized() == name.as_normalized())
.map(|pid| pid.version.clone());
display_dependency(name, version)
})
.join(", ");
Expand Down
2 changes: 1 addition & 1 deletion src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Prefix {
}
}
}

tracing::warn!("Parsing '{}'", path.display());
// Spawn loading on another thread
let future = tokio::task::spawn_blocking(move || {
PrefixRecord::from_path(&path)
Expand Down
Loading