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. Spring ApplicationListener使用方法及二次调用问题解决

    使用场景 在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载.初始化缓存.特定任务的注册等等.这个时候我们就可以使用Spring提供的ApplicationListener来 ...

  2. c++ 列表删除元素(erase)

    #include <list> #include <iostream> #include <iterator> using namespace std; int m ...

  3. PHP如何安装扩展

    PHP如何安装扩展 一.总结 一句话总结:兩步: dll php.ini a.下载好扩展的dll,放入指定文件夹下 b.在php.ini配置文件中声明插件 1.什么是php扩展? php核心 不支持 ...

  4. 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程

    Ruby XML, XSLT 和 XPath 教程 什么是 XML ? XML 指可扩展标记语言(eXtensible Markup Language). 可扩展标记语言,标准通用标记语言的子集,一种 ...

  5. JS之Iterations

    for in.for of.for each in 1.for in:用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作),for ... in 循环中的代码每执行一次,就会对数组的元素或者 ...

  6. itembase协同过滤的详细介绍

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:https://www.cnblogs.com/by-dream/p/9016289.html 前言 通常我们在网购的时候会遇到这 ...

  7. Jenkins插件开发(一)--环境搭建

    最近写了一个jenkins插件,功能比较简单,时间主要是花在对jenkins插件框架和Maven的熟悉上.jenkins插件虽然以前也接触过一点,不过现在都忘得差不多了,这个笔记权当知识点记录,顺带介 ...

  8. 【转】Javascript中的this

    作者: 阮一峰 日期: 2010年4月30日 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ ...

  9. CS231n课程笔记翻译7:神经网络笔记 part2

    译者注:本文智能单元首发,译自斯坦福CS231n课程笔记Neural Nets notes 2,课程教师Andrej Karpathy授权翻译.本篇教程由杜客翻译完成,堃堃进行校对修改.译文含公式和代 ...

  10. Winform开发常用控件之DataGridView的简单数据绑定——自动绑定

    DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据 ...