Skip to content

inlined sum, sumabs2, mean, and similar? #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stevengj opened this issue Nov 2, 2016 · 4 comments
Closed

inlined sum, sumabs2, mean, and similar? #64

stevengj opened this issue Nov 2, 2016 · 4 comments

Comments

@stevengj
Copy link
Contributor

stevengj commented Nov 2, 2016

It seems like it would be worthwhile to inline common reduction operations for small vectors rather than calling the generic Base reduce code?

julia> @which sum(SVector(3,4,5))
sum(a) at reduce.jl:229
@c42f
Copy link
Member

c42f commented Nov 2, 2016

In this case, the optimizer actually seems to be doing very well at turning the generic Base code into something efficient:

julia> using StaticArrays

julia> @code_native sum(SVector(1,2,3))
        .text
Filename: reduce.jl
        pushq   %rbp
        movq    %rsp, %rbp
Source line: 108
        movq    8(%rdi), %rax
        addq    (%rdi), %rax
        addq    16(%rdi), %rax
Source line: 229
        popq    %rbp
        retq
        nopw    %cs:(%rax,%rax)

I get similar results for sumabs2 and mean.

With the current compiler and standard library, it's hard to know what to do about similar: it inherently needs to give a mutable container, but SVector is immutable. As soon as we create a mutable container (MVector) we end up with heap allocations. If you have any good ideas about how to avoid this, we're all ears! Some of the problems are discussed here: #32

@c42f
Copy link
Member

c42f commented Nov 2, 2016

Ah wait. @andyferris has implemented mapreduce, and base calls that which is why this works at all.

Fair enough to have these @inline, if annoying to have to replicate even more of Base.

@andyferris
Copy link
Member

Yes, I've been aiming for some compatibility for Base functions. It seems sad to have to specialize sum simply because Base doesn't include an @inline but is otherwise perfect. 😢 Fortunately, as @c42f points out, sum(::SVector{3}) does happen to inline, but this doesn't work for longer vectors.

Also note that just because @which goes to Base it doesn't mean that's not intentional, or that generically written code in Base isn't performant with StaticArrays. I would actually prefer more of that generic code to work fast.

Steven, do you think adding @inline to a bunch of obvious places like this into Base would be welcome (e.g. here where sum is more-or-less an alias for another function)? Or better to do that here?

@andyferris
Copy link
Member

I've done all the ones I can think of... I'll close this but feel free to reopen with further requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants