Bug 6657 – dotProduct overload for small fixed size arrays

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-09-12T16:18:14Z
Last change time
2020-03-21T03:56:32Z
Keywords
performance, pull
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-09-12T16:18:14Z
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 request). 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
Comment #1 by dlang-bot — 2019-04-30T23:01:30Z
@n8sh created dlang/phobos pull request #6990 "Fix Issue 6657 - dotProduct overload for small fixed size arrays" fixing this issue: - Fix Issue 6657 - dotProduct overload for small fixed size arrays https://github.com/dlang/phobos/pull/6990
Comment #2 by dlang-bot — 2019-05-03T02:30:57Z
dlang/phobos pull request #6990 "Fix Issue 6657 - dotProduct overload for small fixed size arrays" was merged into master: - c334d376011a04356dc150f765a54e7ad1c05810 by Nathan Sashihara: Fix Issue 6657 - dotProduct overload for small fixed size arrays https://github.com/dlang/phobos/pull/6990