Repository: opencv/opencv
File: modules/dnn/src/layers/affinegrid_layer.cpp (C++)

Code before:
            const float xs_ = xs, xd_ = xd, ys_ = ys, yd_ = yd;
            const Mat theta_ = theta;
            Mat grid_ = grid;
            int lastN = -1;
            const T* T0 = nullptr;
            const T* T1 = nullptr;
            for (int nyi = range.start; nyi < range.end; nyi++)
            {
                int n = nyi / H_;
                int y = nyi - n * H_;
                if (n != lastN)
                {
                    int i0[3] = {n, 0, 0};
                    int i1[3] = {n, 1, 0};
                    T0 = theta_.ptr<T>(i0);
                    T1 = theta_.ptr<T>(i1);
                    lastN = n;
                }
                float ny = yd_ + ys_ * y;
                float base0 = (float)T0[1]*ny + (float)T0[2];
                float base1 = (float)T1[1]*ny + (float)T1[2];
                float* out = grid_.ptr<float>(n, y, 0);
                for (int x = 0; x < W_; x++)
                {
                    float nx = xd_ + xs_ * x;
                    out[2*x + 0] = (float)T0[0]*nx + base0;
                    out[2*x + 1] = (float)T1[0]*nx + base1;
                }
            }
        });
    }

    template<typename T>
    void computeGrid3D(const Mat& theta, Mat& grid, int N, int D, int H, int W,
                       float xs, float xd, float ys, float yd, float zs, float zd) const
    {
        const int stride = D * H;
        const int total = N * stride;
        parallel_for_(Range(0, total), [&](const Range& range){
            const int H_ = H;
            const int W_ = W;
            const int stride_ = stride;
            const float xs_ = xs, xd_ = xd, ys_ = ys, yd_ = yd, zs_ = zs, zd_ = zd;
            const Mat theta_ = theta;
            Mat grid_ = grid;
            int lastN = -1;
            const T* T0 = nullptr;
            const T* T1 = nullptr;
            const T* T2 = nullptr;
            for (int ndz = range.start; ndz < range.end; ndz++)
            {

Review comment (suggestion):
it's recommended to read T0[0] and T1[0] into local variables outside of the innermost loop:

```
float T0_0 = (float)T0[0], T1_0 = (float)T1[0];
for (int x = 0; x < W_; x++)
{
    float nx = xd_ + xs_*x;
    out[2*x + 0] = T0_0*nx + base0;
    out[2*x + 1] = T1_0*nx + base1;
}
```


Code after:
        {
            int n = nyi / H_;
            int y = nyi - n * H_;
            if (n != lastN)
            {
                int i0[3] = {n, 0, 0};
                int i1[3] = {n, 1, 0};
                T0 = theta_.ptr<T>(i0);
                T1 = theta_.ptr<T>(i1);
                T0_0 = (float)T0[0];
                T1_0 = (float)T1[0];
                lastN = n;
            }
            float ny = yd_ + ys_ * y;
            float base0 = (float)T0[1]*ny + (float)T0[2];
            float base1 = (float)T1[1]*ny + (float)T1[2];
            float* out = grid_.ptr<float>(n, y, 0);
            for (int x = 0; x < W_; x++)
            {
                float nx = xd_ + xs_ * x;
                out[2*x + 0] = T0_0*nx + base0;
                out[2*x + 1] = T1_0*nx + base1;
            }
        }
    });
}

template<typename T>
void computeGrid3D(const Mat& theta, Mat& grid, int N, int D, int H, int W,
                   float xs, float xd, float ys, float yd, float zs, float zd)
{
    const int stride = D * H;
    const int total = N * stride;
    parallel_for_(Range(0, total), [&](const Range& range){
        const int H_ = H;
        const int W_ = W;
        const int stride_ = stride;
        const float xs_ = xs, xd_ = xd, ys_ = ys, yd_ = yd, zs_ = zs, zd_ = zd;
        const Mat theta_ = theta;
        Mat grid_ = grid;
        int lastN = -1;
        const T* T0 = nullptr;
        const T* T1 = nullptr;
        const T* T2 = nullptr;
        float T0_0 = 0.f, T1_0 = 0.f, T2_0 = 0.f;
        for (int ndz = range.start; ndz < range.end; ndz++)
        {
            int n = ndz / stride_;
            int rem = ndz - n * stride_;
            int z = rem / H_;
            int y = rem - z * H_;