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的更多相关文章

  1. AtomicHashMap

    folly/AtomicHashmap.h folly/AtomicHashmap.h introduces a synchronized UnorderedAssociativeContainer ...

  2. folly学习心得(转)

    原文地址:  https://www.cnblogs.com/Leo_wl/archive/2012/06/27/2566346.html   阅读目录 学习代码库的一般步骤 folly库的学习心得 ...

  3. 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. CentOS环境Docker安装教程(官方推荐的docker三种方式安装)

    CentOS环境Docker安装教程(官方推荐的docker三种方式安装) 一.使用yum方式安装 1.安装依赖包 $ sudo yum install -y yum-utils device-map ...

  2. Mac 下安装Java

    下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 打开下载的文件,出现如下 ...

  3. Android Studio 中删除项目和项目找回------ Project Structure的使用

    删除项目 点击File——Project Structure 在Project Structure页面,选中要删除的项目,点击上面的减号图标. 我把两个一起删除了,再次打开时,啥也没有 如果想要把不要 ...

  4. K-Means算法的收敛性和如何快速收敛超大的KMeans?

    不多说,直接上干货! 面试很容易被问的:K-Means算法的收敛性. 在网上查阅了很多资料,并没有看到很清晰的解释,所以希望可以从K-Means与EM算法的关系,以及EM算法本身的收敛性证明中找到蛛丝 ...

  5. bzoj2330: [SCOI2011]糖果 差分约束系统

    幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖果的时候 ...

  6. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  7. fcntl 改变描述符属性

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  8. Python3 字典Dict(十三)

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 字典是另一种可变容器模型,且可存储任意类 ...

  9. java基础---->Zip压缩的使用

    java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...

  10. New Concept English Two 20 52

    $课文50  乘车兜风 510. I love travelling in the country, but I don't like losing my way. 我喜欢在乡间旅行,但却不愿意迷路. ...