From 6a0f6c40cd00f16fd3d145db1ae0f897da716965 Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Thu, 25 May 2017 15:04:40 +0200 Subject: [PATCH] Fix quadratic-time behaviour of vector extend The "extend" method for vectors defined in stl_bind.h used `reserve` to allocate space for the extra growth. While this can sometimes make a constant-factor improvement in performance, it can also cause construction of a vector by repeated extension to take quadratic rather than linear time, as memory is reallocated in small increments rather than on an exponential schedule. For example, this Python code would take time proportional to the square of the trip count: ```python a = VectorInt([1, 2, 3]) b = VectorInt() for i in range(100000): b.extend(a) ``` This commit removes the `reserve` call. The alternative would be to try to add some smarter heuristics, but the standard library may well have its own heuristics (the iterators are random access iterators, so it can easily determine the number of items being added) and trying to add more heuristics on top of that seems like a bad idea. --- include/pybind11/stl_bind.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index e581b73eb..49dc9a544 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -137,7 +137,6 @@ void vector_modifiers(enable_if_t