Make it more like libraries.rs, tried reading whole file into vec, but it's very slow, maybe slice is better

This commit is contained in:
Fabian Schmidt 2024-12-31 12:58:04 +01:00
parent 45b3014cbb
commit fdd92dd5f7

View File

@ -21,29 +21,28 @@ pub fn run() {
let file_length = reader.seek(SeekFrom::End(0)).unwrap(); let file_length = reader.seek(SeekFrom::End(0)).unwrap();
let chunk_length = file_length as usize / cores; let chunk_length = file_length as usize / cores;
let mut bounds = Vec::with_capacity(cores + 1); let mut bounds = Vec::with_capacity(cores + 1);
bounds.push(0); let mut start = 0;
for i in 1..cores { for i in 0..cores {
let mut reader = BufReader::new(&file); let mut reader = BufReader::new(&file);
let mut byte_start = chunk_length * i; let mut end = chunk_length * i;
reader reader
.seek(SeekFrom::Start(byte_start as u64)) .seek(SeekFrom::Start(end as u64))
.expect("could not seek"); .expect("could not seek");
let mut line = Vec::with_capacity(108); let mut line = Vec::with_capacity(108);
let line_len = reader let line_len = reader
.read_until(b'\n', &mut line) .read_until(b'\n', &mut line)
.expect("could not read bytes"); .expect("could not read bytes");
byte_start += line_len; end += line_len;
bounds.push(byte_start as u64); bounds.push((start, end));
start = end + 1;
} }
bounds.push(file_length);
for i in 0..cores { for i in 0..cores {
let tx = tx.clone(); let tx = tx.clone();
let mut currposition = *bounds.get(i).unwrap(); let (mut currposition, end) = *bounds.get(i).unwrap();
let end = *bounds.get(i + 1).unwrap();
s.spawn(move || { s.spawn(move || {
let file = File::open(FILE_PATH).expect("File measurements.txt not found"); let file = File::open(FILE_PATH).expect("File measurements.txt not found");
let mut reader = BufReader::new(&file); let mut reader = BufReader::new(&file);
reader.seek(SeekFrom::Start(currposition)).unwrap(); reader.seek(SeekFrom::Start(currposition as u64)).unwrap();
let mut t_stations: HashMap<u64, (String, StationMeasurements)> = let mut t_stations: HashMap<u64, (String, StationMeasurements)> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
let mut line = Vec::with_capacity(108); let mut line = Vec::with_capacity(108);
@ -70,7 +69,7 @@ pub fn run() {
}; };
t_stations.insert(hash, (station.to_string(), measurements)); t_stations.insert(hash, (station.to_string(), measurements));
} }
currposition += line_len as u64; currposition += line_len;
if currposition >= end { if currposition >= end {
break; break;
} }