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. codeforces 356 div2 C.Bear and Prime 100 数学

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. redis优缺点

    redis主要是一个内存数据库.很多时候是被作为缓存来使用.redis的优势主要体现在和其他缓存方案进行比较,redis的不足主要是和其他数据库进行比较时体现的. 优点: 读写性能优异 支持数据持久化 ...

  3. 30道Spring面试题和答案

    Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...

  4. Spring中的@Transactional

    spring中的@Transactional基于动态代理的机制,提供了一种透明的事务管理机制,方便快捷解决在开发中碰到的问题. 一般使用是通过如下代码对方法或接口或类注释: @Transactiona ...

  5. 微信授权登录,关于调不起授权页面,无法响应回调方法,获取不到code 详解

    前期准备工作:申请AppId,下载资源包jar.文档等. 微信授权登录步骤: 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据c ...

  6. git 常用命令--抓取分支-为自己记录(二)

    二:抓取分支: 多人协作时,大家都会往master分支上推送各自的修改.现在我们可以模拟另外一个同事,可以在另一台电脑上(注意要把SSH key添加到github上)或者同一台电脑上另外一个目录克隆, ...

  7. python切片取值和下标取值时,超出范围怎么办?

    可迭代对象下标取值超出索引范围,会报错:IndexError 可迭代切片取值超出索引范围,不报错,而是返回对应的空值. a=[1,2,3,4] a[99] Traceback (most recent ...

  8. iOS KVC 和 KVO 区别简单总结

    KVC: key value coding,键值编码.是一种通过使用属性的名称(key)来间接访问对象属性的方法.这个方法可以不用通过 setter/getter 方法来访问对象的属性.该方法使用的实 ...

  9. vim 编辑 windows下的文本时出现乱码, 修改配置后 已解决

    最近用VIM 查看一个 WINDOWS下的文本,打开以后发现出现乱码,具体如上图. 最后在网上找到了一个解决方法: 原文地址: https://www.zhihu.com/question/22363 ...

  10. 20155319 2016-2017-2 《Java程序设计》第六周学习总结

    20155319 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 ==第十章 输入/输出== 10.1 InputStream与OutputStream 10 ...