{"id":167,"date":"2017-07-17T23:45:28","date_gmt":"2017-07-17T15:45:28","guid":{"rendered":"http:\/\/www.myway5.com\/?p=167"},"modified":"2023-07-05T22:00:13","modified_gmt":"2023-07-05T14:00:13","slug":"leveldb-varint-arena","status":"publish","type":"post","link":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/","title":{"rendered":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0"},"content":{"rendered":"<h2>\u4e00\u3001Varint<\/h2>\n<p>Varint\u662f\u5728leveldb\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e00\u79cd\u53d8\u957f\u7684\u6574\u6570\u7c7b\u578b\uff0cVarint\u5176\u5b9e\u548cunicode\u7684\u5b9e\u73b0\u975e\u5e38\u76f8\u4f3c\uff0c\u5e76\u4e14\u662flittle-endian\u3002\u5982\u679c\u5f53\u524d\u5b57\u8282\u7684\u6700\u9ad8\u4f4d\u662f1\uff0c\u5219\u8868\u793a\u540e\u9762\u7684\u5b57\u8282\u4e5f\u5c5e\u4e8e\u8fd9\u4e2a\u6574\u6570\uff0c\u5982\u679c\u6700\u9ad8\u4f4d\u662f0\uff0c\u5219\u8868\u793a\u8be5\u6574\u6570\u7ed3\u675f\u4e86\u3002\u6240\u4ee5\uff0c\u4e00\u4e2a\u65e0\u7b26\u53f74\u4e2a\u5b57\u8282\u7684\u6574\u578b\u6570\uff0c\u4f7f\u7528Varint\u53ef\u80fd\u4ee51,2,3,4,5\u4e2a\u5b57\u8282\u6765\u8868\u793a\u3002<\/p>\n<p>\u6bd4\u5982130:<\/p>\n<pre><code>\u65e0\u7b26\u53f7\u6574\u578b\u8868\u793a\u8d77\u6765\u4e3a\uff1a00000000 00000000 00000000 10000010\n\u4f7f\u7528Varint\u8868\u793a\u4e3a\uff1a10000010 00000001             #\u8fd9\u91cc\u6ce8\u610fVarint\u662flittle-endian\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u727a\u7272\u4e86\u6bcf\u4e2a\u5b57\u8282\u7684\u6700\u9ad8\u4f4d\u4f5c\u4e3a\u6807\u5fd7\u4f4d,\u6240\u4ee5\u9047\u5230\u5927\u7684\u6570\u53ef\u80fd\u9700\u89815\u4e2a\u5b57\u8282\u3002<\/p>\n<pre><code>\u67d0\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\uff1a10000001 00000001 00000001 00000001\n\u4f7f\u7528Varint\u8868\u793a\uff1a10000001 10000010 10000100 10001000 00001000\n<\/code><\/pre>\n<p>\u4f46\u662f\u5e73\u5747\u4e0b\u6765\uff0cVarint\u662f\u7edd\u5bf9\u4f1a\u8282\u7701\u5927\u91cf\u7684\u5b58\u50a8\u7a7a\u95f4\u7684\u3002<\/p>\n<p>\u90a3\u4e48leveldb\u4e2dVarint\u662f\u5982\u4f55\u7f16\u7801\u548c\u89e3\u7801\u7684\u5462\uff1f<\/p>\n<pre><code class=\"line-numbers\">char* EncodeVarint32(char* dst, uint32_t v) {\n  \/\/ Operate on characters as unsigneds\n  unsigned char* ptr = reinterpret_cast&lt;unsigned char*&gt;(dst);\n  static const int B = 128; \/\/10000000\n  if (v &lt; (1&lt;&lt;7)) {  v &lt; 2^7\n    *(ptr++) = v;\n  } else if (v &lt; (1&lt;&lt;14)) { v &lt; 2^14\n    *(ptr++) = v | B;\n    *(ptr++) = v&gt;&gt;7;\n  } else if (v &lt; (1&lt;&lt;21)) { \/\/v &lt; 2^21\n    *(ptr++) = v | B;\n    *(ptr++) = (v&gt;&gt;7) | B;\n    *(ptr++) = v&gt;&gt;14;\n  } else if (v &lt; (1&lt;&lt;28)) {\n    *(ptr++) = v | B;\n    *(ptr++) = (v&gt;&gt;7) | B;\n    *(ptr++) = (v&gt;&gt;14) | B;\n    *(ptr++) = v&gt;&gt;21;\n  } else {\n    *(ptr++) = v | B;\n    *(ptr++) = (v&gt;&gt;7) | B;\n    *(ptr++) = (v&gt;&gt;14) | B;\n    *(ptr++) = (v&gt;&gt;21) | B;\n    *(ptr++) = v&gt;&gt;28;\n  }\n  return reinterpret_cast&lt;char*&gt;(ptr);\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e00\u6bb5\u5bf9uint32_t\u7684\u53d8\u91cf\u8fdb\u884cVarint32\u7f16\u7801\u7684\u4f8b\u5b50\uff0c\u7f16\u7801\u7684\u7ed3\u679c\u4fdd\u5b58\u5728dst\u91cc\uff0c\u8fd4\u56de\u7684ptr\u662fdst\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u7684\u6307\u9488\u3002\u4f7f\u7528if-else\u5bf91\u30012\u30013\u30014\u30015\u4e2a\u5b57\u8282\u7684\u60c5\u51b5\u5206\u522b\u505a\u4e86\u5904\u7406\u3002\u4e3b\u8981\u5c31\u662f\u79fb\u4f4d\u548c\u6216\u8fd0\u7b97\u7b26\u7684\u4f7f\u7528\u3002\u8ddf\u7740\u4ee3\u7801\u8d70\u4e00\u904d\u5c31\u80fd\u7406\u89e3\u3002<\/p>\n<pre><code class=\"line-numbers\">const char* GetVarint32PtrFallback(const char* p,\n                                   const char* limit,\n                                   uint32_t* value) {\n  uint32_t result = 0;\n  for (uint32_t shift = 0; shift &lt;= 28 &amp;&amp; p &lt; limit; shift += 7) {\n    uint32_t byte = *(reinterpret_cast&lt;const unsigned char*&gt;(p));\n    p++;\n    if (byte &amp; 128) {\n      \/\/ More bytes are present\n      result |= ((byte &amp; 127) &lt;&lt; shift);\n    } else {\n      result |= (byte &lt;&lt; shift);\n      *value = result;\n      return reinterpret_cast&lt;const char*&gt;(p);\n    }\n  }\n  return NULL;\n}\n<\/code><\/pre>\n<pre><code class=\"line-numbers\">inline const char* GetVarint32Ptr(const char* p,\n                                  const char* limit,\n                                  uint32_t* value) {\n  if (p &lt; limit) {\n    uint32_t result = *(reinterpret_cast&lt;const unsigned char*&gt;(p));\n    if ((result &amp; 128) == 0) {\n      \/\/\u5982\u679c\u4f4e8\u4f4d\u662f0xxxxxxx,little-endian,\u4e5f\u5c31\u662f\u5c0f\u4e8e128\n      *value = result;    \/\/\u76f4\u63a5\u8d4b\u503c\n      return p + 1;\n    }\n  }\n  return GetVarint32PtrFallback(p, limit, value);\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762GetVarint32Ptr\u662f\u89e3\u7801Varint32\uff0c\u5f53\u503c\u5927\u4e8e128\u65f6\uff0c\u5219\u8c03\u7528GetVarint32PtrFallback\u3002\u5728\u5219\u8c03\u7528GetVarint32PtrFallback\u4e2d\uff0c\u5229\u7528\u5faa\u73af\u4e00\u4e2a\u5b57\u8282\u4e00\u4e2a\u5b57\u8282\u7684\u53d6\u51fa\uff0c\u6700\u7ec8\u5c06value\u6307\u9488\u6307\u5411\u7ed3\u679cresult\u3002<\/p>\n<h2>\u4e8c\u3001Arena<\/h2>\n<p>Arena\u662fleveldb\u4e2d\u7ba1\u7406\u5185\u5b58\u5206\u914d\u7684\u7c7b\u3002\u6240\u6709\u7684\u5185\u5b58\u5206\u914d\u90fd\u901a\u8fc7Arena\u7533\u8bf7\uff0c\u53ef\u4ee5\u6839\u636e\u7533\u8bf7\u7684\u5185\u5b58\u5927\u5c0f\u4f7f\u7528\u4e0d\u540c\u7684\u5185\u5b58\u5206\u914d\u7b56\u7565\uff0c\u4e5f\u53ef\u4ee5\u907f\u514d\u8fc7\u591a\u7684\u5185\u5b58\u788e\u7247\u95ee\u9898\uff0c\u5e76\u4e14\u5728\u5185\u5b58\u91ca\u653e\u65f6\u7edf\u4e00\u4f7f\u7528Arena\u6765\u91ca\u653e\uff0c\u65b9\u4fbf\u7ba1\u7406\u3002<\/p>\n<p>Arena\u7c7b\u7684\u5b9e\u73b0\u5e76\u4e0d\u590d\u6742\u3002\u9996\u5148\u770b\u4e00\u4e0b\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"line-numbers\">  \/\/ Allocation state\n  char* alloc_ptr_;                 \/\/\u6307\u5411\u5f53\u524d\u5757\u4e2d\u5269\u4f59\u7684\u5185\u5b58\u8d77\u70b9\n  size_t alloc_bytes_remaining_;    \/\/\u5f53\u524d\u5757\u4e2d\u5269\u4f59\u7684\u5185\u5b58\n\n  \/\/ Array of new[] allocated memory blocks\n  std::vector&lt;char*&gt; blocks_;       \/\/\u7528\u6765\u4fdd\u5b58\u6240\u6709new\u51fa\u6765\u7684char\u6570\u7ec4\uff0c\u91ca\u653e\u5185\u5b58\u65f6\u4e5f\u4f1a\u4f7f\u7528\u5230\n\n  \/\/ Total memory usage of the arena.\n  port::AtomicPointer memory_usage_;    \/\/\u901a\u8fc7Arena\u7533\u8bf7\u7684\u5185\u5b58\n<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u662fpublic\u7684\u6210\u5458\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"line-numbers\">\/\/ Return a pointer to a newly allocated memory block of \"bytes\" bytes.\n  char* Allocate(size_t bytes);\n\n  \/\/ Allocate memory with the normal alignment guarantees provided by malloc\n  char* AllocateAligned(size_t bytes);\n\n  \/\/ Returns an estimate of the total memory usage of data allocated\n  \/\/ by the arena.\n  size_t MemoryUsage() const {\n    return reinterpret_cast&lt;uintptr_t&gt;(memory_usage_.NoBarrier_Load());\n  }\n<\/code><\/pre>\n<p>\u529f\u80fd\u4e0a\u5f88\u7b80\u5355\uff0cAllocate\u7528\u6765\u7533\u8bf7\u6307\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\uff0cAllocateAligned\u7528\u6765\u7533\u8bf7\u4fdd\u8bc1\u5185\u5b58\u5bf9\u9f50\u7684\u5185\u5b58\u7a7a\u95f4\uff0cMemoryUsage\u7528\u6765\u83b7\u53d6\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<blockquote><p>\n  \u6211\u5bf9\u5185\u5b58\u5bf9\u9f50\u7684\u7406\u89e3\uff1a\u73b0\u5728\u5047\u5b9a\u5185\u5b58\u5bf9\u9f50\u7684\u6700\u5c0f\u5355\u4f4d\u662f8\u4e2a\u5b57\u8282\uff0c\u90a3\u4e48\u5982\u679c\u4f60\u53ea\u7533\u8bf714\u4e2a\u5b57\u8282\uff0c\u5185\u5b58\u5bf9\u9f50\u4f1a\u591a\u7ed9\u4f60\u5206\u914d2\u4e2a\u5b57\u8282\u51d1\u621016\u4e2a\u5b57\u8282\u3002\u5185\u5b58\u5bf9\u9f50\u7684\u597d\u5904\u5c31\u662f\u8bbf\u95ee\u901f\u5ea6\u7684\u63d0\u5347\uff0c\u5728CPU\u4e00\u6b21\u8bfb\u53d68\u4e2a\u5b57\u8282\u7684\u60c5\u51b5\u4e0b\u3002\u5728\u6ca1\u6709\u5185\u5b58\u5bf9\u9f50\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u8981\u8bfb\u53d6\u7b2c6~12\u4e2a\u5b57\u8282\uff0c\u5c31\u9700\u8981\u8bfb\u53d6\u4e24\u6b21\uff0c\u7b2c\u4e00\u6b210~7\u4e2a\u5b57\u8282\uff0c\u901a\u8fc7\u79fb\u4f4d\u53d6\u51fa6~7,\u518d\u8bfb\u53d6\u7b2c8~15\u4e2a\u5b57\u8282\uff0c\u53d6\u51fa8~12\u3002\u8fd9\u6837\u5c31\u9700\u8981\u8bfb\u53d6\u4e24\u6b21\u3002\u800c\u5982\u679c\u5728\u5185\u5b58\u5bf9\u9f50\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u8bfb\u53d6\u76846~12\u4e2a\u5b57\u8282\u5e94\u8be5\u88ab\u653e\u57288~15\u4e2a\u5b57\u8282\u4e2d\u3002\u53ea\u9700\u8bfb\u53d6\u4e00\u6b21\u5373\u53ef\u3002\n<\/p><\/blockquote>\n<p>\u79c1\u6709\u7684\u6210\u5458\u51fd\u6570\u6709\uff1a<\/p>\n<pre><code class=\"line-numbers\">char* AllocateFallback(size_t bytes);\nchar* AllocateNewBlock(size_t block_bytes);\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u4e2a\u51fd\u6570\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u5185\u5b58\u5206\u914d\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5177\u4f53\u5206\u6790\u8fd95\u4e2a\u6210\u5458\u51fd\u6570\u3002<\/p>\n<p>1.Allocate(size_t bytes)<\/p>\n<pre><code class=\"line-numbers\">inline char* Arena::Allocate(size_t bytes) {\n  \/\/ The semantics of what to return are a bit messy if we allow\n  \/\/ 0-byte allocations, so we disallow them here (we don't need\n  \/\/ them for our internal use).\n  assert(bytes &gt; 0);\n  if (bytes &lt;= alloc_bytes_remaining_) {\n    char* result = alloc_ptr_;\n    alloc_ptr_ += bytes;\n    alloc_bytes_remaining_ -= bytes;\n    return result;\n  }\n  return AllocateFallback(bytes);\n}\n<\/code><\/pre>\n<p>Allocate\u662f\u4e00\u4e2a\u5185\u8054\u51fd\u6570\u3002\u5185\u8054\u51fd\u6570\u7684\u597d\u5904\u5728\u4e8e\u7f16\u8bd1\u65f6\u4f1a\u76f4\u63a5\u5728\u8c03\u7528\u5904\u5c55\u5f00\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u6709\u4e00\u5b9a\u63d0\u5347\uff0c\u4f46\u662f\u4e5f\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5927\u5c0f\u53d8\u5927\u3002<br \/>\n<code>assert(bytes &gt; 0);<\/code>\u662f\u4e00\u4e2a\u65ad\u8a00\u8bed\u53e5\uff0c\u65ad\u8a00\u8bed\u53e5\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u7531\u7f16\u8bd1\u5668\u53bb\u9664\uff0c\u4f46\u662f\u5728\u8c03\u8bd5\u7684\u65f6\u5019\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u53d1\u73b0\u95ee\u9898\u6240\u5728\u3002\u8fd9\u91cc\u7528\u6765\u4fdd\u8bc1\u6ca1\u6709\u51fa\u73b00\u4e2a\u5b57\u8282\u5206\u914d\u7684\u60c5\u51b5\u3002<br \/>\n\u540e\u9762\u7684\u5224\u65ad\u8bed\u53e5\u7528\u6765\u5224\u65ad\u5f53\u524d\u5757\u7684\u5269\u4f59\u7a7a\u95f4\u662f\u5426\u591f\u5206\u914d\uff0c\u5982\u679c\u591f\u5219\u5206\u914d\u3002\u5982\u679c\u4e0d\u591f\u5219\u4ea4\u7ed9AllocateFallback()\u5904\u7406\u3002\u8fd4\u56de\u7684\u662f\u5206\u914d\u7684\u7a7a\u95f4\u8d77\u70b9\u3002<\/p>\n<p>2.AllocateAligned(size_t bytes)<\/p>\n<pre><code class=\"line-numbers\">char* Arena::AllocateAligned(size_t bytes) {\n  const int align = (sizeof(void*) &gt; 8) ? sizeof(void*) : 8;\n  assert((align &amp; (align-1)) == 0);   \/\/ Pointer size should be a power of 2\n  size_t current_mod = reinterpret_cast&lt;uintptr_t&gt;(alloc_ptr_) &amp; (align-1);\n  size_t slop = (current_mod == 0 ? 0 : align - current_mod);\n  size_t needed = bytes + slop;\n  char* result;\n  if (needed &lt;= alloc_bytes_remaining_) {\n    result = alloc_ptr_ + slop;\n    alloc_ptr_ += needed;\n    alloc_bytes_remaining_ -= needed;\n  } else {\n    \/\/ AllocateFallback always returned aligned memory\n    result = AllocateFallback(bytes);\n  }\n  assert((reinterpret_cast&lt;uintptr_t&gt;(result) &amp; (align-1)) == 0);\n  return result;\n}\n<\/code><\/pre>\n<p>AllocateAligned(size_t bytes)\u7528\u6765\u505a\u5185\u5b58\u5bf9\u9f50\u7684\u5206\u914d\u3002<br \/>\n<code>const int align = (sizeof(void*) &gt; 8) ? sizeof(void*) : 8;<\/code>\u7528\u6765\u83b7\u53d6\u5206\u914d\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5982\u679c\u6307\u9488\u5927\u5c0f\u5927\u4e8e8\uff0c\u5219\u53d6\u6307\u9488\u5927\u5c0f\uff0c\u5426\u5219\u4f7f\u75288\u4f5c\u4e3a\u6700\u5c0f\u7684\u5bf9\u9f50\u5355\u4f4d\u3002<br \/>\n<code>assert((align &amp; (align-1)) == 0);<\/code>\u8fd9\u662f\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u5224\u65ad\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u53ea\u67092\u7684n\u6b21\u65b9\u624d\u80fd\u6ee1\u8db3\u3002<\/p>\n<p><code>size_t current_mod = reinterpret_cast&lt;uintptr_t&gt;(alloc_ptr_) &amp; (align-1);<\/code>current_mod\u662f\u4f59\u6570\uff0c\u6bd4\u5982\u5f53\u524d\u6307\u9488\u5730\u5740\u662f15\uff0calign\u662f8,\u90a3\u4e48\u4f59\u6570\u4e3a7,\u8fd9\u4e2a\u4f59\u6570\u5c31\u662falloc_prt_&amp;(align-1)\u7684\u503c\u3002<br \/>\n<code>size_t slop = (current_mod == 0 ? 0 : align - current_mod);<\/code>\u56e0\u4e3a\u5f53\u524d\u6307\u9488\u53ef\u80fd\u4e0d\u662falign\u7684\u6574\u6570\u500d,slop\u5373\u4ee3\u8868\u5f53\u524d\u6307\u9488\u9700\u8981\u504f\u79fb\u7684\u5927\u5c0f\uff0c\u6765\u4fdd\u8bc1\u662falign\u7684\u6574\u6570\u500d\u3002<br \/>\n<code>size_t needed = bytes + slop;<\/code> needed\u4ee3\u8868\u9700\u8981\u5206\u914d\u7684\u7a7a\u95f4\u3002<\/p>\n<p>\u4e4b\u540e\u7684\u5224\u65ad\u5219\u548cAllocate()\u51fd\u6570\u505a\u6cd5\u76f8\u540c\u4e86\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd4\u56de\u7684result\u6307\u9488\u662falign\u7684\u6574\u6570\u500d\uff0c\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\u4ee5\u6700\u5c11\u7684\u5185\u5b58\u8bfb\u53d6\u6b21\u6570\u6765\u83b7\u53d6\u60f3\u8981\u7684\u6570\u636e\u3002<\/p>\n<p>3.AllocateFallback(size_t bytes)<\/p>\n<pre><code class=\"line-numbers\">char* Arena::AllocateFallback(size_t bytes) {\n  if (bytes &gt; kBlockSize \/ 4) {\n    \/\/\u5982\u679c\u9700\u8981\u5206\u914d\u7684bytes\u5927\u4e8e\u5757\u5927\u5c0f\u76841\/4\uff0c\u5219\u5355\u72ec\u5728\u65b0\u7684\u5757\u4e2d\u5206\u914d\uff0c\u4ee5\u6b64\u907f\u514d\u6d6a\u8d39\u8fc7\u591a\u7684\u5269\u4e0b\u7684\u7a7a\u95f4\uff08\u8fd9\u6837\u80fd\u4fdd\u8bc1\u6d6a\u8d39\u7684\u7a7a\u95f4\u5c0f\u4e0ekBlockSize \/ 4)\n    char* result = AllocateNewBlock(bytes);\n    return result;\n  }\n\n  \/\/ \u91cd\u65b0\u5f00\u8f9f\u65b0\u7684\u5757\u7a7a\u95f4\u8fdb\u884c\u5206\u914d\uff0c\u4e0a\u4e00\u4e2a\u5757\u5269\u4e0b\u7684\u7a7a\u95f4\u90fd\u6d6a\u8d39\u4e86\u3002\n  alloc_ptr_ = AllocateNewBlock(kBlockSize);\n  alloc_bytes_remaining_ = kBlockSize;\n\n  char* result = alloc_ptr_;\n  alloc_ptr_ += bytes;\n  alloc_bytes_remaining_ -= bytes;\n  return result;\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4e24\u4e2a\u6210\u5458\u51fd\u6570\u5728\u5f53\u524d\u5757\u5206\u914d\u7a7a\u95f4\u4e0d\u8db3\u65f6\uff0c\u90fd\u5c06\u5206\u914d\u4efb\u52a1\u4ea4\u7ed9AllocateFallback\u5904\u7406\u3002<\/p>\n<p>4.AllocateNewBlock(size_t block_bytes)\u3002<\/p>\n<pre><code class=\"line-numbers\">char* Arena::AllocateNewBlock(size_t block_bytes) {\n  char* result = new char[block_bytes];\n  blocks_.push_back(result);\n  memory_usage_.NoBarrier_Store(\n      reinterpret_cast&lt;void*&gt;(MemoryUsage() + block_bytes + sizeof(char*)));\n  return result;\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528new char\u6765\u7533\u8bf7\u4e00\u4e2a\u65b0\u7684\u5757\uff0c\u5e76\u5c06\u5757\u7684\u8d77\u59cb\u6307\u9488\u5b58\u5165blocks_\u4e2d\u3002\u66f4\u65b0memory_usage\u7684\u5927\u5c0f\u3002<\/p>\n<p>5.MemoryUsage()<\/p>\n<p>&#8220;`<br \/>\n  size_t MemoryUsage() const {<br \/>\n    return reinterpret_cast&lt;uintptr_t&gt;(memory_usage_.NoBarrier_Load());<br \/>\n  }<br \/>\n &#8220;`<br \/>\n\u83b7\u53d6memory_usage_\u4e2d\u5b58\u50a8\u7684\u4f7f\u7528\u7a7a\u95f4\u6570\u636e\u3002<\/p>\n<h2>\u4e09\u3001Memeory Barrier\uff08\u5185\u5b58\u5c4f\u969c\uff09<\/h2>\n<p>memory_usage_\u7684\u7c7b\u578b\u662fAtomicPointer,\u8fd9\u662f\u4e00\u4e2a\u539f\u5b50\u7c7b\u578b\uff0c\u9488\u5bf9\u4e0d\u540c\u5e73\u53f0\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u3002\u53ef\u4ee5\u7528\u6765\u7ebf\u7a0b\u5b89\u5168\u7684\u5b58\u50a8\u548c\u83b7\u53d6\u503c\u3002<\/p>\n<p>AtomicPointer\u7c7b\u5b9e\u73b0\u4e86\u51e0\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre><code>NoBarrier_Load();\nNoBarrier_Store(void* v)\nAcquire_Load();\nRelease_Store(void* v)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u6d89\u53ca\u5230\u4e00\u4e2aMemory Barrier\uff08\u5185\u5b58\u5c4f\u969c,Memory fence\uff09\u7684\u6982\u5ff5\u3002<\/p>\n<p>\u5f15\u7528\u4e00\u4e2awikipedia\u7684\u4f8b\u5b50\uff1a<\/p>\n<blockquote><p>\n  <em>Processor #1:<\/em><\/p>\n<p>  while (f <span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\"> 0);<br \/>\n   \/\/ Memory fence required here<br \/>\n   print x;<\/span><\/p>\n<p>  <em>Processor #2:<\/em><\/p>\n<p>  x = 42;<br \/>\n   \/\/ Memory fence required here<br \/>\n   f = 1;\n<\/p><\/blockquote>\n<p>\u4e00\u4e2a\u4e24\u6838\u5fc3\u7684CPU\u6309\u7167\u4e0a\u8ff0\u4f8b\u5b50\u6267\u884c\u3002\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f88\u591a\u79cd\u60c5\u51b5\uff1a<br \/>\n\u867d\u7136\u6211\u4eec\u5e0c\u671b\u7684\u662f\u6253\u5370\u201c42\u201d\u3002\u4f46\u662f\u5982\u679c\u6838\u5fc3#2\u5b58\u50a8\u64cd\u4f5c\u4e71\u5e8f\u6267\u884c(out-of-order)\uff0c\u5c31\u6709\u53ef\u80fdf\u5728x\u4e4b\u524d\u88ab\u66f4\u65b0\u4e86\u3002\u8fd9\u6837\u6253\u5370\u8bed\u53e5\u5c31\u53ef\u80fd\u6253\u5370\u51fa\u201c0\u201d\u3002\u540c\u6837\u7684\uff0c\u6838\u5fc3#1\u8bfb\u53d6\u64cd\u4f5c\u53ef\u80fd\u4e5f\u4e71\u5e8f\u6267\u884c\uff0cx\u5c31\u6709\u53ef\u80fd\u5728f\u88ab\u5224\u65ad\u4e4b\u524d\u8bfb\u53d6\u4e86\uff0c\u6253\u5370\u8bed\u53e5\u53ef\u80fd\u6253\u5370\u51fa\u4e00\u4e2a\u65e0\u6cd5\u9884\u671f\u7684\u503c\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u60c5\u51b5\u90fd\u662f\u4e0d\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u4f7f\u7528memory barrier\u53ef\u4ee5\u907f\u514dout-of-order\u7684\u95ee\u9898\u3002<\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\u5c31\u662f\uff0c\u5728\u591aCPU\u5171\u4eab\u5185\u5b58\u7a7a\u95f4\u7684\u60c5\u51b5\u4e0b\uff0c\u4e24\u6761\u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u5df2\u7ecf\u65e0\u6cd5\u4fdd\u8bc1\u4e86\u3002\u9700\u8981memory barrier\u6765\u4fdd\u8bc1\u6267\u884c\u987a\u5e8f\u7684\u6b63\u786e\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5355CPU\u662f\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\u7684\u3002<\/p>\n<p>\u73b0\u5728\u518d\u6765\u89e3\u91ca\u4e0a\u9762\u76844\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u3002<\/p>\n<pre><code>NoBarrier_Load();           \/\/\u4e0d\u4f7f\u7528\u5185\u5b58\u5c4f\u969c\u7684\u8bfb\u53d6\nNoBarrier_Store(void* v)    \/\/\u4e0d\u4f7f\u7528\u5185\u5b58\u5c4f\u969c\u7684\u5b58\u50a8\nAcquire_Load();             \/\/\u4f7f\u7528\u5185\u5b58\u5c4f\u969c\u7684\u8bfb\u53d6\nRelease_Store(void* v)      \/\/\u4f7f\u7528\u5185\u5b58\u5c4f\u969c\u7684\u5b58\u50a8\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Varint Varint\u662f\u5728leveldb\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e00\u79cd\u53d8\u957f\u7684\u6574\u6570\u7c7b\u578b\uff0cVarint\u5176\u5b9e\u548cunicod &hellip; <a href=\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,23],"tags":[27,24,26],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-cplusplus","category-leveldb","tag-arena","tag-leveldb","tag-varint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001Varint Varint\u662f\u5728leveldb\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e00\u79cd\u53d8\u957f\u7684\u6574\u6570\u7c7b\u578b\uff0cVarint\u5176\u5b9e\u548cunicod &hellip; \u7ee7\u7eed\u9605\u8bfbleveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\" \/>\n<meta property=\"og:site_name\" content=\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-17T15:45:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-05T14:00:13+00:00\" \/>\n<meta name=\"author\" content=\"jiangpengfei\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"jiangpengfei\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\"},\"author\":{\"name\":\"jiangpengfei\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"headline\":\"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0\",\"datePublished\":\"2017-07-17T15:45:28+00:00\",\"dateModified\":\"2023-07-05T14:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\"},\"wordCount\":142,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"keywords\":[\"Arena\",\"leveldb\",\"Varint\"],\"articleSection\":[\"c++\",\"levelDB\u6e90\u7801\u9605\u8bfb\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\",\"url\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\",\"name\":\"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/#website\"},\"datePublished\":\"2017-07-17T15:45:28+00:00\",\"dateModified\":\"2023-07-05T14:00:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.myway5.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.myway5.com\/#website\",\"url\":\"https:\/\/www.myway5.com\/\",\"name\":\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"description\":\"\u60f3\u5565\u5462\",\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.myway5.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\",\"name\":\"jiangpengfei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"caption\":\"jiangpengfei\"},\"logo\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.myway5.com\/index.php\/author\/joyme\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/","og_locale":"zh_CN","og_type":"article","og_title":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","og_description":"\u4e00\u3001Varint Varint\u662f\u5728leveldb\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e00\u79cd\u53d8\u957f\u7684\u6574\u6570\u7c7b\u578b\uff0cVarint\u5176\u5b9e\u548cunicod &hellip; \u7ee7\u7eed\u9605\u8bfbleveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0","og_url":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/","og_site_name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","article_published_time":"2017-07-17T15:45:28+00:00","article_modified_time":"2023-07-05T14:00:13+00:00","author":"jiangpengfei","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"jiangpengfei","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#article","isPartOf":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/"},"author":{"name":"jiangpengfei","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"headline":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0","datePublished":"2017-07-17T15:45:28+00:00","dateModified":"2023-07-05T14:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/"},"wordCount":142,"commentCount":0,"publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"keywords":["Arena","leveldb","Varint"],"articleSection":["c++","levelDB\u6e90\u7801\u9605\u8bfb"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/","url":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/","name":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","isPartOf":{"@id":"https:\/\/www.myway5.com\/#website"},"datePublished":"2017-07-17T15:45:28+00:00","dateModified":"2023-07-05T14:00:13+00:00","breadcrumb":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.myway5.com\/index.php\/2017\/07\/17\/leveldb-varint-arena\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.myway5.com\/"},{"@type":"ListItem","position":2,"name":"leveldb\u6e90\u7801\u9605\u8bfb\uff08\u4e8c\uff09\u2014\u2014 Varint\u548cArena\u7684\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.myway5.com\/#website","url":"https:\/\/www.myway5.com\/","name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","description":"\u60f3\u5565\u5462","publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.myway5.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685","name":"jiangpengfei","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","caption":"jiangpengfei"},"logo":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/"},"url":"https:\/\/www.myway5.com\/index.php\/author\/joyme\/"}]}},"views":6917,"_links":{"self":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":4,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":1607,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/167\/revisions\/1607"}],"wp:attachment":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}