D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6657 - dotProduct overload for small fixed size arrays
Summary: dotProduct overload for small fixed size arrays
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: performance, pull
Depends on:
Blocks:
 
Reported: 2011-09-12 16:18 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-09-12 16:18:14 UTC
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 Dlang Bot 2019-04-30 23:01:30 UTC
@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 Dlang Bot 2019-05-03 02:30:57 UTC
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