Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ path = "src/lib.rs"
rand = "0.8"
image = "0.25"
image-compare = "0.5.0"
bit-set = "0.8.0"

[dev-dependencies]
divan = { version = "4.0.2", package = "codspeed-divan-compat" }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ cargo codspeed build -m walltime
cargo codspeed run -m walltime
```


Note: You can also set the `CODSPEED_RUNNER_MODE` environment variable to `walltime` to avoid passing `-m walltime` every time.
15 changes: 8 additions & 7 deletions src/bfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use bit_set::BitSet;
use std::collections::VecDeque;

/// A simple graph represented as an adjacency list
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -26,22 +27,22 @@ impl Graph {
/// Naive BFS implementation using Vec as a queue (intentionally slow)
/// Returns the order in which nodes were visited
pub fn bfs_naive(graph: &Graph, start: usize) -> Vec<usize> {
let mut visited = HashSet::new();
let mut queue = Vec::new(); // Using Vec instead of VecDeque - intentionally inefficient!
let mut visited = BitSet::new();

let mut queue = VecDeque::new();
let mut result = Vec::new();

queue.push(start);
queue.push_back(start);
visited.insert(start);

while !queue.is_empty() {
// remove(0) is O(n) - this makes BFS slow!
let node = queue.remove(0);
let node = queue.pop_front().unwrap();
result.push(node);

if let Some(neighbors) = graph.adjacency.get(node) {
for &neighbor in neighbors {
if visited.insert(neighbor) {
queue.push(neighbor);
queue.push_back(neighbor);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/blob_corruption_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ pub fn find_corruptions_sequential(
corrupted_path: &str,
chunk_size: usize,
) -> Vec<Corruption> {
let mut ref_file = BufReader::new(File::open(reference_path).unwrap());
let mut corrupt_file = BufReader::new(File::open(corrupted_path).unwrap());
let mut ref_file = BufReader::with_capacity(1024 * 1024, File::open(reference_path).unwrap());
let mut corrupt_file =
BufReader::with_capacity(1024 * 1024, File::open(corrupted_path).unwrap());

let mut ref_buffer = vec![0u8; chunk_size];
let mut corrupt_buffer = vec![0u8; chunk_size];
Expand Down Expand Up @@ -92,10 +93,7 @@ mod tests {
"Middle corruption offset"
);
assert_eq!(corruptions[25].length, 4096, "Middle corruption length");
assert_eq!(
corruptions[49].offset, 507871232,
"Last corruption offset"
);
assert_eq!(corruptions[49].offset, 507871232, "Last corruption offset");
assert_eq!(corruptions[49].length, 5120, "Last corruption length");
}
}
36 changes: 23 additions & 13 deletions src/lut_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,40 @@ pub fn apply_brightness_contrast_gamma(
mod naive {
use super::*;

pub struct BrightnessLut {
lut: [u8; 256],
}

impl BrightnessLut {
fn new(brightness: i16, contrast: f32) -> Self {
let mut lut = [0u8; 256];

for i in 0..256 {
lut[i] = (((i as f32 - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32)
.clamp(0.0, 255.0) as u8;
}

Self { lut }
}
}

/// Apply brightness and contrast with floating-point math per pixel
pub fn apply_brightness_contrast(img: &RgbImage, brightness: i16, contrast: f32) -> RgbImage {
let (width, height) = img.dimensions();
let mut output = ImageBuffer::new(width, height);

let lut = BrightnessLut::new(brightness, contrast);

for (x, y, pixel) in img.enumerate_pixels() {
let r = pixel[0] as f32;
let g = pixel[1] as f32;
let b = pixel[2] as f32;

// Apply contrast and brightness (5 FP ops per channel!)
let r = ((r - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
let g = ((g - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
let b = ((b - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;

output.put_pixel(
x,
y,
Rgb([
r.clamp(0.0, 255.0) as u8,
g.clamp(0.0, 255.0) as u8,
b.clamp(0.0, 255.0) as u8,
]),
);
let r = lut.lut[r.clamp(0.0, 255.0) as usize];
let g = lut.lut[g.clamp(0.0, 255.0) as usize];
let b = lut.lut[b.clamp(0.0, 255.0) as usize];
output.put_pixel(x, y, Rgb([r, g, b]));
}

output
Expand Down