leveldb Arena
背景
我们都知道,对于一个高性能的服务器端程序来说,内存的使用非常重要。C++提供了new/delete来管理内存的申请和释放,但是对于小对象来说,直接使用new/delete代价比较大,要付出额外的空间和时间,性价比不高。
另外,我们也要避免多次的申请和释放引起的内存碎片。一旦碎片到达一定程度,即使剩余内存总量够用,但由于缺乏足够的连续空闲空间,导致内存不够用的假象。
c++ STL为了避免内存碎片,实现一个复杂的内存池,leveldb中则没有那么复杂,只是实现了一个"一次性"内存池Arena。
在leveldb里面,并不是所有的地方都使用了这个内存池,主要是memtable使用,主要是用于临时存放用户的更新数据,由于更新的数据可能很小,所以这里使用内存池就很合适。
原理
为了避免小对象的频繁分配,需要减少对new的调用,最简单的做法就是申请大块的内存,多次分给客户。
leveldb用一个vector<char *>来保存所有的内存分配记录,默认每次申请4k的内存,记录下剩余指针和剩余内存字节数,每当有新的申请,如果当前剩余的字节能满足需要,则直接返回给用户,如果不能,对于超过1k的请求,直接new返回,小于1K的请求,则申请一个新的4k块,从中分配一部分给用户。
但是这样的一个问题就是当前块剩余的部分就浪费了,改进的方法,针对每个block都记录剩余字节,这样就需要遍历来查找合适的block,要付出一些性能的代价。google的做法是浪费就浪费吧:-)
至于释放,需要释放整个内存池来释放所占内存,这个和leveldb的需求有关,memtable不需要释放单次内存,flush到硬盘后整个memtable销毁。
Arena.h
//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]
- // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file. See the AUTHORS file for names of contributors.
- #ifndef STORAGE_LEVELDB_UTIL_ARENA_H_
- #define STORAGE_LEVELDB_UTIL_ARENA_H_
- #include <cstddef>
- #include <vector>
- #include <assert.h>
- #include <stdint.h>
- namespace leveldb
- {
- /*//z
- 思路:
- 先在上一次分配的内存块中查找是否剩余空间能否容纳当前申请的空间;如果足以容纳,直接使用剩余空间
- 否则视其大小重新分配一块空间:如果大于1024,直接分配一块新空间,大小与申请空间大小同
- 如果小于1024,说明上一块空间剩余空间小于1024,那么重新分配一块4096大小的空间,使用该空间来存放待申请的空间
- */
- class Arena
- {
- public:
- Arena();
- ~Arena();
- // Return a pointer to a newly allocated memory block of "bytes" bytes.
- char* Allocate(size_t bytes);
- // Allocate memory with the normal alignment guarantees provided by malloc
- char* AllocateAligned(size_t bytes);
- // Returns an estimate of the total memory usage of data allocated
- // by the arena (including space allocated but not yet used for user
- // allocations).
- size_t MemoryUsage() const
- {
- return blocks_memory_ + blocks_.capacity() * sizeof(char*);
- }
- private:
- char* AllocateFallback(size_t bytes);
- char* AllocateNewBlock(size_t block_bytes);
- // Allocation state
- char* alloc_ptr_;
- size_t alloc_bytes_remaining_;
- // Array of new[] allocated memory blocks
- std::vector<char*> blocks_;
- // Bytes of memory in blocks allocated so far
- size_t blocks_memory_;
- // No copying allowed
- Arena(const Arena&);
- void operator=(const Arena&);
- };
- inline char* Arena::Allocate(size_t bytes)
- {
- // The semantics of what to return are a bit messy if we allow
- // 0-byte allocations, so we disallow them here (we don't need
- // them for our internal use).
- assert(bytes > 0);
- //z 在一块直接分配好的内存上移动一下指针即可;事实上可能会存在很多的浪费。
- if (bytes <= alloc_bytes_remaining_)
- {
- char* result = alloc_ptr_;
- alloc_ptr_ += bytes;
- alloc_bytes_remaining_ -= bytes;
- return result;
- }
- //z 如果剩余控件不足与容纳,那么根据bytes大小决定是否重新分配一块标准大小的内存(4096),或者要是bytes大于1024,直接
- //z 分配其大小的内存,原标准块内存仍旧有用。
- //z 如果小于 1024 ,说明原标准块剩余内存不足以容纳1024,新分配一块标准块内存,用此来为bytes分配对应内存。
- return AllocateFallback(bytes);
- }
- }
- #endif // STORAGE_LEVELDB_UTIL_ARENA_H_
//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]
arena.cc
- // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file. See the AUTHORS file for names of contributors.
- #include "util/arena.h"
- #include <assert.h>
- namespace leveldb {
- //z 常量变量名k开头
- static const int kBlockSize = 4096;
- //z 初始化
- Arena::Arena() {
- blocks_memory_ = 0;
- alloc_ptr_ = NULL; // First allocation will allocate a block
- alloc_bytes_remaining_ = 0;
- }
- Arena::~Arena() {
- //z 删除申请的内存
- for (size_t i = 0; i < blocks_.size(); i++) {
- delete[] blocks_[i];
- }
- }
- char* Arena::AllocateFallback(size_t bytes) {
- //z 如果申请的bytes > 1024
- if (bytes > kBlockSize / 4) {
- // Object is more than a quarter of our block size. Allocate it separately
- // to avoid wasting too much space in leftover bytes.
- char* result = AllocateNewBlock(bytes);
- return result;
- }
- // We waste the remaining space in the current block.
- //z 不大于1024时,分配一块标准大小的内存
- alloc_ptr_ = AllocateNewBlock(kBlockSize);
- alloc_bytes_remaining_ = kBlockSize;
- //z 指定返回的位置
- char* result = alloc_ptr_;
- //z 移动指针位置至空闲内存开始的地方
- alloc_ptr_ += bytes;
- //z 记录还剩下多少内存
- alloc_bytes_remaining_ -= bytes;
- return result;
- }
- char* Arena::AllocateAligned(size_t bytes) {
- //z 这个值是固定的,不用每次都求一次?但是代价非常小,求一次也无所为?
- const int align = sizeof(void*); // We'll align to pointer size
- assert((align & (align-1)) == 0); // Pointer size should be a power of 2
- //z 求的其余数
- size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
- size_t slop = (current_mod == 0 ? 0 : align - current_mod);
- size_t needed = bytes + slop;
- char* result;
- //z 如果剩余的空间足以容纳所需要的内存
- if (needed <= alloc_bytes_remaining_) {
- //z 对齐返回地址
- result = alloc_ptr_ + slop;
- alloc_ptr_ += needed;
- alloc_bytes_remaining_ -= needed;
- } else {
- // AllocateFallback always returned aligned memory
- //z 否则直接分配一块新的内存
- //z 在这种情况下这块内存可能很小
- result = AllocateFallback(bytes);
- }
- //z 确保返回地址是对齐的
- assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
- return result;
- }
- //z 在不小于一个page的时候,直接采用这种方式
- char* Arena::AllocateNewBlock(size_t block_bytes) {
- char* result = new char[block_bytes];
- blocks_memory_ += block_bytes;
- blocks_.push_back(result);
- return result;
- }
- }
//z 2014-06-05 10:48:50 L.209'47470 BG57IV3 T1840949363.K.F1370514324[T6,L108,R4,V118]
leveldb Arena的更多相关文章
- leveldb Arena源码分析
前言 对于一个高性能的服务器程序来说,内存的使用非常重要.C++提供new/delete来管理内存的申请和释放,但是对于小对象来说,直接使用new/delete代价比较大,要付出额外的空间和时间,性价 ...
- LevelDB(v1.3) 源码阅读之 Arena(内存管理器)
LevelDB(v1.3) 源码阅读系列使用 LevelDB v1.3 版本的代码,可以通过如下方式下载并切换到 v1.3 版本的代码: $ git clone https://github.com/ ...
- leveldb分析——Arena内存管理
leveldb中实现了一个简单的内存管理工具Arena,其基本思想为:先预先向系统申请一块内存,此后需要申请内存时,直接到预先分配的内存中申请. 那么这样做的目的是什么呢? (1)避免了频率地进行ma ...
- LevelDB源码分析之:arena内存管理
一.原理 arena是LevelDB内部实现的内存池. 我们知道,对于一个高性能的服务器端程序来说,内存的使用非常重要.C++提供了new/delete来管理内存的申请和释放,但是对于小对象来说,直接 ...
- leveldb源码分析之内存池Arena
转自:http://luodw.cc/2015/10/15/leveldb-04/ 这篇博客主要讲解下leveldb内存池,内存池很多地方都有用到,像linux内核也有个内存池.内存池的存在主要就是减 ...
- LevelDB 源码解析之 Arena
GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...
- LevelDB学习笔记 (3): 长文解析memtable、跳表和内存池Arena
LevelDB学习笔记 (3): 长文解析memtable.跳表和内存池Arena 1. MemTable的基本信息 我们前面说过leveldb的所有数据都会先写入memtable中,在leveldb ...
- LevelDB源码剖析
LevelDB的公共部件并不复杂,但为了更好的理解其各个核心模块的实现,此处挑几个关键的部件先行备忘. Arena(内存领地) Arena类用于内存管理,其存在的价值在于: 提高程序性能,减少Heap ...
- 【转】Leveldb源码分析——1
先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...
随机推荐
- JAVA笔记3-this关键字
1. 2.例题
- CodeForces 557C Arthur and Table STL的使用
题意:一个桌子有n条腿,每条腿有一定的长度l,和砍下的花费w,现在规定,桌子稳的条件是长度最长的腿(可多个)的数量大于长度小于它的桌子腿数量,且不存在比他还长的桌子腿,求让桌子腿稳定的最小的花费 #i ...
- FJOI2017前做题记录
FJOI2017前做题记录 2017-04-15 [ZJOI2017] 树状数组 问题转化后,变成区间随机将一个数异或一,询问两个位置的值相等的概率.(注意特判询问有一个区间的左端点为1的情况,因为题 ...
- make文件基础用法
参照:https://www.jianshu.com/p/0b2a7cb9a469 创建工作目录,包含一下文件 main.c person.c b.h c.h /*** c.h ***/ //this ...
- Java indexOf() 方法
indexOf() 方法有以下四种形式: public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1. public ...
- 博主的OI流水账
2017.8.4 入坑OI 2017.11.11 参加了NOIP2017提高组,0+85+0+30+50+0=165,荣获省二(具体分数其实记不清了反正差不多吧) 2018.2 学会树状数组,线段树 ...
- 【Python】学习笔记一:Hello world
前言 在我看来,无论我们学习什么语言第一个学习的估计都是Hello world,那么接下来就从Hello world说起! 编写代码 我在本机上已经安装了pycharm,所以我所编辑的代码都是在pyc ...
- 微信小程序需求IIS服务器配置https关于SSL,TLS的综合解决方案
SpringBoot配置SSL同时支持http和https访问 传输层安全性协议(英语:Transport Layer Security,缩写作 TLS),及其前身安全套接层(Secure Socke ...
- C++入门经典-例4.4-循环嵌套之求n的阶乘
1:代码如下: // 4.4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- Python学习笔记:外部数据的输入、存储等操作
查看current工作路径: >>> import os >>> os.getcwd() 'D:\\python' 更改工作路径: >>> os. ...