{"action":"created","comment":{"url":"https://api.github.com/repos/studygolang/GCTT/pulls/comments/167505411","pull_request_review_id":95732517,"id":167505411,"diff_hunk":"@@ -0,0 +1,320 @@\n+# 语言机制之逃逸分析（Language Mechanics On Escape Analysis）\n+\n+## 前序（Prelude）\n+\n+本系列文章总共四篇，主要帮助大家理解Go语言中一些语法结构和其背后的设计原则，包括指针、栈、堆、逃逸分析和值/指针传递。这是第二篇，主要介绍堆和逃逸分析。\n+\n+以下是本系列文章的索引：\n+\n+1) 语言机制之栈与指针\n+2) 语言机制之逃逸分析\n+3) 语言机制之内存剖析\n+4) 语言机制之数据和语法的设计哲学\n+\n+## 介绍（Introduction）\n+\n+在四部分系列的第一部分，我用一个将值共享给 goroutine 栈的例子介绍了指针结构的基础。而我没有说的是值存在栈之上的情况。为了理解这个，你需要学习值存储的另外一个位置：堆。有这个基础，就可以开始学习逃逸分析。\n+\n+逃逸分析是编译器用来决定你的程序中值的位置的过程。特别地，编译器执行静态代码分析，以确定一个构造体的实例化值是否会逃逸到堆。在 Go 语言中，没有关键字或者函数你可以用来直接地驱使编译器做这个决定。只能够通过你写的方式来作出这个决定。\n+\n+## 堆（Heaps）\n+\n+堆是内存的第二区域，除了栈之外，用来存储值的地方。堆无法像栈一样能自清理，所以使用这部分内存会造成很大的开销（相比于使用栈）。重要的是，开销跟 GC（垃圾收集），即被牵扯进来保证这部分区域干净的程序，有很大的关系。当垃圾收集程序运行时，它会占用你的可用 CPU 容量的 25%。更者，它会造成微秒级的 “stop the world” 的延时。拥有 GC 的好处是你可以不再关注堆内存的管理，这部分很复杂，是历史上容易出错的地方。\n+\n+在G o 中，会将一部分值分配到堆上。这些分配给 GC 带来了压力，因为堆上没有被指针索引的值都需要被删除。越多需要被检查和删除的值，会给每次运行 GC 时带来越多的工作。所以，分配算法不断地工作，以平衡堆的大小和它运行的速度。\n+\n+## 分享栈（Sharing Stacks）\n+\n+在 Go 语言中，不允许 goroutine 中的指针指向另外一个 goroutine 的栈。这是因为当栈增长或者收缩时，goroutine 中的栈内存会被一块新的内存替换。如果运行时需要追踪指针指向其他的 goroutine 的栈，就会造成非常多需要管理的内存，以至于更新指向那些栈的指针将使 “stop the world” 问题更严重。\n+\n+这里有一个栈被替换好几次的例子。看输出的第 2 和第 6 行。你会看到 main 函数中的栈的字符串地址值改变了两次。\n+[https://play.golang.org/p/pxn5u4EBSI](https://play.golang.org/p/pxn5u4EBSI)\n+\n+## 逃逸机制（Escape Mechanics）\n+\n+任何时候，一个值被分享到函数栈帧范围之外，它都会在堆上被重新分配。这是逃逸分析算法发现这些情况和管控这一层的工作。（内存的）完整性在于确保对任何值的访问始终是准确、一致和高效的。\n+\n+通过查看这个语言机制了解逃逸分析。\n+[https://play.golang.org/p/Y_VZxYteKO](https://play.golang.org/p/Y_VZxYteKO)\n+\n+### 表 1\n+```go\n+01 package main","path":"translated/tech/Language Mechanics On Escape Analysis.md","position":42,"original_position":42,"commit_id":"469767b2c1d96b3007b2e6a9d93fad2c1b3fe79d","original_commit_id":"7e7da9bfe9e61f4033972eefde1ade6719ae81c1","user":{"login":"gogeof","id":13129099,"avatar_url":"https://avatars0.githubusercontent.com/u/13129099?v=4","gravatar_id":"","url":"https://api.github.com/users/gogeof","html_url":"https://github.com/gogeof","followers_url":"https://api.github.com/users/gogeof/followers","following_url":"https://api.github.com/users/gogeof/following{/other_user}","gists_url":"https://api.github.com/users/gogeof/gists{/gist_id}","starred_url":"https://api.github.com/users/gogeof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gogeof/subscriptions","organizations_url":"https://api.github.com/users/gogeof/orgs","repos_url":"https://api.github.com/users/gogeof/repos","events_url":"https://api.github.com/users/gogeof/events{/privacy}","received_events_url":"https://api.github.com/users/gogeof/received_events","type":"User","site_admin":false},"body":"好的","created_at":"2018-02-12T09:53:08Z","updated_at":"2018-02-12T09:53:08Z","html_url":"https://github.com/studygolang/GCTT/pull/335#discussion_r167505411","pull_request_url":"https://api.github.com/repos/studygolang/GCTT/pulls/335","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/comments/167505411"},"html":{"href":"https://github.com/studygolang/GCTT/pull/335#discussion_r167505411"},"pull_request":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/335"}},"in_reply_to_id":167496441},"pull_request":{"url":"https://api.github.com/repos/studygolang/GCTT/pulls/335","id":168522378,"html_url":"https://github.com/studygolang/GCTT/pull/335","diff_url":"https://github.com/studygolang/GCTT/pull/335.diff","patch_url":"https://github.com/studygolang/GCTT/pull/335.patch","issue_url":"https://api.github.com/repos/studygolang/GCTT/issues/335","number":335,"state":"open","locked":false,"title":"【翻译完成】Language Mechanics On Escape Analysis","user":{"login":"gogeof","id":13129099,"avatar_url":"https://avatars0.githubusercontent.com/u/13129099?v=4","gravatar_id":"","url":"https://api.github.com/users/gogeof","html_url":"https://github.com/gogeof","followers_url":"https://api.github.com/users/gogeof/followers","following_url":"https://api.github.com/users/gogeof/following{/other_user}","gists_url":"https://api.github.com/users/gogeof/gists{/gist_id}","starred_url":"https://api.github.com/users/gogeof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gogeof/subscriptions","organizations_url":"https://api.github.com/users/gogeof/orgs","repos_url":"https://api.github.com/users/gogeof/repos","events_url":"https://api.github.com/users/gogeof/events{/privacy}","received_events_url":"https://api.github.com/users/gogeof/received_events","type":"User","site_admin":false},"body":"## 详细描述\r\n- 【翻译完成】Language Mechanics On Escape Analysis #288 \r\n- 请校对","created_at":"2018-02-12T08:40:31Z","updated_at":"2018-02-12T09:53:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e295ea72f81e143610c461cfff18a7493d5a999b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/studygolang/GCTT/pulls/335/commits","review_comments_url":"https://api.github.com/repos/studygolang/GCTT/pulls/335/comments","review_comment_url":"https://api.github.com/repos/studygolang/GCTT/pulls/comments{/number}","comments_url":"https://api.github.com/repos/studygolang/GCTT/issues/335/comments","statuses_url":"https://api.github.com/repos/studygolang/GCTT/statuses/469767b2c1d96b3007b2e6a9d93fad2c1b3fe79d","head":{"label":"gogeof:master","ref":"master","sha":"469767b2c1d96b3007b2e6a9d93fad2c1b3fe79d","user":{"login":"gogeof","id":13129099,"avatar_url":"https://avatars0.githubusercontent.com/u/13129099?v=4","gravatar_id":"","url":"https://api.github.com/users/gogeof","html_url":"https://github.com/gogeof","followers_url":"https://api.github.com/users/gogeof/followers","following_url":"https://api.github.com/users/gogeof/following{/other_user}","gists_url":"https://api.github.com/users/gogeof/gists{/gist_id}","starred_url":"https://api.github.com/users/gogeof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gogeof/subscriptions","organizations_url":"https://api.github.com/users/gogeof/orgs","repos_url":"https://api.github.com/users/gogeof/repos","events_url":"https://api.github.com/users/gogeof/events{/privacy}","received_events_url":"https://api.github.com/users/gogeof/received_events","type":"User","site_admin":false},"repo":{"id":119762880,"name":"GCTT","full_name":"gogeof/GCTT","owner":{"login":"gogeof","id":13129099,"avatar_url":"https://avatars0.githubusercontent.com/u/13129099?v=4","gravatar_id":"","url":"https://api.github.com/users/gogeof","html_url":"https://github.com/gogeof","followers_url":"https://api.github.com/users/gogeof/followers","following_url":"https://api.github.com/users/gogeof/following{/other_user}","gists_url":"https://api.github.com/users/gogeof/gists{/gist_id}","starred_url":"https://api.github.com/users/gogeof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gogeof/subscriptions","organizations_url":"https://api.github.com/users/gogeof/orgs","repos_url":"https://api.github.com/users/gogeof/repos","events_url":"https://api.github.com/users/gogeof/events{/privacy}","received_events_url":"https://api.github.com/users/gogeof/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gogeof/GCTT","description":"GCTT Go中文网翻译组。","fork":true,"url":"https://api.github.com/repos/gogeof/GCTT","forks_url":"https://api.github.com/repos/gogeof/GCTT/forks","keys_url":"https://api.github.com/repos/gogeof/GCTT/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gogeof/GCTT/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gogeof/GCTT/teams","hooks_url":"https://api.github.com/repos/gogeof/GCTT/hooks","issue_events_url":"https://api.github.com/repos/gogeof/GCTT/issues/events{/number}","events_url":"https://api.github.com/repos/gogeof/GCTT/events","assignees_url":"https://api.github.com/repos/gogeof/GCTT/assignees{/user}","branches_url":"https://api.github.com/repos/gogeof/GCTT/branches{/branch}","tags_url":"https://api.github.com/repos/gogeof/GCTT/tags","blobs_url":"https://api.github.com/repos/gogeof/GCTT/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gogeof/GCTT/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gogeof/GCTT/git/refs{/sha}","trees_url":"https://api.github.com/repos/gogeof/GCTT/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gogeof/GCTT/statuses/{sha}","languages_url":"https://api.github.com/repos/gogeof/GCTT/languages","stargazers_url":"https://api.github.com/repos/gogeof/GCTT/stargazers","contributors_url":"https://api.github.com/repos/gogeof/GCTT/contributors","subscribers_url":"https://api.github.com/repos/gogeof/GCTT/subscribers","subscription_url":"https://api.github.com/repos/gogeof/GCTT/subscription","commits_url":"https://api.github.com/repos/gogeof/GCTT/commits{/sha}","git_commits_url":"https://api.github.com/repos/gogeof/GCTT/git/commits{/sha}","comments_url":"https://api.github.com/repos/gogeof/GCTT/comments{/number}","issue_comment_url":"https://api.github.com/repos/gogeof/GCTT/issues/comments{/number}","contents_url":"https://api.github.com/repos/gogeof/GCTT/contents/{+path}","compare_url":"https://api.github.com/repos/gogeof/GCTT/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gogeof/GCTT/merges","archive_url":"https://api.github.com/repos/gogeof/GCTT/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gogeof/GCTT/downloads","issues_url":"https://api.github.com/repos/gogeof/GCTT/issues{/number}","pulls_url":"https://api.github.com/repos/gogeof/GCTT/pulls{/number}","milestones_url":"https://api.github.com/repos/gogeof/GCTT/milestones{/number}","notifications_url":"https://api.github.com/repos/gogeof/GCTT/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gogeof/GCTT/labels{/name}","releases_url":"https://api.github.com/repos/gogeof/GCTT/releases{/id}","deployments_url":"https://api.github.com/repos/gogeof/GCTT/deployments","created_at":"2018-02-01T00:52:24Z","updated_at":"2018-02-01T00:52:26Z","pushed_at":"2018-02-12T09:50:00Z","git_url":"git://github.com/gogeof/GCTT.git","ssh_url":"git@github.com:gogeof/GCTT.git","clone_url":"https://github.com/gogeof/GCTT.git","svn_url":"https://github.com/gogeof/GCTT","homepage":"https://studygolang.com/gctt","size":4685,"stargazers_count":0,"watchers_count":0,"language":"Shell","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"studygolang:master","ref":"master","sha":"9ff17162de00ccf32afec74a07adf2ca50deda36","user":{"login":"studygolang","id":3772217,"avatar_url":"https://avatars3.githubusercontent.com/u/3772217?v=4","gravatar_id":"","url":"https://api.github.com/users/studygolang","html_url":"https://github.com/studygolang","followers_url":"https://api.github.com/users/studygolang/followers","following_url":"https://api.github.com/users/studygolang/following{/other_user}","gists_url":"https://api.github.com/users/studygolang/gists{/gist_id}","starred_url":"https://api.github.com/users/studygolang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/studygolang/subscriptions","organizations_url":"https://api.github.com/users/studygolang/orgs","repos_url":"https://api.github.com/users/studygolang/repos","events_url":"https://api.github.com/users/studygolang/events{/privacy}","received_events_url":"https://api.github.com/users/studygolang/received_events","type":"Organization","site_admin":false},"repo":{"id":110936509,"name":"GCTT","full_name":"studygolang/GCTT","owner":{"login":"studygolang","id":3772217,"avatar_url":"https://avatars3.githubusercontent.com/u/3772217?v=4","gravatar_id":"","url":"https://api.github.com/users/studygolang","html_url":"https://github.com/studygolang","followers_url":"https://api.github.com/users/studygolang/followers","following_url":"https://api.github.com/users/studygolang/following{/other_user}","gists_url":"https://api.github.com/users/studygolang/gists{/gist_id}","starred_url":"https://api.github.com/users/studygolang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/studygolang/subscriptions","organizations_url":"https://api.github.com/users/studygolang/orgs","repos_url":"https://api.github.com/users/studygolang/repos","events_url":"https://api.github.com/users/studygolang/events{/privacy}","received_events_url":"https://api.github.com/users/studygolang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/studygolang/GCTT","description":"GCTT Go中文网翻译组。","fork":false,"url":"https://api.github.com/repos/studygolang/GCTT","forks_url":"https://api.github.com/repos/studygolang/GCTT/forks","keys_url":"https://api.github.com/repos/studygolang/GCTT/keys{/key_id}","collaborators_url":"https://api.github.com/repos/studygolang/GCTT/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/studygolang/GCTT/teams","hooks_url":"https://api.github.com/repos/studygolang/GCTT/hooks","issue_events_url":"https://api.github.com/repos/studygolang/GCTT/issues/events{/number}","events_url":"https://api.github.com/repos/studygolang/GCTT/events","assignees_url":"https://api.github.com/repos/studygolang/GCTT/assignees{/user}","branches_url":"https://api.github.com/repos/studygolang/GCTT/branches{/branch}","tags_url":"https://api.github.com/repos/studygolang/GCTT/tags","blobs_url":"https://api.github.com/repos/studygolang/GCTT/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/studygolang/GCTT/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/studygolang/GCTT/git/refs{/sha}","trees_url":"https://api.github.com/repos/studygolang/GCTT/git/trees{/sha}","statuses_url":"https://api.github.com/repos/studygolang/GCTT/statuses/{sha}","languages_url":"https://api.github.com/repos/studygolang/GCTT/languages","stargazers_url":"https://api.github.com/repos/studygolang/GCTT/stargazers","contributors_url":"https://api.github.com/repos/studygolang/GCTT/contributors","subscribers_url":"https://api.github.com/repos/studygolang/GCTT/subscribers","subscription_url":"https://api.github.com/repos/studygolang/GCTT/subscription","commits_url":"https://api.github.com/repos/studygolang/GCTT/commits{/sha}","git_commits_url":"https://api.github.com/repos/studygolang/GCTT/git/commits{/sha}","comments_url":"https://api.github.com/repos/studygolang/GCTT/comments{/number}","issue_comment_url":"https://api.github.com/repos/studygolang/GCTT/issues/comments{/number}","contents_url":"https://api.github.com/repos/studygolang/GCTT/contents/{+path}","compare_url":"https://api.github.com/repos/studygolang/GCTT/compare/{base}...{head}","merges_url":"https://api.github.com/repos/studygolang/GCTT/merges","archive_url":"https://api.github.com/repos/studygolang/GCTT/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/studygolang/GCTT/downloads","issues_url":"https://api.github.com/repos/studygolang/GCTT/issues{/number}","pulls_url":"https://api.github.com/repos/studygolang/GCTT/pulls{/number}","milestones_url":"https://api.github.com/repos/studygolang/GCTT/milestones{/number}","notifications_url":"https://api.github.com/repos/studygolang/GCTT/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/studygolang/GCTT/labels{/name}","releases_url":"https://api.github.com/repos/studygolang/GCTT/releases{/id}","deployments_url":"https://api.github.com/repos/studygolang/GCTT/deployments","created_at":"2017-11-16T07:10:44Z","updated_at":"2018-02-12T08:28:51Z","pushed_at":"2018-02-12T09:50:01Z","git_url":"git://github.com/studygolang/GCTT.git","ssh_url":"git@github.com:studygolang/GCTT.git","clone_url":"https://github.com/studygolang/GCTT.git","svn_url":"https://github.com/studygolang/GCTT","homepage":"https://studygolang.com/gctt","size":5523,"stargazers_count":294,"watchers_count":294,"language":"Shell","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":124,"mirror_url":null,"archived":false,"open_issues_count":34,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":124,"open_issues":34,"watchers":294,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/335"},"html":{"href":"https://github.com/studygolang/GCTT/pull/335"},"issue":{"href":"https://api.github.com/repos/studygolang/GCTT/issues/335"},"comments":{"href":"https://api.github.com/repos/studygolang/GCTT/issues/335/comments"},"review_comments":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/335/comments"},"review_comment":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/studygolang/GCTT/pulls/335/commits"},"statuses":{"href":"https://api.github.com/repos/studygolang/GCTT/statuses/469767b2c1d96b3007b2e6a9d93fad2c1b3fe79d"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"gogeof"}}