I had a really bad couple of days. I am pissed, annoyed and angry, for totally not technical reasons.
And then I run into this issue, and I just want to throw something really hard at someone, repeatedly.
The issue started from this bug report:
At first, I didn’t really realized why it was my problem. I mean, it is NTS problem, isn’t it?
Except that this particular issue actually crashed ravendb (don’t worry, it is unstable builds only). The reason it crashed RavenDB? An unhandled thread exception.
What I can’t figure out is what on earth is going on. So I took a look at the code, have a look:
I checked, and this isn’t code that has been ported from the Java code. You can see the commented code there? That is from the Java version.
And let us look at what the execute method does?
So let me see if I understand. We have a list of stuff to do, so we spin out threads, reclusively, then we wait on them. I think that the point was to optimize things in some manner by parallelizing the work between the two halves.
Do you know what the real killer is? If we assume that we have a geometry with just 20 items on it, this will generate twenty two threads.
Leaving aside the issue of not handling errors properly (and killing the entire process because of this), the sheer cost of creating the threads is going to kill this program.
Libraries should be made to be thread safe (I already had to fix a thread safety bug there), but they should not be creating their own threads unless it is quite clear that they need to do so.
I believe that this is a case of a local optimization for a specific scenario, it also carry all of the issues associated with local optimizations. It solves one problem and opens up seven other ones.