PackedSyncPtr
folly/PackedSyncPtr.h
A highly specialized data structure consisting of a pointer, a 1-bit spin lock, and a 15-bit integral packed into sizeof(void*).
Typical application is for microsharding of many elements within containers. Because there is no memory overhead, an arbitrarily large number of locks can be used to minimize lock contention with no memory penalty. Additionally, excellent cache performance is obtained by storing the lock inline with the pointer (no additional cache miss or false sharing). Finally, because it uses a simple spinlock mechanism, the cost of aqcuiring an uncontended lock is minimal.
Usage
This is not a "smart" pointer: nothing automagical is going on here. Locking is up to the user. Resource deallocation is up to the user. Locks are never acquired or released outside explicit calls to lock() and unlock().
Change the value of the raw pointer with set(), but you must hold the lock when calling this function if multiple threads could be using it.
Here is an example of using a PackedSyncPtr to build a synchronized vector with no memory overhead - the spinlock and size are stored in the 16 unused bits of pointer, the rest of which points to the actual data. See folly/small_vector.h for a complete implementation of this concept.
template<typename T>
class SyncVec {
PackedSyncPtr<T> base; public:
SyncVec() { base.init(); } void push_back(const T& t) {
base.set(
static_cast<T*>(realloc(base.get(), (base.extra() + ) * sizeof(T))));
base[base.extra()] = t;
base.setExtra(base.extra() + );
} size_t size() const {
return base.extra();
} void lock() {
base.lock();
} void unlock() {
base.unlock();
} T* begin() const {
return base.get();
} T* end() const {
return base.get() + base.extra();
}
};
Implementation
This is using an x64-specific detail about the effective virtual address space. Long story short: the upper two bytes of all our pointers will be zero in reality---and if you have a couple billion such pointers in core, it makes pretty good sense to try to make use of that memory. The exact details can be perused here:
http://en.wikipedia.org/wiki/X86-64#Canonical_form_addresses
PackedSyncPtr的更多相关文章
- AtomicHashMap
folly/AtomicHashmap.h folly/AtomicHashmap.h introduces a synchronized UnorderedAssociativeContainer ...
- 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 ...
随机推荐
- shell 使用ping测试网络
能ping通返回1,不能返回0 ping -c 192.168.1.1 | grep '0 received' | wc -l
- FreeSouth的学习osg小贴士
http://www.osgchina.org/index.php?option=com_content&view=article&id=150&catid=91&It ...
- Mac OS 下安装 Vagrant
Vagrant是一款用来构建虚拟开发环境的工具,它其实算是一个跨平台的虚拟机管理工具 1 安装 1.1 安装Vagrant 下载好pkg文件后,下一步安装即可 1.2 安装Virtualbox Vag ...
- Java回顾之一些基础概念
类的初始化顺序 在Java中,类里面可能包含:静态变量,静态初始化块,成员变量,初始化块,构造函数.在类之间可能存在着继承关系,那么当我们实例化一个对象时,上述各部分的加载顺序是怎样的? 首先来看代码 ...
- 【Python】xlrd,NotImplementedError-formatting_info=True not yet implemented
前言 Python需要读取Excel(.xls..xlsx)时通常使用xlrd模块:如果要对其内容进行编辑的话稍稍有些麻烦,通常的做法是使用xlutils的copy模块对原文件进行复制,然后保存成新的 ...
- apache配置文件详解与优化
apache配置文件详解与优化 一.总结 一句话总结:结合apache配置文件中的英文说明和配置详解一起看 1.apache模块配置用的什么标签? IfModule 例如: <IfModule ...
- Java线程状态分析
Java线程的生命周期中,存在几种状态.在Thread类里有一个枚举类型State,定义了线程的几种状态,分别有: NEW: 线程创建之后,但是还没有启动(not yet started).这时候它的 ...
- @Query 注解实现查询(二十四)
为了节约时间使得各位看官看起来更加简单舒适,这一节把测试方法和测试代码放在一起. 测试方法: // ------------------------------------ 使用 @Query 注解 ...
- PHP:第五章——字符串编码函数
<?php header("Content-Type:text/html;charset=utf-8"); //1.base64_encode和base64_decode.6 ...
- vue.js利用vue.router创建前端路由
node.js方式: 利用node.js安装vue-router模块 cnpm install vue-router 安装完成后我们引入这个模板! 下载vue-router利用script引入方式: ...