Currently, we're querying each item again and again, from the first one to the last one, and again from the first one to the last one. This way of checking each entry again and again could be made much more efficient. As I got an idea how to realize this, I decided to write this post to describe my idea for a more efficient algorithm.
It would probably be much more efficient to query only those list entries that really need attention. That's why I would like to propose a new algorithm that scales much better (or at least I think it does) than the current system.
There are two ways of implementing my Idea. A lazy and easy one and a more complicated, probably more efficient and more difficult to implement one.
The easy one is to maintain two (sorted) lists. The first of these lists will contain entries representing all active downloads , while the second one will contain all queued or currently inactive downloads.
Let's start with the second list. It contains - as already said - all currently queued downloads, sorted according to their position in the download list. Once the length of the first list drops below the amount of active downloads (according to the settings), the first entry of this list is moved to the first one.
The first list contains one entry for each active download. Each entry is composed of the download and of a time stamp (date and time) representing the next time this download requires attention. In case this list is always maintained in a way that it is kept in sorted order, we only need to check the first entry against the current time and once both match, the first download is checked/updated etc. After having checked it, it will get a new time/date time stamp and be resorted into the list.
This makes it possible to only query/update those downloads that currently need attention.
The second way of doing it is almost identical, but the lists contain not the downloads themselves, but the actions to do with these downloads. This increases the amount of work to implement and to maintain the list, but it diminishes the work needed to be done once an element advances to the first place of the list (and the time matches the current time).