Skip to content
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

elide more tuple allocations #1976

Closed
timholy opened this issue Jan 10, 2013 · 9 comments
Closed

elide more tuple allocations #1976

timholy opened this issue Jan 10, 2013 · 9 comments
Assignees
Labels
performance Must go faster

Comments

@timholy
Copy link
Sponsor Member

timholy commented Jan 10, 2013

One hope with the sampling profiler is making our code more efficient; the curse may be that it will trigger questions about things that don't actually matter.

In that spirit, I've gotten the impression that anytime a tuple is involved, there's a likelihood of triggering the garbage collector. It's interesting to run the sampling profiler on the following function:

function myfunc(B::Matrix, n::Int)
    local C
    for i = 1:n
        C = chold(B)
    end
    C
end
A = randn(20, 40);
B = A*A';
@sprofile myfunc(B, 200000)

A large fraction of the samples are taken inside the garbage collector. But at least on my machine, they're not where I expected: they seem to occur inside chkstride1.

I can eliminate many of these "weird" garbage collection events by defining additional functions:

function chkstride1(A::StridedVecOrMat)
    if stride(A,1) != 1
        error("LAPACK: Matrix must have contiguous columns")
    end
end
function chksquare(A::Matrix)
    if size(A, 1) != size(A, 2)
        error("LAPACK: Matrix must be square")
    end
end

Then there are still plenty of garbage collection events (so I think this doesn't change performance), but this time they're in a place that seems to make more sense, jl_alloc_array_1d.

If we get to a point where we can re-use memory in assignments, I wonder if the tuple behavior will become a source of trouble?

I'm asking this in part because I noticed, long ago, that gc was the main bottleneck for the Grid module. At the time, I found this very weird because I designed it to do as little memory allocation as possible. I haven't had time to follow this up recently, however.

Anyway, it was something I found curious, and felt I'd ask about it.

@JeffBezanson
Copy link
Sponsor Member

These profiling investigations promise to be very helpful.
All I can say is tuples are heap-allocated, and so can cause GC, no more or less than anything else. We need more tricks in the compiler to elide tuple allocations.

@dmbates
Copy link
Member

dmbates commented Jan 10, 2013

I was probably getting too cute when writing the chkstride and chksquare functions. Having looked at other code, which I believe was written by Jeff, I think that more liberal use of assert() is the better path. I just got tired of writing

if (stride(A,1) != 1) error("Matrix X must have contiguous columns") end
if (stride(B,1) != 1) ...

@JeffBezanson
Copy link
Sponsor Member

assert is great but it shouldn't be used for user-facing errors. An assertion failure means there is a bug in the code containing the assertion.

@toivoh
Copy link
Contributor

toivoh commented Jan 10, 2013

I submitted a pull request a while back with a corresponding @expect macro for user-facing errors, but there didn't seem to be much interest. Or are you saying that user-facing errors should be more informative as well? (a good thing to strive for, of course)

@johnmyleswhite
Copy link
Member

I believe there's a push towards pushing more typed errors forward for handling errors programmatically.

@toivoh
Copy link
Contributor

toivoh commented Jan 10, 2013

That would be nice, but seems to go against the decision not to have typed catch clauses?

@timholy
Copy link
Sponsor Member Author

timholy commented Jan 11, 2013

I would say that informative is good.

If there's no bug that's triggering excess gc, I'll close this. The issue of reducing temporaries/gc will surely be up front-and-center in future optimization efforts.

@timholy timholy closed this as completed Jan 11, 2013
@timholy
Copy link
Sponsor Member Author

timholy commented Jan 12, 2013

Oops, from the fact that someone changed the title, it seems this is better left open. Reopening.

@timholy timholy reopened this Jan 12, 2013
@JeffBezanson
Copy link
Sponsor Member

Dup of #2496 .

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

No branches or pull requests

6 participants