Apparently this was a problem with the script. Is there anything that JavaScript writers can learn from this issue?
This bug has led to a very interesting analysis work, which will allow me to further optimize several key elements of PixInsight's JavaScript runtime.
After a lot of tests and precise execution time measurements, I managed to isolate the origin of this strange performance degradation. Basically, for reasons that I am still investigating, some statistical methods of the Image object start increasing their execution times exponentially after a long sequence of calls (in the range of thousands of function calls). For example, this code is excerpted from the version of StarDetector.jsh that shipped with build 1083, and is part of the inner loop of the star detection engine:
if ( data.bkg == 0 || (data.norm - data.bkg)/data.bkg > this.sensitivity )
if ( wrk.sample( ix, iy ) > 0.85*data.peak )
if ( wrk.median( r ) < this.peakResponse*data.peak )
S.push( new Star( data.pos, data.flux, data.size ) );This code executes for most candidate stars (that is, not only for reported stars, but for much more). This means that this piece of code typically runs in the range from 1000 to 20,000 times for average deep sky images. In red color is one of the function calls that was causing a severe performance degradation. The wrk variable is an Image object.
To fix this problem, I have replaced all calls to Image's statistic methods within the inner loop of the star detection engine. For example, the above code is now:
if ( data.bkg == 0 || (data.norm - data.bkg)/data.bkg > this.sensitivity )
if ( wrk.sample( ix, iy ) > 0.85*data.peak )
{
var m = Matrix.fromImage( wrk, r );
if ( m.median() < this.peakResponse*data.peak )
S.push( new Star( data.pos, data.flux, data.size ) );
}Fortunately, the Matrix object implements all statistical methods of Image, and Matrix.fromImage() is a fast native call without any measurable penalty. The call to Matrix.median() has no performance degradation. Surprisingly, the underlying native C++ code for Image.median() and Matrix.median() is the same (part of PCL). Of course, the corresponding C++ classes in PCL have no performance degradation at all (in fact, pcl::GenericImage<>::Median() is slightly faster than pcl::GenericMatrix<>::Median() because a part of the code is parallelized in GenericImage<>).
I still don't know what's going on here, but I'll investigate it thoroughly. My best bet is that we have some weird interaction with garbage collection in SpiderMonkey 24, but I'm not sure. The fact that this never happened with SM 17 supports this hypothesis. JavaScript engines, especially the last generation ones, are very complex beasts.
So please don't make any premature conclusions, especially
not one of the "Matrix is faster than Image" kind.