-
Notifications
You must be signed in to change notification settings - Fork 2k
Speed optimization: Replace multi-dimentional with jagged arrays in I… #825
Conversation
…ntegralImage and SURF ResponseLayer.
Hi Alexander, Thank you very much for your contribution. It's great to see you speeding things up! - and fixing the typo :) As this is a breaking change, the cost of the speed performance will need to be weighed up against the disruption caused to existing users. @cesarsouza will be best placed to comment there. However, it would be helpful if you could indicate what parts in particular you have seen a speedup in (is it a particular method or do you have a code snippet you could post? etc.). @cesarsouza - I had a very quick read through of this code (so I may very well have missed the bottleneck) but I noticed that these lines here and here could be sped up by using the (mostly pre-existing) unsafe code more effectively. For instance, in the second link, the bounds check on the image could be done once outside the loop and then then the IntegralImage arrays could be fixed - both examples traverse row-wise through the arrays which is ideal. Also - perhaps this can be optimized a little but, as I am not familiar with the underlying code, it is difficult to identify the obvious bottlenecks without a little more info. Thanks again |
Hi everyone! Alexander, thanks a lot for the contribution! And Alex thanks a lot for doing a first pass over it and raising those concerns! @volgunin: This is a very nice contribution, and I think it does not have to be a breaking change. The first observation is that the ResponseLayer is an internal class, so we can modify it as needed without breaking user code. And the second is that, instead of modifying the return type of the As @AlexJCross, I also think that using unsafe code should also help. However, I think it will only help if we pin the integralImage array at the class constructor to avoid fixing it every time the GetRectangleSum() is called. Let me explain: The main method of the IntegralImage class is the GetRectangleSum() method that is often called multiple times at once (i.e. such as in the @AlexJCross: Just to address some of the other points you had raised, the "CheckBounds" method you see is actually marked with a Regards, |
Nice one @cesarsouza - a lot of good points there. I should have read the code a bit slower :) I missed both the internal and conditional! I never knew the |
oops, sorry i closed and reopened this. that's just my fat fingers hitting the wrong button - I do it all the time! As I was saying, jaggeds are definitely faster for general use so it sounds like the way to go here. I don't have massive familiarity with this code but if it's mission critical and we'd like to find out the results of constructor pinning, I'm sure I could cook up a benchmark. let me know. Finally, @cesarsouza, I have also not looked at SIMD for a while. From my recollection, I thought it was rather limited to the Thanks, |
Thanks everyone for looking into this. I missed that For The method that benefits from change is |
Hi @volgunin, Thanks a lot, after those small changes the PR could be accepted right away. Thanks a lot for the contribution! And @AlexJCross thanks a lot for the help! Regards, |
…d compartibility.
Hi everyone! I just added Matrix property as promised. Regards, |
Hi @volgunin, It is amazing! Thanks a lot for your contribution! Regards, |
Hi,
I optimized the performance of SURF feature detector a little bit by using jagged instead of multi-dimensional arrays in IntegralImage and SURF ResponseLayer.
My tests show ~10-15% performance gain, which is due to the poor implementation of multi-dimensional arrays in CLR. Microsoft recommends using jagged arrays instead.
This is apparently a braking change as some public properties in IntegralImage and ResponseLayer class were affected.
Regards,
Alexander