Skip to content

Commit

Permalink
revert: move large SparseRepoData extraction move to rust ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
Wackyator committed Oct 2, 2023
1 parent d773231 commit 5cb5b4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 51 deletions.
31 changes: 3 additions & 28 deletions py-rattler/src/repo_data/sparse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{path::PathBuf, sync::Arc};

use pyo3::{intern, pyclass, pymethods, FromPyObject, PyAny, PyResult, Python};
use pyo3::{pyclass, pymethods, PyResult, Python};

use rattler_repodata_gateway::sparse::SparseRepoData;

use crate::channel::PyChannel;
use crate::error::PyRattlerError;
use crate::package_name::PyPackageName;
use crate::repo_data::repo_data_record::PyRepoDataRecord;

Expand All @@ -30,28 +29,6 @@ impl<'a> From<&'a PySparseRepoData> for &'a SparseRepoData {
}
}

impl<'a> TryFrom<&'a PyAny> for PySparseRepoData {
type Error = pyo3::PyErr;
fn try_from(value: &'a PyAny) -> Result<Self, Self::Error> {
let intern_val = intern!(value.py(), "_sparse");
if !value.hasattr(intern_val)? {
return Err(PyRattlerError::from(anyhow::anyhow!(
"TypeError: Object is not an instance of 'SparseRepoData'"
))
.into());
}

let inner = value.getattr(intern_val)?;
if !inner.is_instance_of::<PySparseRepoData>() {
return Err(
PyRattlerError::from(anyhow::anyhow!("TypeError: '_sparse' is invalid!")).into(),
);
}

PySparseRepoData::extract(inner)
}
}

#[pymethods]
impl PySparseRepoData {
#[new]
Expand Down Expand Up @@ -86,11 +63,9 @@ impl PySparseRepoData {
repo_data: Vec<PySparseRepoData>,
package_names: Vec<PyPackageName>,
) -> PyResult<Vec<Vec<PyRepoDataRecord>>> {
let repo_data = repo_data.iter().map(Into::into);
let package_names = package_names.into_iter().map(Into::into);

// release gil to allow other threads to progress
py.allow_threads(move || {
let repo_data = repo_data.iter().map(Into::into);
let package_names = package_names.into_iter().map(Into::into);
Ok(
SparseRepoData::load_records_recursive(repo_data, package_names, None)?
.into_iter()
Expand Down
41 changes: 18 additions & 23 deletions py-rattler/src/solver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pyo3::{pyfunction, PyAny, PyResult, Python};
use pyo3::{pyfunction, PyResult, Python};
use rattler_repodata_gateway::sparse::SparseRepoData;
use rattler_solve::{resolvo::Solver, SolverImpl, SolverTask};

Expand All @@ -13,35 +13,30 @@ use crate::{
pub fn py_solve(
py: Python<'_>,
specs: Vec<PyMatchSpec>,
available_packages: Vec<&'_ PyAny>,
available_packages: Vec<PySparseRepoData>,
locked_packages: Vec<PyRepoDataRecord>,
pinned_packages: Vec<PyRepoDataRecord>,
virtual_packages: Vec<PyGenericVirtualPackage>,
) -> PyResult<Vec<PyRepoDataRecord>> {
let packages = available_packages
.iter()
.map(|&pkg| TryInto::<PySparseRepoData>::try_into(pkg))
.collect::<PyResult<Vec<_>>>()?;

let package_names = specs
.iter()
.filter_map(|match_spec| match_spec.inner.name.clone());
py.allow_threads(move || {
let package_names = specs
.iter()
.filter_map(|match_spec| match_spec.inner.name.clone());

// pure rust operation, release gil to allow python threads to do other work
let available_packages = py.allow_threads(move || {
SparseRepoData::load_records_recursive(packages.iter().map(Into::into), package_names, None)
})?;
let available_packages = SparseRepoData::load_records_recursive(
available_packages.iter().map(Into::into),
package_names,
None,
)?;

let task = SolverTask {
available_packages: &available_packages,
locked_packages: locked_packages.into_iter().map(Into::into).collect(),
pinned_packages: pinned_packages.into_iter().map(Into::into).collect(),
virtual_packages: virtual_packages.into_iter().map(Into::into).collect(),
specs: specs.into_iter().map(Into::into).collect(),
};
let task = SolverTask {
available_packages: &available_packages,
locked_packages: locked_packages.into_iter().map(Into::into).collect(),
pinned_packages: pinned_packages.into_iter().map(Into::into).collect(),
virtual_packages: virtual_packages.into_iter().map(Into::into).collect(),
specs: specs.into_iter().map(Into::into).collect(),
};

// pure rust operation, release gil to allow python threads to do other work
py.allow_threads(move || {
Ok(Solver
.solve(task)
.map(|res| {
Expand Down

0 comments on commit 5cb5b4e

Please sign in to comment.