Reputation: 457
I believe it is not practical to access to implementation of Standard Library of C++ because it is platform-dependent (correct me if this sentence was wrong!). Well, I wanted practice about some very low level (at least, as low level as I can program in C++) things in C++. So, I started to write an Allocator class, just to practice, learn and if I create something useful I plan to use it in my real projects.
My class looks okay. It is as fast as classical Base* pointer = new Derivative;
and pointer->~Base(); ::operator delete[] (pointer);
allocation, construction, destruction and deallocation policy. In fact for 10000000 iterations my class is 0.1 second slower.
The reason I am asking this question is: std::allocator. For some it can be slow for specific reasons; but merely comparing with my implementation, it is very fast. Please observe my code:
int main()
{
for(int x=0; x<10000000; x++)
{
/* TEST 1: Using my Allocator class
A* a = Allocator<B>::construct(10,3.2);
Allocator<A>::destruct(a);
//*/
/* TEST 2: Using operators 'new' and 'delete[]'
A* a = new B(10,3.2);
delete a;
//*/
/* TEST 3: std::allocator
std::allocator<B> allocator;
std::allocator<A> deallocator;
A* a = allocator.allocate(1);
allocator.construct(a,10,3.2);
deallocator.destroy(a);
deallocator.deallocate(a,1);
//*/
}
}
Additional important notes about my code: class A is the base of class B. Allocator<T>::construct(args...)
and Allocator<T>::destruct(pointer)
are static inline methods. The first one both allocates memory (enough for only one object) and constructs object in this memory location (by using arguments). The second one first destructs objects then deallocates the memory location. All three tests result (as far as I debugged) exactly the same and work. I use this loop with those 3 seperate tests (one test for each time).
Here are the results for 10000000 iterations:
Test 1: app. 1.550 seconds
Test 2: app. 1.450 seconds
Test 3: app. 1.200 seconds
(Note: these numbers are approximate numbers)
Well, std::allocator is way faster than both. Maybe there isn't a huge distinction but I want to know the reason of this distinction. I am very curious about the implementation of std::allocator. Thank you for your help and time.
Additional information: I am using GCC 4.7.2 under Ubuntu Quantal Quetzal.
Upvotes: 0
Views: 1269
Reputation: 1889
The std:allocator will be reserving a pool of memory and doesn't need to do a system call every time you call construct on the other hand every time we do a new memory is allocated by system
Upvotes: 1
Reputation: 1915
You're going to have to learn about branch prediction and CPU pipelining, low time-complexity algorithms for internal implementations, along with cache coherency in order to make something highly optimized. I don't know about std::allocator
's implementation, but judging from your question the things I've mentioned are areas to improve performance dramatically.
Edit: If std::allocator
uses new
and delete
like the comments suggest, then it's quite easy to make a much more specific and optimized allocator with some memory pooling.
Upvotes: 1