image-rs / image-rs/imageproc

Check elision of bounds checking for simple loops

Open
#374 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
979
Forks
181
Avg merge
5m
Merged PRs (30d)
4

Description

When checking unsafe usage for https://github.com/image-rs/imageproc/issues/371 I found a few cases where benchmarks showed a speedup from using unsafe getters despite the accesses having the following form:

    for y in 0..image.height() {
        for x in 0..image.width() {
            ...
            let p = image.unsafe_get_pixel(x, y);
            ...
        }
    }

Check the generated assembly from using the current accessor functions, starting from a trivial loop, e.g.


/// .
pub fn trivial_loop(image: &mut GrayImage, d: u8) {
    for y in 0..image.height() {
        for x in 0..image.width() {
            let p = image.get_pixel(x, y)[0];
            image.put_pixel(x, y, Luma([p + d]));
        }
    }
}

/// .
pub fn trivial_loop_unsafe(image: &mut GrayImage, d: u8) {
    unsafe {
        for y in 0..image.height() {
            for x in 0..image.width() {
                let p = image.unsafe_get_pixel(x, y)[0];
                image.unsafe_put_pixel(x, y, Luma([p + d]));
            }
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the current accessor functions used by the trivial_loop and trivial_loop_unsafe examples in the issue. Generate and compare their assembly, beginning with the nested height/width loops, and determine whether safe accesses retain bounds checks. Done means documenting the comparison and identifying whether the safe form can match the unsafe form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-vision, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.