I posted a comment on the gist with code which probably does the right
thing.  One thing I noted is that you returned a value for the functions
which did in-place modification, I think that may have caused the memory
allocation.

On Wed, 2014-06-25 at 18:57, [email protected] wrote:
> I'm having some trouble understanding some performance issues. I put
> together a minimal example here:
>
> https://gist.github.com/ssfrr/8934c14d8d2703a3d203
>
> I had some methods that were allocating arrays on each call, which I
> figured wasn't very efficient.
>
> My first attempt to improve things was to allocate an array in my main
> container type on initialization, and then share that between function
> calls.
>
> Suprisingly (to me) this slowed things down by about 60x.
>
> I wondered if maybe this was because of the extra dereference to get the
> array (though the slowdown seemed too dramatic for that) so I saved the
> reference to the array in a temp variable before my tight loop.
>
> This slowed down by an additional 7x (more surprises!).
>
> Passing the array as a parameter directly to each function invocation was
> by far the fastest, and was about 2x faster than my original that allocated
> each time. This approach complicates my interface somewhat though, as now
> the caller needs to know how many work buffers the function might need,
> instead of baking that information into the type. I could probably solve
> this with a wrapper function, but I'd like to understand what's going on
> and if there's some sort of type-inference thing I should clean up.
>
> Specifically my questions are:
>
>    1. Why is accessing the array as a parameter so much faster than
>    accessing the array through an object passed as a parameter? As far as I
>    can tell the same type information is there.
>    2. Why does it slow things down so much to store the reference to the
>    array in the beginning of the function and then access that in the tight
>    loop?
>
>
> peace,
> s

-- 

Reply via email to