Fix performance issues for vector-based storage#413
Fix performance issues for vector-based storage#413jeremy-murphy merged 2 commits intoboostorg:developfrom AnthonyVH:anthonyvh/fix_reserve_performance
Conversation
|
Thanks for your pull request.
Yup, that's good.
I don't actually see why this is so, unless |
Everything I'm writing here assumes that TLDR: Calling Now, for the long and windy explanation. Inside
and
I.e. in case the new size is larger than the old, Note that it clearly states that the complexity is linear, not O(1). Growing an Manually calling
I did not see existing performance benchmarks, so I didn't try to implement one. However, the behavior of calling Here is a quick benchmark I threw together illustrating the difference: quick-bench. Note that the difference is only 10% here, most likely because there's not that much data to copy around. I tried creating another benchmark where each entry in the vector is larger (which would be the case when e.g. a vertex has bundled properties that take up a reasonable amount of space). Unfortunately the quick-bench.com website kept timing out on that. I expect the difference to be much more pronounced in this case. If you'd like to try yourself, here's the code in Compiler Explorer (you can hit "Quick bench" in the top left there). Edit: I realize the benchmarks show only a small speed difference. I guess the compiler somehow sees through the whole thing? I know of instances in industrial code where this exact same change caused a massive speed-up. |
|
Here's another snippet from
|
|
Ohh, I get it, sorry, I just completely forgot that |
|
Thanks for a great improvement to the efficiency! I know the code change was very small, but in future please don't combine unrelated changes into one PR. |
|
Thanks a lot for the quick merge! I'll try to better pay attention to the PR hygiene next time 😁. |
|
Btw, whatever differences you can benchmark, I can put in the release notes. At the moment, I think the only thing I can say, and correct me if I get it wrong, is "A 10% improvement to graph construction when using |
|
I've tried creating a benchmark to show the result before and after my changes, but am finding it exceedingly difficult to trick the compiler. So unfortunately for now, I can't present a meaningful result (i.e. a plot clearly showing O(N) vs O(N^2) The fix was only for the vertex list. (I did just see that the same |
|
No worries, thanks for trying. It's hard to outsmart the compiler. 😂 |

vertex_by_property()required a mutable graph object, butvertex_by_property()itself did. This is now fixed.container.reserve(container.size() + 1), e.g.add_vertex(). Calling such anadd_vertex()in a loop with anstd::vectorbacking the vertex storage results in O(N²) time complexity instead of the expected (amortized) O(N). This is fixed by allowing the container to grow as expected.