small_vector
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 letsmalloc
track our allocation capacity. (Note that this slows down the insertion/reallocation code paths significantly; if you need those to be fast you should usefbvector
.)
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. (Throwsstd::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的更多相关文章
- C++ folly库解读(二) small_vector —— 小数据集下的std::vector替代方案
介绍 使用场景 为什么不是std::array 其他用法 其他类似库 Benchmark 代码关注点 主要类 small_vector small_vector_base 数据结构 InlineSto ...
- PackedSyncPtr
folly/PackedSyncPtr.h A highly specialized data structure consisting of a pointer, a 1-bit spin lock ...
- folly学习心得(转)
原文地址: https://www.cnblogs.com/Leo_wl/archive/2012/06/27/2566346.html 阅读目录 学习代码库的一般步骤 folly库的学习心得 ...
- 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 ...
随机推荐
- c# DataTable 数据集处理DataTableHandler
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Linux:expand命令详解
expand 用于将文件的制表符[TAB]转换为空格,将结果显示到标准输出设备 语法 expand(选项)(file) 选项 -t<数字>:指定制表符所代表的空白字符的个数,而不使用默认的 ...
- 在头文件中声明class 类 与 include类所在的头文件区别---理解
在头文件中声明class 类 与 include类所在的头文件的理解: 在头文件中,声明类 它告诉编译器:存在这样的类.而实际的类则可以位于同一个编译单元中,也可以放在其他编译单元中.没有这个类原型, ...
- c++多线程在异常环境下的等待
c++11开始支持多线程编程,相关的类和函数封装在标准库头文件<thread>中,而c++多线程编程很重要的一点就是当用户创建一个std::thread对象,关联了可调用对象后,需要在该t ...
- HAWQ + MADlib 玩转数据挖掘之(五)——奇异值分解实现推荐算法
一.奇异值分解简介 奇异值分解简称SVD(singular value decomposition),可以理解为:将一个比较复杂的矩阵用更小更简单的三个子矩阵的相乘来表示,这三个小矩阵描述了大矩阵重要 ...
- HAWQ中的行列转置
行列转置是ETL或报表系统中的常见需求,HAWQ提供的内建函数和过程语言编程功能,使行列转置操作的实现变得更为简单. 一.行转列 1. 固定列数的行转列 原始数据如下: test=# select * ...
- SVN 安装配置详解,包含服务器和客户端,外带一个项目演示,提交,更改,下载历史版本,撤销
本次要介绍的是svn版本管理工具包含2个: 服务器端:visualsvn server 下载地址为:https://www.visualsvn.com/server/download/ 此处演示的 ...
- import 路径
例子: import sys sys.path.append("/home/wang/Downloads/caffe-master/python") import caffe
- 【angular之起步】安装
人生只有眼前的苟且. 所以为了远方,最近在策划一个大阴谋------做一个自己的网站,杂而全的. 各种胡思乱想了一周,先把页面写完了,没辙,就这个不用费太多脑子. 然后开始重头戏,就卡死了. angu ...
- HDU - 6172:Array Challenge (BM线性递推)
题意:给出,三个函数,h,b,a,然后T次询问,每次给出n,求sqrt(an); 思路:不会推,但是感觉a应该是线性的,这个时候我们就可以用BM线性递推,自己求出前几项,然后放到模板里,就可以求了. ...