Skip to content

3 argument dot is slower than a matmul and two arg dot #1287

Open

Description

@SobhanMP

Somewhat counter intuitive:

using LinearAlgebra, StaticArrays, BenchmarkTools

f1(x, A) = dot(x, A * x)
f2(x, A) = dot(x, A, x)
let 
    x = randn(3)
    A = randn(3, 3)
    A = A * A' + 1e-6 * I
    sx = SVector{3}(x...)
    sA = SMatrix{3,3}(A...)
    display(@benchmark(f1($sx, $sA)))
    display(@benchmark(f2($sx, $sA)))
end


BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  2.813 ns … 18.835 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     2.817 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.878 ns ±  0.673 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █   ▅▁                                                     ▁
  █▅▁▁██▄▁▄▁▁▁▁▁▇▁▁▁▁▆▃▁▁▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▄▄▃▆▇▅ █
  2.81 ns      Histogram: log(frequency) by time     3.54 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  3.815 ns … 54.783 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     3.821 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   3.931 ns ±  1.141 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █  ▆     ▁                                                 ▁
  █▆▅█▅▇▃▅▇█▃▅▃▅▄▅▁▁▁▄▁▁▁▃▁▁▁▁▁▃▃▁▃▁▃▁▁▁▃▁▁▁▁▃▁▃▃▁▁▁▁▄▁▁▁▁▁▃ █
  3.82 ns      Histogram: log(frequency) by time      5.3 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
julia> versioninfo()
Julia Version 1.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
 Official https://julialang.org/ release
Platform Info:
 OS: Linux (x86_64-linux-gnu)
 CPU: 16 × 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz
 WORD_SIZE: 64
 LLVM: libLLVM-16.0.6 (ORCJIT, rocketlake)
Threads: 16 default, 0 interactive, 8 GC (on 16 virtual cores)
Environment:
 JULIA_EDITOR = code
 JULIA_NUM_THREADS = 16

Activity

  1. kbarros commented on Oct 26, 2026

    @kbarros

    I hit this also. A simple fix would be the method overload:

    LinearAlgebra.dot(x::StaticVector, A::StaticMatrix, y::StaticVector) = dot(x, A*y)
    

    Would this be welcomed as a PR?

  2. kbarros commented on Oct 29, 2026

    @kbarros

    Also, apparently u' * A * v translates to dot(u, A, v), so this is not an esoteric situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions