On Windows, Tree maintains its items with a single linked list. For this
reason, "find previous" operation is O(N) because it requires walking
from start of the list.
When inserting an item, it needs to be linked into the list, which
involves finding the previous item and updating its 'next' field, which
is O(N) for one item and O(N*N) for inserting N items.
The workaround is to bulk insert all items at the same position. This
means that if TreeItem had no children, inserting N items into it is
O(N) instead of O(N*N).
On top of that, this patch preallocates 'Tree.items[]' to hold exactly
the required number of items.
Resulting performance improvement on my machine:
| before patch | after patch |
+-----------+-----------+-----------+-----------+
| REGULAR | VIRTUAL | REGULAR | VIRTUAL |
----------------+-----------+-----------+-----------+-----------+
10_000 items | 0,44sec | 0,43sec | 0,07sec | 0,06sec |
100_000 items | 113,83sec | 111,95sec | 0,70sec | 0,63sec |
1_000_000 items | too long | too long | 6,82sec | 6,28sec |
This patch combines:
* Original patch for Bug 575787 (the parts that were not reverted later)
* Fix for Issue #287
* Fix for Issue #333
* Converting some test snippets into JUnit tests
Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>