levelDB源码笔记(4)- SkipList
来源:互联网 发布:阿里云认证 有用吗 编辑:程序博客网 时间:2024/06/05 22:54
skipList是LevelDB的memtable实际采用的数据结构.其原理可以自行google
SkipList应用概率保持平衡,可以用来替代平衡树,优点是实现简单。这里每个链表从小到大排列,height=0是所有节点的链表,height越高,链表节点越稀少。
几个tips:
i) 使用内存池和placement new操作内存:
template<typename Key, class Comparator>typename SkipList<Key,Comparator>::Node*SkipList<Key,Comparator>::NewNode(const Key& key, int height) { char* mem = arena_->AllocateAligned( sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1)); return new (mem) Node(key);}
ii)memory barrier。
在SkipList的实现中,内部并没有使用到锁,只是用到了AtomicPointer。AtomicPointer可以保存一个void*,利用了memory barrier来实现同步。(似乎说mutex已经带了mb的操作,但是对单个int或者位的操作,不必用到mutex,用mb可以更快?)
inline void MemoryBarrier() { // See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on // this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering. __asm__ __volatile__("" : : : "memory");}Acquire_Load(){void* result = rep_; MemoryBarrier();return result;}Release_Store(void* v){MemoryBarrier();rep_ = v;}
An operation has acquire semantics if other processors will always see its effect before any subsequent operation's effect. An operation has release semantics if other processors will see every preceding operation's effect before the effect of the operation itself.
acquire保证其他线程先看到执行当前命令,而后执行后续指令;release保证其他线程先看到其他前序指令执行,而后执行当前指令
在强内存模型下,(X86-64,AMD64的内存模型都是强内存模型)volatile关键字声明的变量已经具备load acquire和store release的功能了,但在其他如安腾等是弱内存模型,需要另外的实现
参考:http://duanple.blog.163.com/blog/static/70971767201242491334104/
- levelDB源码笔记(4)- SkipList
- levelDB源码分析-Skiplist
- LevelDB源码之SkipList原理LevelDB源码之SkipList原理
- LevelDB源码之SkipList原理
- LevelDB源码之SkipList原理
- LevelDB源码之SkipList原理
- LevelDB源码剖析之SkipList
- LevelDB源码剖析之SkipList
- leveldb源码解析7——跳表(SkipList)
- LevelDB源码分析之五:skiplist(1)
- LevelDB源码分析之六:skiplist(2)
- leveldb源代码分析4:SkipList
- leveldb 源码分析 —— SkipList跳表
- Leveldb --- SkipList
- LevelDb skiplist
- Leveldb源码解析第五篇【memtable之skiplist】
- levelDB源码笔记(1)-TEST
- levelDB源码笔记(2)-Arena
- 黑马程序员 对图片文件的初步加密
- linux服务配置之Iptables
- XTUOJ 1142 Collatz Conjecture(数论)
- popToViewController用法
- scala
- levelDB源码笔记(4)- SkipList
- zoj 3597 Hit the Target! (线段树)
- 杂记
- 题集1
- 【足迹】学习C++ primer
- 通信协议中三次握手的故事<转http://blog.sina.com.cn/heinhe >
- 正则表达式
- -------------别人解决的, rtmp中音频和视频数据不对称导致的卡顿的情况-----------------
- 二维坐标中一个移动矩形块中的最大和值