Skip to content

Commit

Permalink
call by value is more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
getreu committed Dec 20, 2018
1 parent 7764e09 commit 80c1b5a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
12 changes: 6 additions & 6 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,20 @@ pub fn from_file(
let mmap = unsafe { self::MmapOptions::new().map(&file)? };
while byte_counter + WIN_LEN <= len {
let chunk = &mmap[byte_counter..byte_counter + WIN_LEN];
sc.launch_scanner(Some(&filename), &byte_counter, &chunk);
sc.launch_scanner(Some(&filename), byte_counter, &chunk);
byte_counter += WIN_STEP;
}
// The last is shorter than WIN_LEN bytes, but can be longer than WIN_STEP
if byte_counter < len {
let chunk = &mmap[byte_counter..len];
sc.launch_scanner(Some(&filename), &byte_counter, &chunk);
sc.launch_scanner(Some(&filename), byte_counter, &chunk);
byte_counter += WIN_STEP;
}

// Now there can be still some bytes left (maximum WIN_OVERLAP bytes)
if byte_counter < len {
let chunk = &mmap[byte_counter..len];
sc.launch_scanner(Some(&filename), &byte_counter, &chunk);
sc.launch_scanner(Some(&filename), byte_counter, &chunk);
}
Ok(())
}
Expand Down Expand Up @@ -238,21 +238,21 @@ fn from_stdin(sc: &mut ScannerPool<'_>) -> Result<(), Box<std::io::Error>> {
}
// Handle data.
while data_start + WIN_LEN <= data_end {
sc.launch_scanner(None, &byte_counter, &buf[data_start..data_start + WIN_LEN]);
sc.launch_scanner(None, byte_counter, &buf[data_start..data_start + WIN_LEN]);
data_start += WIN_STEP;
byte_counter += WIN_STEP;
}
}
// The last is shorter than WIN_LEN bytes, but can be longer than WIN_STEP
if data_start < data_end {
sc.launch_scanner(None, &byte_counter, &buf[data_start..data_end]);
sc.launch_scanner(None, byte_counter, &buf[data_start..data_end]);
data_start += WIN_STEP;
byte_counter += WIN_STEP;
}

// Now there can be still some bytes left (maximum WIN_OVERLAP bytes)
if data_start < data_end {
sc.launch_scanner(None, &byte_counter, &buf[data_start..data_end]);
sc.launch_scanner(None, byte_counter, &buf[data_start..data_end]);
}
Ok(())
}
Expand Down
56 changes: 28 additions & 28 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ impl<'a> ScannerPool<'a> {
pub fn launch_scanner<'b>(
&mut self,
filename: Option<&'static str>,
byte_counter: &usize,
byte_counter: usize,
input_slice: &'b [u8],
) {
ScannerPool::launch_scanner2(
filename,
&byte_counter,
byte_counter,
&input_slice,
&mut self.pool,
&self.tx,
Expand All @@ -173,7 +173,7 @@ impl<'a> ScannerPool<'a> {
///
fn launch_scanner2<'b>(
filename: Option<&'static str>,
byte_counter: &usize,
byte_counter: usize,
input_slice: &'b [u8],
pool: &mut Pool,
tx: &SyncSender<FindingCollection>,
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<'a> ScannerPool<'a> {
fn scan_window<'b>(
scanner_state: &ScannerState,
filename: Option<&'static str>,
byte_counter: &usize,
byte_counter: usize,
input: &'b [u8],
) -> (FindingCollection, usize) {
// True if `mission.offset` is in the last UTF8_LEN_MAX Bytes of WIN_OVERLAP
Expand Down Expand Up @@ -441,7 +441,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 18);
Expand All @@ -467,7 +467,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 18);
Expand Down Expand Up @@ -508,7 +508,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, mut start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, mut start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, WIN_LEN);
Expand All @@ -534,7 +534,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &WIN_STEP, &inp[WIN_STEP..]);
let (res, start) = ScannerPool::scan_window(&ms, None, WIN_STEP, &inp[WIN_STEP..]);
assert_eq!(res, expected_fc);
assert_eq!(start, 17);
}
Expand Down Expand Up @@ -565,7 +565,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 25);
Expand All @@ -585,7 +585,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 4);
Expand Down Expand Up @@ -619,7 +619,7 @@ mod tests {
mission: &MISSIONS.v[1],
};

let (res, mut start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, mut start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, 23);
Expand All @@ -642,7 +642,7 @@ mod tests {
ms.offset = start;
ms.completes_last_str = res.last_str_is_incomplete;

let (res, start) = ScannerPool::scan_window(&ms, None, &WIN_STEP, &inp[WIN_STEP..]);
let (res, start) = ScannerPool::scan_window(&ms, None, WIN_STEP, &inp[WIN_STEP..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 9);
Expand Down Expand Up @@ -680,7 +680,7 @@ mod tests {
mission: &MISSIONS.v[1],
};

let (res, mut start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, mut start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, 23);
Expand All @@ -703,7 +703,7 @@ mod tests {
ms.offset = start;
ms.completes_last_str = res.last_str_is_incomplete;

let (res, start) = ScannerPool::scan_window(&ms, None, &WIN_STEP, &inp[WIN_STEP..]);
let (res, start) = ScannerPool::scan_window(&ms, None, WIN_STEP, &inp[WIN_STEP..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 9);
Expand Down Expand Up @@ -740,7 +740,7 @@ mod tests {
mission: &MISSIONS.v[1],
};

let (res, mut start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, mut start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, 25);
Expand All @@ -758,7 +758,7 @@ mod tests {
ms.offset = start;
ms.completes_last_str = res.last_str_is_incomplete;

let (res, start) = ScannerPool::scan_window(&ms, None, &WIN_STEP, &inp[WIN_STEP..]);
let (res, start) = ScannerPool::scan_window(&ms, None, WIN_STEP, &inp[WIN_STEP..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 9);
Expand Down Expand Up @@ -791,7 +791,7 @@ mod tests {
mission: &MISSIONS.v[1],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, 25);
Expand Down Expand Up @@ -829,7 +829,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 17);
Expand All @@ -844,7 +844,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 18);
Expand Down Expand Up @@ -883,7 +883,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 18);
Expand Down Expand Up @@ -917,7 +917,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

assert_eq!(res, expected_fc);
assert_eq!(start, 18);
Expand Down Expand Up @@ -948,7 +948,7 @@ mod tests {
mission: &MISSIONS.v[0],
};

let (res, mut start) = ScannerPool::scan_window(&ms, None, &start, &inp[..WIN_LEN]);
let (res, mut start) = ScannerPool::scan_window(&ms, None, start, &inp[..WIN_LEN]);

assert_eq!(res, expected_fc);
assert_eq!(start, 25);
Expand All @@ -971,7 +971,7 @@ mod tests {
ms.offset = start;
ms.completes_last_str = res.last_str_is_incomplete;

let (res, start) = ScannerPool::scan_window(&ms, None, &WIN_STEP, &inp[WIN_STEP..]);
let (res, start) = ScannerPool::scan_window(&ms, None, WIN_STEP, &inp[WIN_STEP..]);

assert_eq!(res, expected2);
assert_eq!(start, 10);
Expand Down Expand Up @@ -1035,7 +1035,7 @@ mod tests {
//println!("Merger terminated.");
});

sc.launch_scanner(None, &1000usize, &"hallo1234üduüso567890".as_bytes());
sc.launch_scanner(None, 1000usize, &"hallo1234üduüso567890".as_bytes());
assert_eq!(sc.scanner_states.v[0].offset, 6);
//assert_eq!(sc.scanner_states.v[1].offset, 6);
} // tx drops here
Expand Down Expand Up @@ -1072,31 +1072,31 @@ mod tests {
completes_last_str: false,
mission: &MISSIONS3.v[0],
};
let (res0, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res0, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

let start = 0;
let ms = ScannerState {
offset: 0,
completes_last_str: false,
mission: &MISSIONS3.v[1],
};
let (res1, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res1, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

let start = 0;
let ms = ScannerState {
offset: 0,
completes_last_str: false,
mission: &MISSIONS3.v[2],
};
let (res2, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res2, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

let start = 0;
let ms = ScannerState {
offset: 0,
completes_last_str: false,
mission: &MISSIONS3.v[3],
};
let (res3, start) = ScannerPool::scan_window(&ms, None, &start, &inp[..]);
let (res3, start) = ScannerPool::scan_window(&ms, None, start, &inp[..]);

// To see println! output: cargo test -- --nocapture
/*
Expand Down

0 comments on commit 80c1b5a

Please sign in to comment.