Performance improvements in compute_quantities()
There are a couple of possible optimizations in compute_quantities() method which are worth adding because the method is low level and frequently used, potentially with a large number of locations and products (and lots).
On the one side, the algorithm to compute quantites browses the location tree structure by cumulating from leafs towards up the tree. At the end, it needs to cleanup all quantities that have been cumulated just because an upper location in the tree required it.
However, we could in the first place cumulate quantities only for locations we need. For example, in the following structure:
1
|-> 1.1
|-> 1.1.1
|-> 1.1.2
|-> 1.2
|-> 1.2.1
|-> 1.2.2
If we were only asked for the stock of location '1' we could consider that the parent of 1.1.1, 1.1.2, 1.2.1 and 1.2.2 is 1, instead of 1.1 and 1.2. Because we actually don't need the cumulated values at 1.1 and 1.2.
This optimization makes the computation of cumulative values faster and also avoids removing many entries in the quantities dictionary.
In a real case we've seen the number of items to be computed reduced to half compared with current implementation. Also, the time for removing quantities was greatly reduced.
Another possible optimization is simply reducing the number of accesses to the quantities dictionary. As the number of entries can be quite large, this reduction can have an important impact on performance.