slice performance

オープン
#571 コメント 13 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
rust
領域
performance

調査の方向性

Start with the reported bench_loop and bench_slice benchmarks, comparing test_loop with test_slice on the same 3x3x3 patches in an 11x11x11 Array3. Inspect the slice and azip paths in the referenced src or lib code and determine what accounts for the timing difference. Done means explaining the slowdown and identifying whether the clean implementation can match the loop without changing its result.

索引モデルが issue の本文から書いたものです。

説明

performance

Don't try to understand my first version too much, the second function is much simpler. It's simply comparing two 3x3x3 patches of data in a 11x11x11 image and returning a score. I refactored this function from

fn test_loop(data: &[f64], m: usize, n: usize, o: usize) -> f64 {
    let pl = 3;
    let br = 4;
    let bl = 11;
    let bl2 = bl * bl;
    let mut sum = 0.0;
    for a in 0..pl {
        let idx1_a = (br + a) * bl2;
        let idx2_a = (m + a) * bl2;
        for b in 0..pl {
            let idx1_b = (br + b) * bl;
            let idx2_b = (n + b) * bl;
            for c in 0..pl {
                let idx1 = idx1_a + idx1_b + br + c;
                let idx2 = idx2_a + idx2_b + o + c;
                let diff = (data[idx1] - data[idx2]).powi(2);
                sum += diff;
            }
        }
    }
    sum
}

to

fn test_slice(data: &Array3<f64>, m: usize, n: usize, o: usize) -> f64 {
    let pl = 3;
    let br = 4;
    let s1 = data.slice(s![br..br+pl, br..br+pl, br..br+pl]);
    let s2 = data.slice(s![m..m+pl, n..n+pl, o..o+pl]);

    let mut sum = 0.0;
    azip!(s1, s2 in { sum += (s1 - s2).powi(2) });
    sum
}

Of course, I'm happy with the code quality now (!), but the clean version is surprisingly slow. I benched those 2 functions using the same data, with test_loop using &data.as_slice().unwrap() instead of &data

test bench_loop              ... bench:          25 ns/iter (+/- 5)
test bench_slice             ... bench:         142 ns/iter (+/- 1)

Are those results surprising to you? Both versions don't allocate, calculate the indices (in src or lib) and the sum, etc. I fail to see why the clean version is almost 6 times slower. Is slice doing something really complex?

主要言語
Rust
スター
4.3k
フォーク
391
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-ndarray/ndarray のほかの issue

rust-ndarray/ndarray の issue をすべて見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。