473,790 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2733
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
1880
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 member work here??: // static std::allocator<T> alloc;
13
1951
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 correct type? For example I tried the following code which uses a 'wrong' allocator and was slightly surrised to find it compiles on the three compilers I tried it on #include <vector> #include <memory>
2
3448
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 different types. For example an 8 bit unsigned array is allocated with ippsMalloc_8u(size). So I want to create memory aligned allocators for use with the STL (in particular the vector container) that is fast (due to alignment), and pointer...
1
1469
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 to replace the memory allocation for this particular map. I have the impression that the template parameter Allocator is there for this purpose. A map is declared as template <class Key, class T, class Compare = less<Key>
31
3744
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 malloc package to find out how big an allocated block is". ( Question 7.27) Is there somwhere explained why - because it would seem to me, that free()
7
3081
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 over the place with #ifdefs and what not, I'd like a clean copy. thanks much
6
13861
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 std::list<intList_t; // default allocator //typedef std::list<int, boost::pool_allocator<int List_t;
2
2479
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 got an error for it. $ cat -n allocator.cc 1 #include <allocator>
16
2703
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 allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.