Skip to content

If you are using std::list<>, you are doing it wrong

I am not wasting time here to repeat benchmarks which a lot of people did already.

You think your case is special, a unique snowflake. It is not.

You have another STL data structure that is better than std::list: std::deque<> almost 99% of the time.

In some cases, even the humble std::vector is better than a list.

If you like very exotic alternatives have a look at plf::colony.

But seriously, just use vectoror deque.

Real world example: improving the Intel RealSense driver

This is a practical example of a Pull Request I sent to the RealSense repository a while ago.

They where using that abomination called std::list<> for a reason that I can not understand.

Just kidding, Intel Developers, we love you!

Here you can find the link to the Pull Request: - Considerable CPU saving in BaseRealSenseNode::publishPointCloud()

In a nutshell, the whole PR contains only two tiny changes:

// We changed this list, created at each camera frame
std::list<unsigned> valid_indices;

// With this vector: a class member, that is cleared before reusing
// (but allocated memory is still there)
std::vector<unsigned> _valid_indices;

Additionally, we have a quite large object called sensor_msgs::PointCloud2 msg_pointcloud that is converted into a class member that is reused over and over again at each frame.

The reported speed improvement is 20%-30%, that is huge, if you think about it.