Skip to content

Commit

Permalink
initial discovery rework
Browse files Browse the repository at this point in the history
  • Loading branch information
tbillington committed Dec 11, 2023
1 parent b5fe3f5 commit 44c9073
Show file tree
Hide file tree
Showing 11 changed files with 765 additions and 0 deletions.
141 changes: 141 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[workspace]
members = ["kondo-lib", "path-trie"]
resolver = "2"

[workspace.dependencies]
path-trie = { path = "./path-trie" }

[profile.release]
lto = "thin"
codegen-units = 1
11 changes: 11 additions & 0 deletions kondo-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "kondo-lib"
version = "0.1.0"
edition = "2021"

[dependencies]
crossbeam-channel = "0.5.8"
crossbeam-deque = "0.8.3"
crossbeam-utils = "0.8.16"
enum_dispatch = "0.3.12"
path-trie = { workspace = true }
119 changes: 119 additions & 0 deletions kondo-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
mod project;
mod search;

use std::{path::PathBuf, thread::available_parallelism, time::Duration};

use crossbeam_channel::unbounded;
use crossbeam_deque::{Injector, Worker};
use crossbeam_utils::{atomic::AtomicCell, sync::Parker};
pub use project::ProjectEnum;

use crate::search::search_thread;

fn run_local() {

Check warning on line 13 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test using stable

function `run_local` is never used
let injector = Injector::<PathBuf>::new();

let thread_count = available_parallelism()
.unwrap_or(std::num::NonZeroUsize::new(4).unwrap())
.get();

let workers = (0..thread_count)
.map(|_| (Worker::<PathBuf>::new_fifo(), Parker::new()))
.collect::<Vec<_>>();

let stealers = workers
.iter()
.map(|w| Worker::stealer(&w.0))
.collect::<Vec<_>>();

let thread_work_references = workers
.iter()
.map(|w| (w.1.unparker().clone(), AtomicCell::new(true)))
.collect::<Vec<_>>();

let finished = AtomicCell::new(false);

let initial_paths = vec![
PathBuf::from("/Users/choc/junk-code"), // std::env::current_dir().unwrap()
];

for path in initial_paths.into_iter() {
injector.push(path);
}

let worker_thread_idxs = (0..workers.len()).collect::<Vec<_>>();

let (result_sender, r) = unbounded();

let start = std::time::Instant::now();

{
let senders = (0..thread_count)
.map(|_| result_sender.clone())
.collect::<Vec<_>>();

drop(result_sender);

std::thread::scope(|s| {
for (i, (w, p)) in workers.into_iter().enumerate() {
let active_ref = &thread_work_references[i].1;
let sender = &senders[i];
let i = &worker_thread_idxs[i];
s.spawn(|| {
search_thread(i, w, &injector, &stealers, p, active_ref, &finished, sender)
});
}

loop {
std::thread::sleep(Duration::from_millis(10));

if thread_work_references
.iter()
.any(|(_, active)| active.load())
{
let mut sleeping_threads = 0;

Check warning on line 74 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test using stable

variable `sleeping_threads` is assigned to, but never used

Check failure on line 74 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lint and Formatting

variable `sleeping_threads` is assigned to, but never used
for (p, active) in thread_work_references.iter() {
if !active.load() {
sleeping_threads += 1;
p.unpark();
}
}
// println!("Sleeping threads: {}", sleeping_threads);
// for (i, s) in stealers.iter().enumerate() {
// print!("{}={} ", i, s.len());
// }
// println!();
} else {
finished.store(true);
for (p, _) in thread_work_references.iter() {
p.unpark();
}
break;
}
}
});
}

println!("Done Loop");

let mut c = 0;
for r in r {

Check warning on line 100 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test using stable

unused variable: `r`

Check failure on line 100 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lint and Formatting

unused variable: `r`
c += 1;
// println!("Found project: {} {}", r.1.name(), r.0.display());
}

println!("Took {}ms", start.elapsed().as_millis());

println!("Found {} projects", c);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
run_local();
assert!(false);

Check failure on line 117 in kondo-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lint and Formatting

`assert!(false)` should probably be replaced
}
}
34 changes: 34 additions & 0 deletions kondo-lib/src/project.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::path::Path;

use enum_dispatch::enum_dispatch;

mod node;
mod rust;
mod unity;

use node::NodeProject;
use rust::RustProject;
use unity::UnityProject;

#[enum_dispatch]
#[derive(Debug, Clone, Copy)]
pub enum ProjectEnum {
RustProject,
NodeProject,
UnityProject,
}

impl ProjectEnum {
pub const ALL: [Self; 3] = [
Self::RustProject(RustProject),
Self::NodeProject(NodeProject),
Self::UnityProject(UnityProject),
];
}

#[enum_dispatch(ProjectEnum)]
pub trait Project {
fn name(&self) -> &str;
fn is_project(&self, root_dir: &Path) -> bool;
fn is_artifact(&self, path: &Path) -> bool;
}
20 changes: 20 additions & 0 deletions kondo-lib/src/project/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::path::Path;

use super::Project;

#[derive(Debug, Clone, Copy)]
pub struct NodeProject;

impl Project for NodeProject {
fn name(&self) -> &str {
"Node"
}

fn is_project(&self, root_dir: &Path) -> bool {
root_dir.join("package.json").exists()
}

fn is_artifact(&self, path: &Path) -> bool {
path.is_dir() && path.file_name().is_some_and(|f| f == "node_modules")
}
}
20 changes: 20 additions & 0 deletions kondo-lib/src/project/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::path::Path;

use super::Project;

#[derive(Debug, Clone, Copy)]
pub struct RustProject;

impl Project for RustProject {
fn name(&self) -> &str {
"Rust"
}

fn is_project(&self, root_dir: &Path) -> bool {
root_dir.join("Cargo.toml").exists()
}

fn is_artifact(&self, path: &Path) -> bool {
path.is_dir() && path.file_name().is_some_and(|f| f == "target")
}
}
20 changes: 20 additions & 0 deletions kondo-lib/src/project/unity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::path::Path;

use super::Project;

#[derive(Debug, Clone, Copy)]
pub struct UnityProject;

impl Project for UnityProject {
fn name(&self) -> &str {
"Unity"
}

fn is_project(&self, root_dir: &Path) -> bool {
root_dir.join("Assembly-CSharp.csproj").exists()
}

fn is_artifact(&self, path: &Path) -> bool {
path.is_dir() && path.file_name().is_some_and(|f| f == "Library")
}
}
Loading

0 comments on commit 44c9073

Please sign in to comment.