Excessive dynamic allocation when using boost::split(..., boost::is_any_of(", "))?

Dominique Pellé via Boost-users <[email protected]>
Newsgroups gmane.comp.lib.boost.user
Message-ID <CAON-T_iFbG-HSE8CcoSwy+wZZFJfk=e=Sd7fpEYNr0SFHOuTbA@mail.gmail.com>
Hi

This following program shows many allocations coming
from boost::split(..., boost::is_any_of(",")):
```
$ cat split.cpp

#include <string>
#include <cstdio>
#include <boost/algorithm/string.hpp>
#include <boost/container/small_vector.hpp>

int main() {
  std::string str = "foo,bar,foobar,quux";
  std::size_t s = 0;
  for (int i = 0; i < 100; ++i) {
    boost::container::small_vector<std::string, 4> v;
#ifdef USE_IS_ANY_OF
    boost::split(v, str, boost::is_any_of(","));
#else
    boost::split(v, str, [](const char c) { return c == ',';});
#endif
    s += v.size();
  }
  fprintf(stderr, "%zu\n", s);
}
```
When using boost::is_any_of(","), there are 7 dynamic allocations
per loop iteration whereas when using the lambda predicate,
there are 0 dynamic allocation per loop as shown below:
```
$ clang++ -std=c++14 -DUSE_IS_ANY_OF split.cpp
$ heaptrack ./a.out
  ...
  allocations: 702

$ clang++ -std=c++14 split.cpp
$ heaptrack ./a.out
   ...
  allocations: 2
```

I suspect that there are potential optimizations in Boost here to
avoid so many allocations when boost::is_any_of(",") is used.

Regards
Dominique
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.