Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an allocation hint get used?

Tags:

c++

allocator

I was reading Why is there no reallocation functionality in C++ allocators? and Is it possible to create an array on the heap at run-time, and then allocate more space whenever needed?, which clearly state that reallocation of a dynamic array of objects is impossible.

However, in The C++ Standard Library by Josuttis, it states an Allocator, allocator, has a function allocate with the following syntax

pointer allocator::allocate(size_type num, allocator<void>::pointer hint = 0)

where the hint has an implementation defined meaning, which may be used to help improve performance.

Are there any implementations that take advantage of this?

like image 841
Alex Chamberlain Avatar asked Mar 13 '13 08:03

Alex Chamberlain


3 Answers

I have gained significant performance advantages for iteration times on small scalar types in my plf::colony c++ container using hints with std::allocator under Visual Studio 2010-2013 (iteration speed increased by ~21%), and much smaller speedups under GCC 5.1. So it's safe to say that with those compilers and std::allocator, it makes a difference. But the difference will be compiler-dependent. I am not aware of the ratio of hint-ignoring to hint-observing allocators.

like image 147
metamorphosis Avatar answered Nov 07 '22 22:11

metamorphosis


I'm not sure about specific implementations, but note that the allocator isn't allowed to return the hint pointer value before it's been passed to deallocate. So that can't be used as a primitive operation to form a reallocate.

The Standard says the hint must have been returned by a previous call to allocate. It says "The use of [the hint] is unspecified, but it is intended as an aid to locality." So if you're allocating and releasing a sequence of similar-sized blocks on one thread, you might pass the previously-freed value to avoid cache contention between microprocessor caches.

Otherwise, when CPU B sees that you're using memory addresses still in CPU A's cache (even that memory contains objects that were destroyed according to C++), it must forward the junk data over the bus. Better to let CPU A and B each reuse their own respective cached addresses.

like image 2
Potatoswatter Avatar answered Nov 07 '22 21:11

Potatoswatter


C++11 states, in 20.6.9.1 allocator members:

4 - [ Note: In a container member function, the address of an adjacent element is often a good choice to pass for the hint argument. — end note ]
[...]
6 - [...] The use of hint is unspecified, but intended as an aid to locality if an implementation so desires.

Allocating new elements adjacent or close to existing elements in memory can aid performance by improving locality; because they are usually cached together, nearby elements will tend to travel together up the memory hierarchy and will not evict each other.

like image 1
ecatmur Avatar answered Nov 07 '22 22:11

ecatmur