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 ...
随机推荐
- redis的使用及方法
一.redis (1).redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset ...
- RabbitMQ入门_03_推拉模式
我们知道,消费者有两种方式从消息中间件获取消息: 推模式:消息中间件主动将消息推送给消费者 拉模式:消费者主动从消息中间件拉取消息 推模式将消息提前推送给消费者,消费者必须设置一个缓冲区缓存这些消息. ...
- 雷林鹏分享:Ruby 数据库访问 - DBI 教程
Ruby 数据库访问 - DBI 教程 本章节将向您讲解如何使用 Ruby 访问数据库.Ruby DBI 模块为 Ruby 脚本提供了类似于 Perl DBI 模块的独立于数据库的接口. DBI 即 ...
- PythonInstaller编译EXE方法+编译过程出错方案大全
https://www.cnblogs.com/gopythoner/p/6337543.htmlhttps://www.zhihu.com/question/22963200https://blog ...
- C#中json字符串的序列化和反序列化
改文章转自:https://www.cnblogs.com/shang201215019/p/7907655.html 什么是 Json ? Json[javascript对象表示方法] ...
- Filter实现session超时自动跳转到login页,超过试用期不许登录
新建一个过滤器 package com.autumn.filter; import com.autumn.pojo.Users; import javax.servlet.*; import java ...
- 在EORow或者VORow中对数据进行重复性校验
需求:在设置付款条件时不允许账期+付款方式重复. 由于本次需求仅需要对VO缓存中的数据进行重复性校验,所以仅需进行缓存遍历即可,不需要校验数据库. 方式1,在EORow的进行数据校验. public ...
- JS之Iterations
for in.for of.for each in 1.for in:用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作),for ... in 循环中的代码每执行一次,就会对数组的元素或者 ...
- ES选主策略
ES版本5.6.3 1.整个流程的开始,实在node启动后触发的,Node.java中start()方法,通过调用ZenDiscovery.java中的doStart()方法,之后会调用startIn ...
- 06-jenkins的账号相关的问题
飞测说:最近几天,在团队分享jenkins后,大家都十分感兴趣,各自下载安装和练习,然而jenkins2.3安装默认有权限设置,这块好多人遇到了问题,现在统一就账号登录的问题一起看看,踩过的坑,希望对 ...