Go 1.9 的 GC 改善

Update:被提醒後仔細看了一下,是 1.8 預設生效 (但保留選項切回來 debug),如果沒問題的話 1.9 把舊的方式拔乾淨:

Assuming things go smoothly, we will remove stack re-scanning support when the tree opens for Go 1.9 development.

標題就不改了... 以下原文。

在「Sub-millisecond GC pauses」這邊看到的。Golang 想辦法將 GC 造成的影響降低:「Proposal: Eliminate STW stack re-scanning」。

目標是解決最大的 GC pause 來源:

As of Go 1.7, the one remaining source of unbounded and potentially non-trivial stop-the-world (STW) time is stack re-scanning.

然後拿新的解法來戰,目前初步的測試看起來可以降到 50µs (== 0.05ms):

We propose to eliminate the need for stack re-scanning by switching to a hybrid write barrier that combines a Yuasa-style deletion write barrier [Yuasa '90] and a Dijkstra-style insertion write barrier [Dijkstra '78]. Preliminary experiments show that this can reduce worst-case STW time to under 50µs, and this approach may make it practical to eliminate STW mark termination altogether.

在「runtime: eliminate stack rescanning · Issue #17503 · golang/go」這邊可以看到進度,現在已經在 master branch 上了,看起來會在 1.9 的時候被放出來... 不過 worst case 的時間上修了 XDDD

The high level summary is that this reduces worst-case STW time to about 100 µs and typical 95%ile STW time to 50 µs (assuming, of course, that the OS doesn't get in the way and that the system isn't otherwise overloaded).

但看起來應該還是很大的效能改善,尤其是 CPU bound 的應用?

玩 Pokémon Go 抓怪用的 iPhone 手機殼...

看到「This iPhone thong case will help you catch 'em all in 'Pokémon Go'」這個,光看圖就知道意思了:

應該是用 3D Printer 做出來的:

If this sounds familiar, it's basically the commercialized, rubberized version of the 3D-printed Poké Ball "aimer" case that one guy made.

不過這不能丟 Curveball 啊 XDDD

Golang 1.7

Golang 1.7 主打更小的 binary size:「Smaller Go 1.7 binaries」:

Typical programs, ranging from tiny toys to large production programs, are about 30% smaller when built with Go 1.7.

還附了一張經典的「Hello, world」程式的分析:

由於現代 CPU 的速度與 L1/L2/... cache 有緊密關係,當 binary size 變小時,常常會伴隨著 memory access 變快 (因為 hitrate 提昇),所以 binary size 也是效能指數蠻重要的一環。

Go 上面的白箱安全性檢查

HP 的 open source 專案「Go AST Scanner」,分析 Go 的原始程式碼拉出 AST 進行分析 (Static program analysis),再找出可能的安全性問題。

雖然是 alpha 階段,但看起來是個好東西啊... 至少寫的太誇張的 SQL injection 可以掃出來。

舊 bug 新名字:httpoxy

依照慣例,security issue 都會取個名字,這次叫做 httpoxy:「A CGI application vulnerability for PHP, Go, Python and others」。

事情發生在兩個命名變數上的衝突:

  • RFC 3875 (The Common Gateway Interface (CGI) Version 1.1) 定義了 CGI 環境會把 Header 裡的 Proxy 欄位放到環境變數裡的 HTTP_PROXY
  • 而很多程式會拿環境變數裡的 HTTP_PROXY 當作 proxy 設定。

這件事情 2001 年在 libwww-perl 就有發生過 (並且修正),curl 也發生過 (然後修正),2012 年在 Ruby 的 Net::HTTP 也發生過 (也修正了)。

然後在 2016 年還是被發現有很多應用程式會中獎... 這頭好痛啊 :o

在 C 裡 Concurrency 的 Library

看到「libdill: Structured Concurrency for C」這個東西,在 C 裡實作了兩個不同種類的 concurrency,一個是 proc (process-based) 一個是 go (corouting-based)。

支援的 function 算是蠻清晰的,範例也很清楚:

#include <libdill.h>
#include <stdio.h>
#include <stdlib.h>

coroutine int worker(const char *text) {
    while(1) {
        printf("%s\n", text);
        msleep(now() + random() % 500);
    }
    return 0;
}

int main() {
    go(worker("Hello!"));
    go(worker("World!"));
    msleep(now() + 5000);
    return 0;
}

也有 channel 的觀念可以用,之後需要寫玩具的話應該是個好東西...

AlphaGo 不是使用 GPU 加速...

Google 今天公佈的資料中說明了 AlphaGo 不是用一般常見的 GPU 加速運算:「Google supercharges machine learning tasks with TPU custom chip」。

這是特別為 TensorFlow 製作的 ASIC:

The result is called a Tensor Processing Unit (TPU), a custom ASIC we built specifically for machine learning — and tailored for TensorFlow.

而 AlphaGo 用的版本是 TPU 版:

AlphaGo was powered by TPUs in the matches against Go world champion, Lee Sedol, enabling it to "think" much faster and look farther ahead between moves.

放 AlphaGo 的機櫃長這樣:

通常 ASIC 特製的版本會比 FPGA 或是 GPU 快上許多,這代表目前這些沒有大公司撐腰的圍棋軟體要跟 AlphaGo 拼,除非演算法上有重大的突破,不然就得用更大量的設備跟他換...

谷李五番棋今天開打

GoogleDeepMind 所研發出來的 AlphaGo李世乭的「谷李五番棋」將在今天開打。

中國規則、兩個小時、一分鐘讀秒:

The matches will be played under Chinese rules with a komi of 7.5 (the compensation points the player who goes second receives at the end of the match). Each player will receive two hours per match with three lots of 60-second byoyomi (countdown periods after they have finished their allotted time).

將在韓國時間下午一點開賽,對我們也就是十二點開賽:

The matches will be held at the Four Seasons Hotel, Seoul, South Korea, starting at 1pm local time (4am GMT; day before 11pm ET, 8pm PT) on March 9th, 10th, 12th, 13th and 15th.

將會有大量的媒體講解直播,毫無疑問的,YouTubeDeepMind 這個頻道會有直播,目前看起來是早上的十一點半就會開始了。

其他的頻道,台灣已知的有:

不過我應該會到處看吧,中國的圍棋網站「围棋TV」也會在十一點半開始直播。

很多人都有猜測勝負,但自從去年十月贏了樊麾後,不知道成長了多少。其實都是在資訊不足的情況下猜測,在這種情況下,人類大獲全勝或是電腦大獲全勝都不意外...

也因此,五番棋的第一盤應該是最轟動的,因為可以看出 AlphaGo 長到什麼程度... 不過聽說第一盤 Google 只會拿單機版出來應戰?所以如果輸的很慘的話就會拿雲端版來戰?

啊啊啊我好想看啊...

Go 1.6 把 HTTP/2 變成預設支援的功能

Go 的官方公告「Go 1.6 is released」提到了把 net/http 的 HTTP/2 預設啟用了:

In Go 1.6, support for HTTP/2 is enabled by default for both servers and clients when using HTTPS, bringing the benefits of the new protocol to a wide range of Go projects, such as the popular Caddy web server.

另外值得一提的是 sort 演算法的效能改善:

The algorithm inside sort.Sort was improved to run about 10% faster, but the change may break programs that expect a specific ordering of equal but distinguishable elements.

這應該算還蠻基本常用到的東西,會改善很多程式效能...

Google 的 Load balancer:Seesaw

前幾天因為流感而睡太多,來消化一些文章。

上個星期 Google 放出一套用 Go 寫的 Load balancer,叫 Seesaw:「Seesaw: scalable and robust load balancing」。

比較有趣的是 BGP 與 anycast VIP 的能力:

Seesaw v2 provides full support for anycast VIPs - that is, it will advertise an anycast VIP when it becomes available and will withdraw the anycast VIP if it becomes unavailable.

Google 這個規模玩的是不同 scale 的花樣...