Skip to content

Commit

Permalink
Use Rc for imports_info.package_info to avoid expensive cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Jan 1, 2025
1 parent 8dac579 commit 431b072
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/imports_info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
mod parse;
mod queries;

use anyhow::Result;
use rayon::iter::ParallelBridge;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};

pub use crate::imports_info::queries::external_imports::ExternalImportsQueries;
pub use crate::imports_info::queries::internal_imports::InternalImportsQueries;
use crate::{
package_info::{PackageInfo, PackageItemToken},
Error, PackageItemIterator, Pypath,
};
use anyhow::Result;
use rayon::iter::ParallelBridge;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

/// An explicit import statement.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -88,7 +88,8 @@ impl From<PackageItemToken> for PackageItemTokenSet {
/// ```
#[derive(Debug, Clone)]
pub struct ImportsInfo {
package_info: PackageInfo,
// Use `Rc` to avoid cloning `package_info` on `import_info.clone()`.
package_info: Rc<PackageInfo>,
//
internal_imports: HashMap<PackageItemToken, HashSet<PackageItemToken>>,
reverse_internal_imports: HashMap<PackageItemToken, HashSet<PackageItemToken>>,
Expand Down Expand Up @@ -144,10 +145,12 @@ impl ImportsInfo {
package_info: PackageInfo,
options: ImportsInfoBuildOptions,
) -> Result<Self> {
let package_info = Rc::new(package_info);

let all_raw_imports = get_all_raw_imports(&package_info)?;

let mut imports_info = ImportsInfo {
package_info: package_info.clone(),
package_info: Rc::clone(&package_info),
internal_imports: HashMap::new(),
reverse_internal_imports: HashMap::new(),
internal_imports_metadata: HashMap::new(),
Expand Down

0 comments on commit 431b072

Please sign in to comment.