Efficient linked list in C++?

后端 未结 11 628
孤街浪徒
孤街浪徒 2021-02-02 06:36

This document says std::list is inefficient:

std::list is an extremely inefficient class that is rarely useful. It performs a heap allocation

11条回答
  •  独厮守ぢ
    2021-02-02 07:18

    Your requirements are exactly those of std::list, except that you've decided you don't like the overhead of node-based allocation.

    The sane approach is to start at the top and only do as much as you really need:

    1. Just use std::list.

      Benchmark it: is the default allocator really too slow for your purposes?

      • No: you're done.

      • Yes: goto 2

    2. Use std::list with an existing custom allocator such as the Boost pool allocator

      Benchmark it: is the Boost pool allocator really too slow for your purposes?

      • No: you're done.

      • Yes: goto 3

    3. Use std::list with a hand-rolled custom allocator finely tuned to your unique needs, based on all the profiling you did in steps 1 and 2

      Benchmark as before etc. etc.

    4. Consider doing something more exotic as a last resort.

      If you get to this stage, you should have a really well-specified SO question, with lots of detail about exactly what you need (eg. "I need to squeeze n nodes into a cacheline" rather than "this doc said this thing is slow and that sounds bad").


    PS. The above makes two assumptions, but both are worth investigation:

    1. as Baum mit Augen points out, it's not sufficient to do simple end-to-end timing, because you need to be sure where your time is going. It could be the allocator itself, or cache misses due to the memory layout, or something else. If something's slow, you still need to be sure why before you know what ought to change.
    2. your requirements are taken as a given, but finding ways to weaken requirements is often the easiest way to make something faster.

      • do you really need constant-time insertion and deletion everywhere, or only at the front, or the back, or both but not in the middle?
      • do you really need those iterator invalidation constraints, or can they be relaxed?
      • are there access patterns you can exploit? If you're frequently removing an element from the front and then replacing it with a new one, could you just update it in-place?

提交回复
热议问题