Summary: dotProduct overload for small fixed size arrays
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: performance
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from 2011-09-12 16:18:14 PDT ---
When the arrays given to std.numeric.dotProduct are very small, its performance
compared to an inlined foreach loop becomes quite bad (a simple benchmarks on

This is a hard problem to solve in general (you probably need profile-driven
optimizations, that maybe future LLVM versions will perform with -O5 or -O6
level optimization or more), but there is an important special case where I
think there is a way to improve the situation:

int[3] a, b;
auto r = dotProduct(a, b);

In this situation dotProduct may just recognize (with template constraints)
that both a and b are fixed-sized arrays, that their length is the same (so no
need to test it again at runtime), and that this length is small (less than 6?
8?). In this case a function template overload for dotProduct performs a
"static foreach" on the items of a and b (in theory you are also allowed to use
inline asm with few instructions that use SIMD registers, but currently with
DMD this kills inlining, so it's not good enough). I think in this case DMD
will *inline* this very simple dotProduct function overload, allowing good
enough performance.

This low performance problem has caused problems in my code.

See also bug 4393

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to