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 letsmalloctrack 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_errorif 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 ...
随机推荐
- 添加operations模块
添加operations模块,models内容为: from django.db import models from datetime import datetime from users.mode ...
- 42.zip
最近看linux的解压缩,无意间了解到了一个和压缩率相关的小故事——42.zip 一般我们使用压缩工具的时候,都会用到无损压缩技术,对于无损压缩,算法非常重要,不同的算法实现 的压缩率和速度有很大差别 ...
- python编程(最简单的rpc代码)
[ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 采用twisted可以编写最简单的rpc代码. server端代码如下, from twiste ...
- EasyDarwin如何支持点播和RTMP/HLS直播?EasyDSS!
2017年很长很长一段时间没有更新EasyDarwin开源项目了,虽然心里有很多EasyDarwin功能扩展的计划:比如同步录像.同步RTMP/HLS直播输出.拉模式转发优化.Onvif接入.GB28 ...
- swift 3 发送 HTTP 请求函数
private func HttpPost(requestURL:String, postString:String) -> [String : AnyObject] { return Http ...
- 前端构建工具-fis3使用入门
FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自动化工具.开发规范.代码部署等问题. 官网地址是: https:/ ...
- 评价指标的计算:accuracy、precision、recall、F1-score等
记正样本为P,负样本为N,下表比较完整地总结了准确率accuracy.精度precision.召回率recall.F1-score等评价指标的计算方式: (右键点击在新页面打开,可查看清晰图像) 简单 ...
- kd树的原理
kd树就是一种对k维空间中的实例点进行存储以便对其进行快速检索的树形数据结构,可以运用在k近邻法中,实现快速k近邻搜索.构造kd树相当于不断地用垂直于坐标轴的超平面将k维空间切分. 假设数据 ...
- 编程之美Ex1——求二进制中1的个数
又被阿里机考虐了一次,决定改变策略开始刷题T^T 一个字节(8bit)的无符号整型,求其二进制中的“1”的个数,算法执行效率尽可能高. 最先想到的移位操作,末尾位&00000001,然后右移, ...
- HihoCoder1050 树中的最长路 树形DP第三题(找不到对象)
题意:求出的树中距离最远的两个结点之间相隔的距离. 水题一道,以前只会用路的直径来解. 代码如下: #include<cstdio> #include<cstdlib> #in ...