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:

CollapseCopy 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

时间: 2024-11-02 01:10:24

A Custom Block Allocator for Speeding Up VC++ STL的相关文章

MySQL · TokuDB · rbtree block allocator

背景 Percona在今年8月引入了一个大patch "rb tree block allocation strategy",使用变种的红黑树作为跟踪未使用block hole的数据结构. Percona上对此介绍的连接(点击这里跳转到原文) 这个改进是基于percona内部性能测试和外部用户反馈,发现当TokuDB引擎在长时间写入压力比较大的场景下,随着时间增长写入性能会急剧下降:当采用small node size配置时(这是对高速存储设备场景下性能调优的建议),这个问题会更加严重

【原创】各种 JSON 解析库的功能简介

这里增加一项无聊的对比图,看客自斟. =================================== [rui_maciel/mjson]Last Update:2013-05-15(最新版本为 mjson-1.5 发布日期为 2012-08-22)description M's JSON parser is a small JSON parser written in ISO C which enables the user to handle information describe

分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)

笔者分享总结如下(本篇会不定期进行更新) : Objective-C 1.让Xcode的控制台支持LLDB类型的打印 这有什么用? 怎么说尼,笔者认为这个还是比较有用滴,为什么有用尼? 因为在Xcode断点调试的时候, 在控制台输入 po self.view.frame 或者 po id 类型的时候就死翘翘了.不信? 看如下图 : 进入正题 打开终端输入三条命令: 1. touch ~/.lldbinit 2. echo display @import UIKit >> ~/.lldbinit

[转]GNU Radio Companion - GRC

GNU Radio Companion - GRC 注:该文档适应于捆绑 GNU Radio 的 GRC,它不适应任何独立发行本的 GRC.如果想使用 GRC 0.70 请参阅 GNU Radio Companion (Old) . GNU Radio Companion (GRC) 是一个用来产生信号流程图及流程图源代码的图形化工具.它目前是由 Josh Blum 构建.   新特性  同稳定版 GRC 0.70 不同的,GRC 目前有哪些新特性? 捆绑式发行(Bundled)- GRC 目前

PostgreSQL on ECS多云盘的部署、快照备份和恢复

标签 PostgreSQL , ECS , 云盘 , 快照 , 一致性备份 , 时间点恢复 , zfs , lvm , raid , 并行计算 背景 随着阿里云云盘的发展,云盘的性能已经越来越好了.IOPS可以做到十万以上,读写吞吐也超过1GB/s了.相信随着网络的发展,SSD云盘IOPS突破40万,读写吞吐突破4GB/s也不远了. 不过这里的IOPS和吞吐是指并发的IO能力,单次IO的延迟与本地还是不能比(将来RDMA网络也许能解决这个问题). PostgreSQL 如何解决SSD云盘IO延迟

Apple新发布的APFS文件系统对用户意味着什么

2016年WWDC大会上,Apple除了公布watchOS.tvOS.macOS以及iOS等一系列系统和软件更新外,还公布了一个名为APFS(Apple File System)的文件系统. 这一全新文件系统专门针对闪存/SSD进行优化(但依然可用于传统机械硬盘),提供了更强大的加密.写入时复制元数据.空间分享.文件和目录克隆.快照.目录大小快速调整.原子级安全存储基元,以及改进的文件系统底层技术,将全面应用于该公司旗下所有设备中. 为什么不继续使用HFS+? HFS诞生于1985年,随后在19

阿里数据库内核月报:2016年11月

# 01 PgSQL · 特性分析 · 金融级同步多副本分级配置方法 # 02 MySQL · myrocks · myrocks之事务处理 # 03 MySQL · TokuDB · rbtree block allocator # 04 MySQL · 引擎特性 · Column Compression浅析 # 05 MySQL · 引擎介绍 · Sphinx源码剖析(一) # 06 PgSQL · 特性分析 · PostgreSQL 9.6 如何把你的机器掏空 # 07 PgSQL · 特

Alluxio层次化存储简介

                 一.分层存储种类         1.MEM (内存)         2.SSD (固态硬盘)         3.HDD (硬盘驱动器)         二.分层存储参数                  1.alluxio.worker.tieredstore.levels,缺省值1          Alluxio Worker多层存储中的最大存储级数.当前Alluxio支持1,2,3层.                  2.alluxio.worke

GNU Radio中流图的动态配置

How can I reconfigure a flow graph? How do I use lock(), unlock()?¶ A running flow graph is static, and can't be changed. There are two ways to implement reconfigurability: Use lock() / unlock() Create blocks that react dynamically Using lock() and u