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. vue.js 源代码学习笔记 ----- core scedule.js

    /* @flow */ import type Watcher from './watcher' import config from '../config' import { callHook } ...

  2. 【PL/SQL编程】变量和常量

    1. 变量格式 <变量名><数据类型>[(长度):=<初始值>]; v_countryname varchar2(50):='中国'; 2. 常量格式 <常量 ...

  3. 【Keras学习】资源

    Keras项目github源码(python):keras-team/keras: Deep Learning for humans 里面的docs包含说明文档 中文文档:Keras中文文档 预训练模 ...

  4. Git详解之八 Git与其他系统

    以下内容转载自:http://www.open-open.com/lib/view/open1328070454218.html Git 与其他系统 世界不是完美的.大多数时候,将所有接触到的项目全部 ...

  5. 【javascript】base.js

    作为一个好的脚手架使用 /* Base.js, version 1.1a Copyright 2006-2010, Dean Edwards License: http://www.opensourc ...

  6. [转]blocks编程

    原文地址:http://geeklu.com/2012/01/block/ 介绍 声明创建和调用 Block和变量 Block实际应用 1.介绍 Block是一个C Level的语法以及运行时的一个特 ...

  7. BZOJ2555 SubString【后缀自动机+LCT】

    Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...

  8. 接口测试框架——第二篇-python读取excel文件内容

    今天完善excel_module.py文件,上代码: # coding: utf-8 import xlrd class ReadExcel(): def __init__(self, file_na ...

  9. 基于C#的UDP协议的异步实现

    一.摘要 总结UDP传输协议的异步实现. 二.实验平台 visual studio 2010 三.实验实例 服务器端代码: using System; using System.Collections ...

  10. 在Centos中yum安装和卸载软件的使用方法(转)

    在Centos中yum安装和卸载软件的使用方法 安装方法 安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软 ...