`
tibaloga
  • 浏览: 877972 次
文章分类
社区版块
存档分类
最新评论

A Custom Block Allocator for Speeding Up VC++ STL

 
阅读更多

Introduction

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:

Collapse Copy Code

// block allocated list of ints with chunks of 1024 elements
std::list > 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 -> block_allocated_list
  • set -> block_allocated_set
  • multiset -> block_allocated_multiset
  • map -> block_allocated_map
  • multimap -> block_allocated_multimap

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.

License

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

About the Author

Joaquín M López Muñoz

Member

分享到:
评论

相关推荐

    彻底学习STL中的Allocator

    关于stl中的Allocator的深入了解

    stl allocator(stl 分配器)

    代码实现了大的内存池以及由此实现的stl allocator代码。

    CSAPP lab 5 实验指导书

    In this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version of the malloc and free routines. It is quite involved. Start early! We are providing some extra ...

    A Fast Memory Allocator -- 快速内存分配器

    A Fast Memory Allocator -- 快速内存分配器 v0.3 基于Python obmalloc模块,改变了block与pool的缓存方式,将arena由原来的两种状态明确处理成三种状态——empty、usable、full,使得以统一的方式处理pool与arena...

    自定义STL std :: allocator替换可提高性能

    通过替代STL std :: allocator的固定块来防止堆碎片错误并提高执行速度

    CSAPP Lab 5:Writing a Dynamic Storage Allocator实验材料

    《深入理解计算机系统》课程的实验5材料 解答过程在:http://blog.csdn.net/u010560443/article/details/50611251

    The Slab Allocator An Object-Caching Kernel Memory Allocator

    The Slab Allocator An Object-Caching Kernel Memory Allocator

    Hoard - A Scalable Memory Allocator for Multithreaded Applications (berger-asplos2000)-计算机科学

    Hoard: A Scalable Memory Allocator for Multithreaded ApplicationsEmery D. Berger∗ Kathryn S. McKinley† Robert D. Blumofe∗ Paul R. Wilson∗∗Department of Computer Sciences The University of Texas ...

    STL 入门最全的资料

    ISO字符串、各种不同类型的容器(container)、模板(template)、游标(Iterator)、算法(Algorithms)、分配器(Allocator)、容器的嵌套等方面的问题,作者在这篇文章中对读者提出了一些建议,并指出了使用STL时...

    标准模板库STL

    一份讲解全面的标准模板库STL学习资料 标准模板库STL主要由6大组件组成: (1)容器(Containers)。包括各种基本数据结构的类模板。 STL容器部分主要由头文件、、、、、和组成。 (2)算法(Algorithms)。...

    STL源码剖析.pdf(侯捷,完整版,已加全书签)

    本書假設你對STL 已有基本認識和某種程度的運用經驗。因此除了第㆒章略作介 紹之外,立刻深入STL 技術核心,並以STL 六大組件(components)為章節之進 行依據。以㆘是各章名稱,這樣的次序安排大抵可使每㆒章所剖析...

    Dynamic Storage Allocator

    Malloc Lab: Writing a Dynamic Storage Allocator

    Linux Slub Allocator工作原理

    Linux中Slub allocator工作原理介绍

    c++ allocator 实现

    linux下c++ allocator 共享内存,内存池实现

    Effictive STL CHM中文版

    》灰《《常好的STL教程Effective STL 目录 容器 条款1: 仔细选择你要的容器 条款2: 小心对“容器无关代码”的幻想 条款3: 使容器里对象的拷贝操作轻量而正确 条款4: 用empty来代替检查size是否为0 条款5: ...

    STL源码剖析完整版

    疱丁解牛(侯捷自序) 前言 第1章 STL概论与版本简介 第2章 空间配置器(allocator) 第3章 迭代器(iterators)概念与traits编程技法 第4章 序列式容器(sequence containers) 第5章 关联式容器(associattive ...

    STL源码剖析.pdg

    庖丁解牛(侯捷自序) i ...附录a 参考资料与推荐读物(bibliography) 461 附录b 侯捷网站简介 471 附录c stlport 的移植经验(by 孟岩) 473 borland c++builder 5 474 microsoft visual c++ 6.0 477 索引 481

    49.Loki_allocator总结

    49.Loki_allocator总结

    C++ STL开发技术导引(第5章)

    4.4.6 内存分配器(Allocator) 56 4.4.7 概念(Concept)和模型(Model) 56 4.5 C++ STL存在的一些问题 57 4.6 本章小结 57 第5章 C++ STL泛化技术分析 58 5.1 算法和迭代器 58 5.1.1 算法 58 5.1.2 迭代...

    c++ Effective STL(中文+英文)

    Understand the legitimate uses of custom allocators........................40 Item 12. Have realistic expectations about the thread safety of STL containers. 43 容器 条款1: 仔细选择你要的容器 ...

Global site tag (gtag.js) - Google Analytics