Avoid type dispatch in inner loop of dot()
- Dominant language
- JavaScript
- Stars
- 15.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
The implementation of `dot` in src/function/matrix/dot.js goes to some pain to select specialized methods for add and multiply to avoid type dispatch on every entry. However, it does not do so for conj; and since it also does not check whether the type only encodes real numbers (in which case the conj step could be eliminated), ultimately the cost of performing type check for conj is incurred for every element of the the first vector. That's not negligible for long vectors, and makes the trouble taken to specialize add and multiply somewhat irrelevant. Therefore, as long as we are visiting the dot product, it could be worthwhile to either (a) select the implementation of conj for the datatype of the first vectors, when it is known and not `'mixed'`, or (b) when the datatype of the first vector exclusively represents real numbers (e.g. bigint, number, BigNumber, Fraction), skip the conjugation altogether, or even both (a) and (b) -- which is to say, skip conjugating for real number types, and directly conjugate with the complex number conjugator, rather than via type dispatch, when the first vector is Complex.
Note that a PR addressing this should include a benchmark for some long dot products, to show the benefit of specializing conj.
_Originally posted by @gwhitney in https://github.com/josdejong/mathjs/issues/3456#issuecomment-2819551739_
Contributor guide
Assessment
This issue has not been assessed yet.