473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Should I define my own allocator?

ddh
Hi,

I have a map type such as std::map<int, int>, which I used to manage
many values in a fixed order. But I have thousands of values to be
inserted into the map, and in some case, the action of inserting and
re-building is very frequent, that say, many times per second.

I've made some tests to insert 1500 values into a map, it cost about
30ms in my athlon XP1800+. I think it shouldn't be so expensive. When I
do an inserting action, I think that an operator 'new' is called to
allocate very little memory, so about 1500 times 'new' is called. Maybe
it is the reason which make the action so expensive?

Now my question is: is it worth to implement my own allocator to
allocate memory in such a situation? And if it is worth, could you give
me some advice about how to write an allocator?

Thank you very much for your help :-)

Dec 30 '05 #1
5 2717
ddh,

It depends on what you're using this class for, and how big of a delta
you'd expect in performance by writing your own allocator
(unfortunately non-trivial to determine a priori). If this is
something you're using within the context of a larger application local
to you and a small-to-medium sized set of colleagues, I'd say first run
a profiler on the app as a whole to see how much of a bottleneck the
map is. Don't optimize part of a large system if it isn't slowing the
system down appreciably!

On the other hand, if you're publishing this as a standalone library or
something, intended for wide use, a custom allocator is worth
considering. Of course, I wonder why anyone would prefer your
implementation over std::map, but that's for you to answer.

For advice on writing an allocator like this, I recommend that you
check out chapter 4 of Andrei Alexandrescu's "Modern C++ Design." It
discusses, in great detail, the creation of an allocator for large
numbers of small objects. May be just what you need. There's probably
also a fair number of such allocator implementations freely available,
if you look around. If you can use one directly, fantastic. If not,
you may be able to at least temporarily plug it in so you can get some
perf numbers and decide if it's worth the effort to roll your own.

Best of luck,
Luke

Dec 30 '05 #2
ddh wrote:
When I
do an inserting action, I think that an operator 'new' is called to
allocate very little memory, so about 1500 times 'new' is called. Maybe
it is the reason which make the action so expensive?

Now my question is: is it worth to implement my own allocator to
allocate memory in such a situation? And if it is worth, could you give
me some advice about how to write an allocator?


It seems you might be looking for a "pool allocator".
(It's just a class calls new to get batches of say a 1000
items at a time, then hands them over one by one as you request).

You could consider just using a std::vector for your objects
(it doubles the allocated size each time it runs out, but does
reallocation and copying, which may or may not suit you.)

There are also several such libraries available,
so you don't need to write your own.
You could try the pool library in Boost, see
http://www.boost.org/libs/pool/doc/index.html

Look for the pool_alloc or pool_allocator object - it can be
used as a drop-in replacement for std::allocator.

Cheers,

homsan
Dec 30 '05 #3

ddh wrote:

[]
Now my question is: is it worth to implement my own allocator to
allocate memory in such a situation? And if it is worth, could you give
me some advice about how to write an allocator?


Profile your code to find hot spots.

Linux malloc, for example, has fast bins for objects smaller than a
certain limit (64 is a common value), which is essentially a pool
allocator.

Dec 31 '05 #4
ddh
Thank you, and I've read some articles about it. To implement an
allocator is not difficult, but to implement an efficient & bug-free
one is somewhat difficult. So I want to do this after I finish all my
other work.

Thank you
Luke Meyers wrote:
ddh,

It depends on what you're using this class for, and how big of a delta


Jan 2 '06 #5
ddh

homsan toft wrote:


It seems you might be looking for a "pool allocator". Yes, it is what I want.

You could consider just using a std::vector for your objects I must sort the data, so I choice std::map.

http://www.boost.org/libs/pool/doc/index.html Thank you very much, and I will read this document

Thank you :-)

Jan 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static...
13
by: John Harrison | last post by:
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the...
2
by: Joshua Kolden | last post by:
STL allocators are templates so that when you write one you are obliged to make it work with any type. However, the Intel IPP library that we use has memory aligned allocators for each of 15...
1
by: Fred Zwarts | last post by:
I think I am missing something obvious. Maybe someone can set me on the right track. For some reason (not to be discussed here) I cannot use the normal operator new for a certain map, so I want...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
7
by: Grahamo | last post by:
Hi, can anybody tell me where I can get the boiler plate code for std::allocator. I need to have my version of new and delete called and want to get reference code. My compilers headers are all...
6
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef...
2
by: * Tong * | last post by:
Hi, I'm following the example in "C++ Templates: The Complete Guide", section 5.4 Template Template Parameters. It's basics/stack8.hpp example has a statement of "#include <allocator>", but I...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.