fritzy/SleekXMPP — pull request #244 by anton-ryzhov (2013-06-20 15:14:08+00:00)
Title: Speedup main threads loops

I'm using this library in application which runs lots of weak loaded xmpp connections. I've expected that it shouldn't consume lots of resources, but this is not true.

I've found that all 4 main threads almost never sleeps and always loops over and over. If I correctly understand, they all breaks waiting (net IO or get from Queue) to check stop Condition. 3 loops checks if every 100 ms, and one - every 1 second. In result all threads can be stopped in 0..1 second (after latest one). I think this is reasonable time to stop. But other threads checks stop flags faster, and they have no reason to do that.

I've made a patch to reduce useless CPU consuming:
* All threads use same config variable as check rate (WAIT_TIMEOUT).
* I've increase timeouts in some threads, but it doesn't affect total time to stop.
* I've replaced magic numbers by named properties in main loops.
* There was no reason in scheduler to mandatory break loop every 3 seconds. Now it goes through small loop until time of next event.
* There was a bug in loop, it couldn't loop over small loop more than one time because of QueueEmpty exception.

I've benchmarked upstream and patched version on idle mode. After 2 hours of work patched version consumed 2.3 times less CPU.

I'm not sure that I totally understand entire library, maybe you had some reasons to implement it that way. I want to know if I'm doing something wrong.

If this patch is OK, maybe I'll continue some optimization work later.