remove Arc

This commit is contained in:
xenia 2024-11-22 13:15:31 -05:00
parent f7efe0f420
commit f0ca662d0c
1 changed files with 50 additions and 49 deletions

View File

@ -2,7 +2,6 @@ use std::fs::File;
use std::io::Write; use std::io::Write;
use std::os::fd::AsRawFd; use std::os::fd::AsRawFd;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use clap::Parser; use clap::Parser;
use miette::{IntoDiagnostic, Result}; use miette::{IntoDiagnostic, Result};
@ -67,50 +66,50 @@ fn main() -> Result<()> {
}; };
let mut file = File::open(args.file).into_diagnostic()?; let mut file = File::open(args.file).into_diagnostic()?;
let strings = Arc::new(lib::get_strings(&mut file, args.min_str_len).into_diagnostic()?); let strings = lib::get_strings(&mut file, args.min_str_len).into_diagnostic()?;
eprintln!("Found {} strings", strings.len()); eprintln!("Found {} strings", strings.len());
let pointers = Arc::new(lib::get_pointers(&mut file, args.big_endian).into_diagnostic()?); let pointers = lib::get_pointers(&mut file, args.big_endian).into_diagnostic()?;
eprintln!("Found {} pointers", pointers.len()); eprintln!("Found {} pointers", pointers.len());
let start = 0x0000_0000u64; let start = 0x0000_0000u64;
let end = 0x1_0000_0000u64; let end = 0x1_0000_0000u64;
let total_pages = ((end - start) / (args.page_size as u64)) as usize; let total_pages = ((end - start) / (args.page_size as u64)) as usize;
let chunk_size = (end - start) / (args.threads as u64); let chunk_size = (end - start) / (args.threads as u64);
let progress_counters: Vec<_> =
(0..args.threads).map(|_| lib::ComputeProgress::new()).collect();
let mut thread_results = std::thread::scope(|s| {
let ranges = (start..=end) let ranges = (start..=end)
.step_by(chunk_size as usize) .step_by(chunk_size as usize)
.zip((start + chunk_size..=end).step_by(chunk_size as usize)); .zip((start + chunk_size..=end).step_by(chunk_size as usize));
let tasks: Vec<_> = ranges let tasks: Vec<_> = ranges.zip(progress_counters.iter())
.map(|(start, end)| { .map(|((start, end), counter)| {
let progress = Arc::new(lib::ComputeProgress::new()); let strings = &strings;
let pointers = &pointers;
let strings = Arc::clone(&strings); s.spawn(move || {
let pointers = Arc::clone(&pointers);
let child_progress = Arc::clone(&progress);
let thread = std::thread::spawn(move || {
lib::compute_matches( lib::compute_matches(
&strings, strings,
&pointers, pointers,
start, start,
end, end,
args.page_size, args.page_size,
Some(&child_progress), Some(counter),
) )
}); })
(thread, progress)
}) })
.collect(); .collect();
if let Some(mut term) = term { if let Some(mut term) = term {
loop { loop {
std::thread::sleep(std::time::Duration::from_millis(100)); std::thread::sleep(std::time::Duration::from_millis(100));
if tasks.iter().any(|(thread, _)| !thread.is_finished()) { if tasks.iter().any(|thread| !thread.is_finished()) {
term.carriage_return().unwrap(); term.carriage_return().unwrap();
term.delete_line().unwrap(); term.delete_line().unwrap();
let completed_pages: usize = tasks.iter().map(|(_, prg)| prg.num_completed()).sum(); let completed_pages: usize =
progress_counters.iter().map(|prg| prg.num_completed()).sum();
eprint!("{} / {} ({}%)", completed_pages, total_pages, eprint!("{} / {} ({}%)", completed_pages, total_pages,
completed_pages * 100 / total_pages); completed_pages * 100 / total_pages);
@ -127,10 +126,12 @@ fn main() -> Result<()> {
eprintln!("Scanning..."); eprintln!("Scanning...");
} }
let mut thread_results: Vec<_> = tasks let thread_results: Vec<_> = tasks
.into_iter() .into_iter()
.map(|(thread, _)| thread.join().map_err(std::panic::resume_unwind).unwrap()) .map(|thread| thread.join().map_err(std::panic::resume_unwind).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
thread_results
});
eprintln!("Results:"); eprintln!("Results:");