8

During my daily work, I am always advised by senior member of the team that list is not cache friendly so I should vector. I understand that list is not continuous so that the memory allocation is scattered around the whole memory.

However, very often I do need the functionality of a list (or a map). So I am wondering if I can write my own allocator, which is a vector underneath. Every time when I push_back, my own allocator will allocate a new item from a per-allocated vector.

When I travel the list/map, the cache locality is preserved.

Does this make sense to any of you?

11
  • 2
    std::list isn't an associative container. Commented Mar 29, 2017 at 21:19
  • 1
    What your looking for is called a stack allocator Commented Mar 29, 2017 at 21:24
  • Obvious question: why not just use a vector? What does this construction give you that a vector doesn't? If you try to use any of the functionality that's ordinarily more efficient on a list than a vector, you'll leak memory. Commented Mar 29, 2017 at 21:27
  • I have a roughly fixed size list, i constantly need to insert/erase in the middle of it, i don't think this can be easily replaced by a vector Commented Mar 29, 2017 at 21:28
  • 2
    Do some benchmarks. For 500,000 elements vector out performed list (and map) doing random insert deletes. Commented Mar 29, 2017 at 21:34

1 Answer 1

1

std::list and std::set (I believe you need set as alternative to list, not a map) both will use allocator for there internals. You can preallocating a block of memory and use it to create your objects and containers. If you google, you will find several. In this case your objects instead if "scattered around the whole memory" will be scattered around your block of memory. If block fit on the cache, you will get some improvement. But it will not completely solve you problem.

From description of the problem, you really need deque. Deque is implemented as a list of arrays. It is compromise between vector and a list. It is cache friendly for iteration and faster then array when you insert.

So you can choose either custom allocator or deque, depends on your collection size.

image

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.