folly/small_vector.h

folly::small_vector<T,Int=1,...> is a sequence container that implements small buffer optimization. It behaves similarly to std::vector, except until a certain number of elements are reserved it does not use the heap.

Like standard vector, it is guaranteed to use contiguous memory. (So, after it spills to the heap all the elements live in the heap buffer.)

Simple usage example:

    small_vector<int,> vec;
vec.push_back(); // Stored in-place on stack
vec.push_back(); // Still on the stack
vec.push_back(); // Switches to heap buffer.

Details


This class is useful in either of following cases:

  • Short-lived stack vectors with few elements (or maybe with a usually-known number of elements), if you want to avoid malloc.

  • If the vector(s) are usually under a known size and lookups are very common, you'll save an extra cache miss in the common case when the data is kept in-place.

  • You have billions of these vectors and don't want to waste space on std::vector's capacity tracking. This vector lets malloc track our allocation capacity. (Note that this slows down the insertion/reallocation code paths significantly; if you need those to be fast you should use fbvector.)

The last two cases were the main motivation for implementing it.

There are also a couple of flags you can pass into this class template to customize its behavior. You can provide them in any order after the in-place count. They are all in the namespace small_vector_policy.

  • NoHeap - Avoid the heap entirely. (Throws std::length_error if you would've spilled out of the in-place allocation.)

  • <Any integral type> - customizes the amount of space we spend on tracking the size of the vector.

A couple more examples:

    // With space for 32 in situ unique pointers, and only using a
// 4-byte size_type.
small_vector<std::unique_ptr<int>, , uint32_t> v; // A inline vector of up to 256 ints which will not use the heap.
small_vector<int, , NoHeap> v; // Same as the above, but making the size_type smaller too.
small_vector<int, , NoHeap, uint16_t> v;

small_vector的更多相关文章

  1. C++ folly库解读(二) small_vector —— 小数据集下的std::vector替代方案

    介绍 使用场景 为什么不是std::array 其他用法 其他类似库 Benchmark 代码关注点 主要类 small_vector small_vector_base 数据结构 InlineSto ...

  2. PackedSyncPtr

    folly/PackedSyncPtr.h A highly specialized data structure consisting of a pointer, a 1-bit spin lock ...

  3. folly学习心得(转)

    原文地址:  https://www.cnblogs.com/Leo_wl/archive/2012/06/27/2566346.html   阅读目录 学习代码库的一般步骤 folly库的学习心得 ...

  4. Folly: Facebook Open-source Library Readme.md 和 Overview.md(感觉包含的东西并不多,还是Boost更有用)

    folly/ For a high level overview see the README Components Below is a list of (some) Folly component ...

随机推荐

  1. node csrf 防御 待续

    csrf 防御 token 与 ajax 主要是在cookie添加随机数, 因为攻击者 无法访问第三方网站的 cookie,  加上httponly, 即使是xss也无法访问了 也可以在页面上嵌入一个 ...

  2. 关于$_SERVER['PHP_AUTH_USER']

    http://www.cnblogs.com/thinksasa/p/3421379.html PHP 的 HTTP 认证机制仅在 PHP 以 Apache 模块方式运行时才有效,因此该功能不适用于 ...

  3. 《Effective Java》读书笔记(二)之对于所有对象都通用的方法

    第八条 在改写equals的时候请遵守通用约定 一般以下几种情况,不适宜覆盖equals方法 1.类的每个实例本质上都是唯一的,对于代表活动实体而不是值的类确实如此,例如Thread. 2.不关心类是 ...

  4. OpenGL实现相机视频NV21格式转RGB格式

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  5. python爬虫入门(3)-环境搭建

    下载集成包 链接:http://pan.baidu.com/s/1pKD2zBP 密码:f75b 这里采用python2.7.9   安装步骤:1.安装python2.7(默认安装即可) 2.打开“运 ...

  6. boost::function和boost:bind取代虚函数

    以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...

  7. erl_0016 《硝烟中的erlang》 读书笔记003 “error_logger 爆炸”

    error_logger 爆炸 具有讽刺意味的是,负责错误日志的进程竟然是最为脆弱的之一.在Erlang的缺省安装中,error_logger39负责记录日志(写入磁盘或者发送到网络上),它的速度要比 ...

  8. const in C/C++

    1.const 对象必须初始化,因为一旦创建后值不能改变. Eg: const int i = GetSize(); //正确:运行时初始化 const int j = 42;   //正确:编译时初 ...

  9. C#/.NET 中推荐的 Dispose 模式的实现

    如果你觉得你的类需要实现 IDisposable 接口,还是需要注意一些坑的.不过前人准备了 Dispose 模式 供我们参考,最大程度避免这样的坑. C#程序中的 Dispose 方法,一旦被调用了 ...

  10. Core Animation1-简介

    一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...