Skip to content

Commit

Permalink
more cleanup, less cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
max-trunk committed Jan 10, 2025
1 parent ce062af commit 47209f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
13 changes: 10 additions & 3 deletions codeowners/src/codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use constants::CODEOWNERS_LOCATIONS;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
// use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods, gen_stub_pyclass_enum};
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
Expand Down Expand Up @@ -101,7 +100,9 @@ impl CodeOwners {
num_threads: usize,
) -> HashMap<String, Option<Self>> {
let chunk_size = (to_parse.len() + num_threads - 1) / num_threads;

let mut handles = Vec::with_capacity(num_threads);

let results_map: Arc<Mutex<HashMap<String, Option<Self>>>> =
Arc::new(Mutex::new(HashMap::new()));

Expand All @@ -128,7 +129,6 @@ impl CodeOwners {
}

#[derive(Debug, Clone, PartialEq, Eq)]
// #[cfg_attr(feature = "pyo3", gen_stub_pyclass_enum, pyclass(eq))]
pub enum Owners {
GitHubOwners(GitHubOwners),
GitLabOwners(GitLabOwners),
Expand Down Expand Up @@ -169,19 +169,26 @@ pub fn associate_codeowners_multithreaded(
) -> Vec<Vec<String>> {
let input_len = to_associate.len();
let chunk_size = (input_len + num_threads - 1) / num_threads;

let mut handles = Vec::with_capacity(num_threads);

let codeowners_matchers = Arc::new(RwLock::new(codeowners_matchers));
let to_associate = Arc::new(RwLock::new(to_associate));
let all_associated_owners: Arc<Mutex<Vec<Option<Vec<String>>>>> =
Arc::new(Mutex::new(vec![None; input_len]));

for i in 0..num_threads {
let to_associate = to_associate.clone();
let codeowners_matchers = Arc::clone(&codeowners_matchers);
let to_associate = Arc::clone(&to_associate);
let all_associated_owners = Arc::clone(&all_associated_owners);

let start = i * chunk_size;
let end = ((i + 1) * chunk_size).min(input_len);

let handle = thread::spawn(move || {
let codeowners_matchers = codeowners_matchers.read().unwrap();
let to_associate = to_associate.read().unwrap();

for j in start..end {
let (bundle_upload_id, file) = &to_associate[j];
let codeowners_matcher = codeowners_matchers.get(bundle_upload_id);
Expand Down
3 changes: 1 addition & 2 deletions codeowners/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl FromStr for GitHubOwner {

/// Mappings of GitHub owners to path patterns
#[derive(Debug, PartialEq, Clone, Eq)]
// #[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass)]
pub struct GitHubOwners {
paths: Vec<(Pattern, Vec<GitHubOwner>)>,
}
Expand Down Expand Up @@ -174,7 +173,7 @@ pub struct BindingsGitHubOwners(pub GitHubOwners);
#[gen_stub_pymethods]
#[pymethods]
impl BindingsGitHubOwners {
pub fn of(&self, path: String) -> Option<Vec<String>> {
fn of(&self, path: String) -> Option<Vec<String>> {
let owners = self.0.of(Path::new(&path));
owners.map(|owners| owners.iter().map(ToString::to_string).collect())
}
Expand Down
3 changes: 1 addition & 2 deletions codeowners/src/gitlab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ impl fmt::Display for GitLabOwner {
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
// #[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass)]
pub struct GitLabOwners {
file: File,
}
Expand Down Expand Up @@ -118,7 +117,7 @@ pub struct BindingsGitLabOwners(pub GitLabOwners);
#[gen_stub_pymethods]
#[pymethods]
impl BindingsGitLabOwners {
pub fn of(&self, path: String) -> Option<Vec<String>> {
fn of(&self, path: String) -> Option<Vec<String>> {
let owners = self.0.of(Path::new(&path));
owners.map(|owners| owners.iter().map(ToString::to_string).collect())
}
Expand Down

0 comments on commit 47209f8

Please sign in to comment.