Click here to Skip to main content
11,411,241 members (64,403 online)
Click here to Skip to main content

A Custom Block Allocator for Speeding Up VC++ STL

, 30 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A block allocator for use with STL containers that greatly improves speed in programs doing massive data insertions and extractions.


block_allocator is a custom STL allocator for use with STL as implemented in Microsoft VC++. Rather than doing allocations on a per-node basis, block_allocator allocates memory in fixed sized chunks, and delivers portions of these chunks as requested. Typical speed improvements of 40% have been obtained with respect to the default allocator. The size of the chunks, set by the user, should not be too little (reduced speed improvements) nor too large (memory wasted). Experiment and see what sizes fit best to your application.

block_allocator can substitute for the default allocator in the following containers:

  • list,
  • set,
  • multiset,
  • map,
  • multimap,
and WON'T work with other containers such as vector or queue. Note however that vector and queue already perform allocation in chunks. The usage of block_allocator is fairly simple, for instance:
// block allocated list of ints with chunks of 1024 elements
std::list<int,block_allocator<int,1024> > l;
Normal containers and block allocated containers can coexist without problems.

Compatibility mode with MSVC++ 6.0/7.0

Due to limitations of the standard library provided with these compilers, the mode of usage explained above does not work here. To circumvent this problem one must proceed as follows: For each of the containers supported, there's an associated block allocated container derived from it thru use of block_allocator. You have to define an activating macro for each container to be defined prior to the inclusion of blockallocator.h:

  • list -> block_allocated_list (macro DEFINE_BLOCK_ALLOCATED_LIST),
  • set -> block_allocated_set (macro DEFINE_BLOCK_ALLOCATED_SET),
  • multiset -> block_allocated_multiset (macro DEFINE_BLOCK_ALLOCATED_MULTISET),
  • map -> block_allocated_map (macro DEFINE_BLOCK_ALLOCATED_MAP),
  • multimap -> block_allocated_multimap (macro DEFINE_BLOCK_ALLOCATED_MULTIMAP),

To use block allocation based STL in your application, define the corresponding activating macro, include blockallocator.h and then change your declarations as follows:

  • list<type> -> block_allocated_list<type,chunk_size>
  • set<key> -> block_allocated_set<key,chunk_size>
  • multiset<key> -> block_allocated_multiset<key,chunk_size>
  • map<key,type> -> block_allocated_map<key,type,chunk_size>
  • multimap<key,type> -> block_allocated_multimap<key,type,chunk_size>

where chunk_size is the size of the chunks. You can enter too the other optional template parameters (see MSVC++ STL docs for more info).

The MSVC++ 6.0/7.0 compatibility mode can also be used in MSVC++ 7.1, so you need not modify your block_allocator-related code when porting legacy code to 7.1.

Multithreading issues

Each block allocated container instance uses its own block_allocator, so no multithreading problems should arise as long as your program conveniently protects their containers for concurrent access (or if no two threads access the same container instance). This is the same scenario posed by regular STL classes (remember operations on containers are not guarded by CRITICAL_SECTIONs or anything similar), so the moral of it all is: If your program was multithread safe without block_allocator, it'll continue to be with it.

Version history

  • 29th Feb, 2000 - 1.1
    • Initial release in CodeProject.
  • 22nd Mar, 2001 - 1.2
    • Included definitions for operator== and operator!=. The lack of these caused linking errors when invoking list::swap() and similar methods. The funny thing about it is that no one ever reported this seemingly important bug, so either swap() is not that much used or not that many people use block_allocator!
  • 25th Oct, 2006 - 1.3
    • block_allocator now works with MSVC++ 7.1 and 8.0. Thanks to James May for helping with testing this new version of the code.
  • 30th Oct, 2006 - 1.4
    • Fixed some typedefs incorrectly made private in block_allocated_list, block_allocated_set, etc.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

GeneralRe: Anyone tried this with VS .net? PinmemberJoão Paulo Figueira20-Oct-03 1:10 
GeneralRe: Anyone tried this with VS .net? PinmemberJoaquín M López Muñoz20-Oct-03 1:36 
AnswerRe: Anyone tried this with VS .net? PinmemberRajg27-Jun-04 0:40 
GeneralRe: Anyone tried this with VS .net? PinsussAnonymous16-Jul-05 3:59 
GeneralChange in behaviour of list::splice PinmemberBrad Davis19-Apr-02 16:31 
GeneralRe: Change in behaviour of list::splice PinmemberJoaquín M López Muñoz20-Apr-02 7:35 
QuestionWill this decrease size? PinmemberRusty19-Feb-02 14:39 
AnswerRe: Will this decrease size? PinmemberJoaquín M López Muñoz19-Feb-02 21:10 
Does this custom allocator decrease the memory usage of a list(map, or set)?

On a first approach, no it doesn't. When an allocator is requested n bytes of fresh memory, it has no other option but to provide n bytes of fresh memory, so memory efficiency can only result from second-order effects:
  1. Alignment issues. An allocator reserving memory on a per-object basis can incur a constant alignment overhead (e.g. reserving always 32 bytes when requested 31). IMHO, in release mode this will only happen in very bad designed allocators; I don't think malloc based allocation suffers from this defect.
  2. Memory fragmentation. This can actually be an issue. Reserving memory in larger chunks prevents the formation of many unusable little blocks that can fragment memory to the point that the practical limit of total memory seen by the program is much less than theoretically possible. Here, block allocators have a reputation for doing a good job. Furthermore, block allocation improves locality and hence, due to cache considerations, speed.
Anyway, I'd suggest you conduct some tests to see how block_allocator performs with respect to memory efficiency in your particular application.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Generalusing with dinkumware library PinmemberThomas George14-Feb-02 11:26 
GeneralRe: using with dinkumware library PinmemberJoaquín M López Muñoz14-Feb-02 21:41 
QuestionHow can such an allocator be used in a vector? PinmemberThomas George5-Feb-02 6:32 
AnswerRe: How can such an allocator be used in a vector? PinmemberJoaquín M López Muñoz5-Feb-02 7:01 
Generalblockallocator bug PinmemberIng. Capelli Carlo6-Mar-01 11:20 
GeneralRe: blockallocator bug PinmemberJoaquín M López Muñoz7-Mar-01 22:58 
Generalblockallocator bug: closer inspection and proposed workaround PinmemberJoaquín M López Muñoz2-Oct-01 1:12 
QuestionIs the allocator thread-safe? PinmemberAnonymous29-Dec-00 6:52 
AnswerRe: Is the allocator thread-safe? PinmemberJoaquín M López Muñoz2-Oct-01 1:20 
General12% map speed improvement achieved by using this approach PinmemberLeonid Medvedev26-Nov-00 6:18 
QuestionWhere is the speedup? PinsussJim16-May-00 3:12 
AnswerRe: Where is the speedup? PinsussJoaquín M López Muñoz16-May-00 4:18 
AnswerRe: Where is the speedup? PinmemberJoaquín M López Muñoz8-Mar-01 22:23 
GeneralRe: Where is the speedup? PinmemberSteven Schimmelpfennig19-Apr-01 15:19 
GeneralRe: Where is the speedup? PinmemberLothar6-Sep-01 7:44 
GeneralRe: Where is the speedup? PinmemberRusty14-Sep-01 10:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 31 Oct 2006
Article Copyright 2000 by Joaquín M López Muñoz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid