Skip to content

Commit

Permalink
feat: use WalkDir to.. walk directories
Browse files Browse the repository at this point in the history
- also don't try to walk node_modules or hidden directories
- this increases performance for my own repository from 100s -> 6s
  • Loading branch information
AvarianKnight committed Oct 31, 2021
1 parent ffeeeed commit 229b45a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
1 change: 1 addition & 0 deletions exporter/Cargo.lock

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

1 change: 1 addition & 0 deletions exporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
walkdir = "2"
eframe = "0.15.0"
native-dialog = "0.5.5"
serde_derive = "1.0.130"
Expand Down
Binary file modified exporter/exporter.exe
Binary file not shown.
72 changes: 43 additions & 29 deletions exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ mod carcols;
mod ui;
use std::{fs, io, env};
use std::path::{Path, PathBuf};
use std::fs::{DirEntry, File};
use std::fs::{File};
use serde_derive::Serialize;
use std::sync::{Mutex};
use std::time::Instant;
use walkdir::{DirEntry, WalkDir};

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -41,19 +42,29 @@ impl Model {
}
}

fn should_walk_dir(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| {
if entry.path().is_dir() {
!s.contains("node_modules") && !s.starts_with(".")
} else {
s.ends_with(".meta") || s.ends_with(".xml")
}
})
.unwrap_or(false)
}

#[allow(unused_must_use)]
pub fn handle_files(path: PathBuf) {
let start = Instant::now();
// TODO: Proper error handling
let entries = fs::read_dir(path.as_path()).unwrap()
.map(|res| res.map(|e| {
e.path()
}))
.collect::<Result<Vec<_>, io::Error>>().unwrap();

for entry in entries.iter() {
crate::visit_dirs(entry, &crate::handle_file);
}

WalkDir::new(path.as_path())
.into_iter()
.filter_entry(|e| should_walk_dir(e))
.filter_map(|v| v.ok())
.for_each(|dir| handle_file(dir));

println!("Finished executing, {:.2?} time elapsed", start.elapsed());

Expand All @@ -64,6 +75,23 @@ pub fn handle_files(path: PathBuf) {
fs::write("data.json", val).unwrap();
}

fn jooat(string: String) -> u32 {
let lower_str = string.to_lowercase();
let char_iter = lower_str.chars();
let mut hash: u32 = 0;

for char in char_iter {
hash = hash.overflowing_add(u32::from(char as u8)).0;
hash = hash.overflowing_add(hash.overflowing_shl(10).0).0;
hash ^= hash.overflowing_shr(6).0;
}

hash = hash.overflowing_add(hash.overflowing_shl(3).0).0;
hash ^= hash.overflowing_shr(11).0;
hash = hash.overflowing_add(hash.overflowing_shl(15).0).0;

hash
}

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -84,24 +112,10 @@ fn main() {
eframe::run_native(Box::new(app), native_options);
}

fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, cb)?;
} else {
cb(&entry);
}
}
}
Ok(())
}

fn handle_file(dir: &DirEntry) {
let entry_name = dir.file_name().into_string().unwrap();
let path = &dir.path();
fn handle_file(dir: DirEntry) {
let entry_name = dir.file_name().to_str().unwrap();
// let entry_name = dir.file_name().into_string().unwrap();
let path = &dir.path().to_path_buf();
// We don't need to send the entire direntry
if entry_name.contains("vehicles.meta") {
vehicles::handle_vehicles(path);
Expand Down

0 comments on commit 229b45a

Please sign in to comment.