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. mybatis分页查询

    方式1: select * from table order by id limit m, n; 该语句的意思为,查询m+n条记录,去掉前m条,返回后n条记录.无疑该查询能够实现分页功能,但是如果m的 ...

  2. 浅谈OSSemPost()和OSSemPend()

    http://blog.csdn.net/goodman_lqifei/article/details/53616174

  3. android 几个开源项目

    android的几个开源项目ormlite.volley.jsoup.vitamio ksoap2

  4. JSCover(查看代码覆盖率)

    1. [文档](http://tntim96.github.io/JSCover/manual/manual.xml)1. [下载](https://sourceforge.net/projects/ ...

  5. Jmter-Test Fragment、Include Controller和Module Controller

    Test Fragment--测试片段 The Test Fragment is used in conjunction with the Include Controller and Module ...

  6. Filter学习(二)Filter使用Decorator设计模式

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  7. 在MEF中实现延迟加载部件(转)

    在MEF的宿主中,当我们通过Import声明导入的对象时,组装(Compose)的时候会创建该对象.例如: interface ILogger    {        void Log(string  ...

  8. Thread中,join()方法

    Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行. public static void main(String[] args) throws Interrupted ...

  9. Requirejs简单介绍

    具体详情请进入官网查阅:http://requirejs.org 一.什么是Requirejs RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一. 二. ...

  10. angular-ui-bootstrap弹出框的使用(一)

    在开发项目时,我们经常性的会遇到弹出框的需求,例如登陆,注册,添加信息等等....面对这一需求,我们当然也可以使用自己的双手进行编写,如果你时间充足可以试试. 今天我们讲解一下如何在angular框架 ...