{"id":10006853,"date":"2022-11-17T00:02:01","date_gmt":"2022-11-16T16:02:01","guid":{"rendered":"https:\/\/ityet.com\/?p=10006853"},"modified":"2022-11-17T00:02:01","modified_gmt":"2022-11-16T16:02:01","slug":"%e6%89%93%e8%84%b8%e4%ba%86%e5%85%84%e5%bc%9f%e4%bb%ac%ef%bc%8cgo1-20-arena-%e6%9d%a5%e4%ba%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006853","title":{"rendered":"\u6253\u8138\u4e86\u5144\u5f1f\u4eec\uff0cGo1.20 arena \u6765\u4e86\uff01"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p>\u5927\u6982\u534a\u5e74\u524d\uff0c\u6211\u5199\u8fc7\u4e00\u7bc7\u6587\u7ae0\u300aGo \u8981\u8fdd\u80cc\u521d\u5fc3\u5417\uff1f\u65b0\u63d0\u6848\uff1a\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u300b\u3002\u6709\u5174\u8da3\u4e86\u6df1\u5165\u89e3\u7684\u540c\u5b66\uff0c\u53ef\u4ee5\u518d\u56de\u987e\u4e00\u4e0b\u3002<\/p>\n<p>\u5f53\u65f6\u6211\u4eec\u8fd8\u60f3\u7740 Go \u56e2\u961f\u5e94\u8be5\u4e0d\u4f1a\u63a5\u7eb3\uff0c\u81f3\u5c11\u4e0d\u4f1a\u90a3\u4e48\u5feb\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042812971\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u61d2\u5f97\u7ffb\u4e5f\u53ef\u4ee5\u770b\u6211\u518d\u6b21\u9053\u6765\uff0c\u672c\u6587\u63d0\u5230\u7684\u63d0\u6848\u300a<a href=\"https:\/\/link.segmentfault.com\/?enc=xySAmbg5Sf3Clz9FYsmeGA%3D%3D.3MUuPVErmrwarWs8hHAPZkUP4LSciplqicYpSqDJ96Bz%2BfO2Bs2zNJglELwR2E%2Be\" rel=\"nofollow\" title=\"proposal: arena: new package providing memory arenas\">proposal: arena: new package providing memory arenas<\/a>\u300b\uff0c\u8fd9\u5176\u4e2d\u7684 Arena \u5c06\u4f1a\u662f\u4e00\u4e2a\u7a81\u7834\u9879\u3002<\/p>\n<h2>\u5feb\u901f\u80cc\u666f<\/h2>\n<p>Arena \u6307\u7684\u662f\u4e00\u79cd<strong>\u4ece\u4e00\u4e2a\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u5206\u914d\u4e00\u7ec4\u5185\u5b58\u5bf9\u8c61\u7684\u65b9\u5f0f<\/strong>\u3002\u4f18\u70b9\u6bd4\u4e00\u822c\u7684\u5185\u5b58\u5206\u914d\u66f4\u6709\u6548\u7387\uff0c\u4e5f\u53ef\u4ee5\u4e00\u6b21\u6027\u91ca\u653e\u3002\u5f53\u7136\u4e86\uff0c\u5b83\u7684\u91cd\u70b9\u662f\u8981<strong>\u624b\u52a8\u7ba1\u7406\u5185\u5b58<\/strong>\u3002<\/p>\n<p>Go \u56e2\u961f\u5e0c\u671b\u52a0\u8fdb Go \u7279\u6027\u4e2d\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">import (\n \u201carena\u201d\n \u2026\n)\n\ntype T struct {\n val int\n}\n\nfunc main() {\n a := arena.New()\n var ptrT *T\n a.New(&amp;ptrT)\n ptrT.val = 1\n\n var sliceT []T\n a.NewSlice(&amp;sliceT, 100)\n sliceT[99].val = 4\n\n a.Free()\n}<\/code><\/pre>\n<p>\u624b\u52a8\u8c03\u7528 <code>arena.New<\/code> \u65b9\u6cd5\u5206\u914d arena \u5185\u5b58\uff0c\u518d\u8c03\u7528 <code>Free<\/code> \u65b9\u6cd5\u8fdb\u884c\u91ca\u653e\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bb2\u5c31\u662f\u53ef\u4ee5\u624b\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u5c31\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u4e86\uff0c\u4e5f \u201c\u5bb9\u6613\u201d \u5d29\u3002<\/p>\n<h2>\u6700\u65b0\u8fdb\u5c55<\/h2>\n<p>\u8fd9\u4e2a\u63d0\u6848\u4e00\u76f4\u5728 issues \u4e0a\u9002\u5ea6\u7684\u4e89\u8bae\u8ba8\u8bba\uff0c@Michael Knyszek \u5927\u4f6c\u4ee3\u7801\u5199\u7684\u5f88\u5feb\uff0c\u5df2\u7ecf\u76f4\u63a5\u63d0\u4ea4\u4e0a\u53bb\u4e86&#8230;\u76f4\u5230\u6700\u8fd1\u88ab\u4eba\u53d1\u73b0\uff0c\u8ba9\u4ed6\u66f4\u65b0\u8fdb\u5ea6\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042812972\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5df2\u7ecf\u660e\u786e\uff1a<strong>Go1.20 \u5c06\u4f1a\u652f\u6301 arena \u7279\u6027<\/strong>\uff0c\u901a\u8fc7 <code>GOEXPERIMENT=arena<\/code> \u6765\u6253\u5f00\uff0c\u63a5\u53d7\u5927\u5bb6\u7684 review \u548c\u4f7f\u7528\uff0c\u6297\u963b\u5f88\u5c0f\u3002<\/p>\n<p>\u5df2\u5b9e\u73b0 API \u548c\u539f\u63d0\u6848\u4e0d\u540c\u7684\u5730\u65b9\u6709\uff1a<\/p>\n<ul>\n<li>API \u4f7f\u7528\u4e86\u6cdb\u578b\uff0c\u4f8b\u5982\uff1a<code>arena.New[int](myArena &quot;int&quot;)<\/code>\u3002<\/li>\n<li>Arena \u7684 \u5757\u5927\u5c0f\u662f 8 MiB \u800c\u4e0d\u662f 64 MiB\uff0c\u4f3c\u4e4e\u5728\u66f4\u591a\u60c5\u51b5\u4e0b\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u3002<\/li>\n<li>MSAN \u548c ASAN \u6a21\u5f0f\u53ef\u7528\u4e8e\u8bc6\u522b\u4e0d\u4f1a\u5bfc\u81f4\u5d29\u6e83\u7684 use-after-free \u9519\u8bef\uff08\u5185\u5b58\u635f\u574f\u5e94\u8be5\u4ecd\u7136\u662f\u4e0d\u53ef\u80fd\u7684\uff09\u3002 \u9700\u6ce8\u610f\uff0c\u8fd9\u4e9b\u6a21\u5f0f\u5bf9\u975e cgo \u7684 Go \u7a0b\u5e8f\u51e0\u4e4e\u6ca1\u6709\u4f5c\u7528\u3002Arena \u662f\u4e2a\u4f8b\u5916\u3002<\/li>\n<\/ul>\n<p>\u53e6\u5916\u6839\u636e\u793e\u533a\u7684\u53cd\u9988\uff0c\u53ef\u80fd\u8fd8\u4f1a\u51fa\u73b0\u914d\u5957\u7c7b\u578b\u7684 Arena\u3002\u5982\u4e0b\u51fd\u6570\u7b7e\u540d\uff1a<\/p>\n<pre><code class=\"go\">\/\/ MakeMap creates a new map[K]V with the provided capacity.\n\/\/ The map[K]V must not be used after the arena is freed.\n\/\/ Accessing the underlying storage of the map after free may result in a fault,\n\/\/ but this fault is also not guaranteed.\nfunc MakeMap[K comparable, V any](a *Arena, cap int &quot;K comparable, V any&quot;) map[K]V { ... }<\/code><\/pre>\n<p>\u5728 Go1.20 \u53d1\u5e03\u8be5\u65b0\u7279\u6027\u7684\u8bdd\uff0c\u6309\u7167\u53d1\u5e03\u5468\u671f\u8ba1\u5212\uff0c\u662f 2 \u6708\u4efd\u5de6\u53f3\u53d1\u5e03\uff0c\u76f8\u4fe1\u5927\u5bb6\u5f88\u5feb\u5c31\u80fd\u7528\u4e0a\uff0c\u53ef\u4ee5\u591a\u591a\u5173\u6ce8\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4e00\u5f00\u59cb\u4e86\u89e3\u8fd9\u4e2a\u63d0\u6848\u65f6\uff0c\u8fd8\u60f3\u7740 Go \u641e\u4e86\u5feb 10 \u5e74\u624d\u91c7\u7eb3\u548c\u63a8\u8fdb\u6cdb\u578b\u3002\u8fd9 Arena \u5e94\u8be5\u4e0d\u81f3\u4e8e\u8fd9\u4e48\u5feb\u5427\uff1f\u6bd5\u7adf\u52a0\u8fdb\u53bb\u4e86\uff0c\u8bb8\u591a\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5199\u7684\u590d\u6742\u8d77\u6765\u3002<\/p>\n<p>\u6ca1\u60f3\u5230&#8230;\u73b0\u5b9e\u6253\u8138\u6765\u7684\u592a\u5feb\uff0c\u63a8\u8fdb\u7684\u5f88\u5feb\u3002<\/p>\n<p>\u5c31\u50cf\u5176\u4ed6\u5c0f\u4f19\u4f34\u8bf4\u7684\uff0c\u8fd9\u53ef\u4ee5\u4ece\u4ee3\u7801\u4f18\u5316\u6027\u80fd\uff0c\u800c\u4e0d\u9700\u8981\u780d\u9700\u6c42\u3002\u4e5f\u662f\u4e00\u4e2a\u6709\u610f\u601d\u4e14\u4e0d\u9519\u7684\u6e90\u52a8\u529b\uff01<\/p>\n<p>\u636e\u5c0f\u9053\u6d88\u606f\uff0c\u67d0\u4e9b\u540c\u5b66\u8868\u793a\u5728\u6846\u67b6\u548c\u5176\u5b83\u573a\u666f\u6d4b\u8fc7\uff0c\u6709\u8bf4\u53d8\u5f97\u5feb\u4e86\uff0c\u6709\u8bf4\u6ca1\u5dee\u591a\u5c11\u3002\u6bd4\u8f83\u8ff7\uff0c\u63d0\u6848\u5185\u6682\u65f6\u672a\u63d0\u4f9b\u6d4b\u8bd5\u62a5\u544a\uff0c\u4e0d\u597d\u5b9a\u8bba\u3002<\/p>\n<p>Go 1.20 Beta \u5c06\u5728\u672a\u6765\u51e0\u5468\u5185\uff082022.11 \u6708\u5e95\u524d\uff09\u53d1\u5e03\uff0c\u8ba9\u6211\u4eec\u62ed\u76ee\u4ee5\u5f85\uff1a\uff09<\/p>\n<blockquote><p>\u6587\u7ae0\u6301\u7eed\u66f4\u65b0\uff0c\u53ef\u4ee5\u5fae\u4fe1\u641c\u3010\u8111\u5b50\u8fdb\u714e\u9c7c\u4e86\u3011\u9605\u8bfb\uff0c\u672c\u6587 <strong>GitHub<\/strong> <a href=\"https:\/\/link.segmentfault.com\/?enc=SJluvSR75ThzSlOWtLu%2BEQ%3D%3D.UNyFzBK1zV0rB3yn%2BYvxbveORXfk%2B9hX%2F%2BEP1upJ8d8%3D\" rel=\"nofollow\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=NvhbB4DzCHvgU6J%2B4qVwRg%3D%3D.i6R8%2B94XCZgjFByVHg5g%2F8WnAvkdmfvyv94SQP6Rdbh%2FHCjxe7iW8%2FOkLXdbPgAx\" rel=\"nofollow\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<h2>\u63a8\u8350\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=c05ZJDi7Ohlqa4bQbsYnmw%3D%3D.6BHLo9Tswu%2FbwMyFgGrrsJ9V3nv06q6IXICgn8nQyHtET6NI7zNRKHintA4boQ5xoXiAe9A3Rz4ldxj7Bq1pUQ%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=cQuGD1yuvZC4Hko9gj%2B69g%3D%3D.toHdfjIV4GUQXTjrTZLRu9ON1EEoPiH7wyatx5SiaRnIq5OmKnPmnSLf4YdirBsmWLIUOp%2BR2An1Ea8FlokDiA%3D%3D\" rel=\"nofollow\">Go \u9519\u8bef\u5904\u7406\u65b0\u601d\u8def\uff1f\u7528\u5de6\u4fa7\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=0Gz9ZyI6G8Kxy2TKCoR6Fw%3D%3D.GC7imTVrjOzfuY0LaTvZL0nX%2BYJWDv2eXwFVTkM%2FrI%2F4RP7YbUqTr%2FupHsmdUSTs2d8yYJt4XxdEew%2FoTZjQyg%3D%3D\" rel=\"nofollow\">\u5148\u7779\u4e3a\u5feb\uff0cGo2 Error \u7684\u6323\u624e\u4e4b\u8def<\/a><\/li>\n<\/ul>\n<h3>Go \u56fe\u4e66\u7cfb\u5217<\/h3>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=1m42E9nO3vEchabzrJ%2FEgg%3D%3D.hpZoPMKpasJ12gGGylcjsDv7S%2BOOeaSTzm1Z3Z5%2BQJfCiAHqThpYq3dEwTyyFaOj\" rel=\"nofollow\">Go \u8bed\u8a00\u5165\u95e8\u7cfb\u5217\uff1a\u521d\u63a2 Go \u9879\u76ee\u5b9e\u6218<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=nmMbcQF5vaJTK5iM1Wxwaw%3D%3D.j7GetKucSNelRVilyLoJj7t208tSwfKIu9thonxGF%2F0%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u7f16\u7a0b\u4e4b\u65c5\uff1a\u6df1\u5165\u7528 Go \u505a\u9879\u76ee<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=CYL3BHZ%2F3w3uY7%2FEjKnHSg%3D%3D.7iofByM7OeMSNn1aojwYgKoSK8IzYewGgWJCkLGEvsU%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8bbe\u8ba1\u54f2\u5b66\uff1a\u4e86\u89e3 Go \u7684\u4e3a\u4ec0\u4e48\u548c\u8bbe\u8ba1\u601d\u8003<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=20mBPoShbvHAKpsVcqIiyg%3D%3D.71WtGCcOfgoa%2B0dKsw%2FhVVgMlhNikC9IppLivY0iNJI%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8fdb\u9636\u4e4b\u65c5\uff1a\u8fdb\u4e00\u6b65\u6df1\u5165 Go \u6e90\u7801<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042812969\">https:\/\/segmentfault.com\/a\/1190000042812969<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 \u5927\u6982\u534a\u5e74\u524d\uff0c\u6211\u5199\u8fc7\u4e00\u7bc7\u6587\u7ae0\u300aGo \u8981\u8fdd\u80cc\u521d\u5fc3\u5417\uff1f\u65b0\u63d0\u6848\uff1a\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u300b\u3002\u6709\u5174\u8da3\u4e86\u6df1\u5165\u89e3\u7684\u540c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006853","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006853"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006853\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}