Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
リンカを変えてgo buildを 速く出来るか
Search
nasa
January 30, 2024
Technology
2
3.4k
リンカを変えてgo buildを 速く出来るか
hatena.go 5分LTの発表資料
https://hatena.connpass.com/event/307931/
nasa
January 30, 2024
Tweet
Share
More Decks by nasa
See All by nasa
難解な自己紹介プログラムを書く
nasa_desu
1
260
鉄は熱いうちに打て - Kaigi Effect LT大会
nasa_desu
2
440
RustでOS開発はじめの一歩
nasa_desu
8
7.3k
ログから学ぶgo build
nasa_desu
4
1.2k
goのメモリアロケーターの話
nasa_desu
1
640
GoとRust - 並行処理編
nasa_desu
5
4k
Other Decks in Technology
See All in Technology
Cracking the Coding Interview 6th Edition
gdplabs
14
28k
データマネジメントのトレードオフに立ち向かう
ikkimiyazaki
6
1.2k
偏光画像処理ライブラリを作った話
elerac
1
160
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
18k
依存パッケージの更新はコツコツが勝つコツ! / phpcon_nagoya2025
blue_goheimochi
3
200
いまからでも遅くない!コンテナでWebアプリを動かしてみよう!コンテナハンズオン編
nomu
0
130
ソフトウェアエンジニアと仕事するときに知っておいたほうが良いこと / Key points for working with software engineers
pinkumohikan
1
140
JavaにおけるNull非許容性
skrb
2
2.5k
データベースの負荷を紐解く/untangle-the-database-load
emiki
1
440
データエンジニアリング領域におけるDuckDBのユースケース
chanyou0311
8
2.1k
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
0
200
AWS Well-Architected Frameworkで学ぶAmazon ECSのセキュリティ対策
umekou
2
130
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
640
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
114
50k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
GitHub's CSS Performance
jonrohan
1030
460k
Faster Mobile Websites
deanohume
306
31k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Fireside Chat
paigeccino
34
3.2k
Optimizing for Happiness
mojombo
376
70k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
It's Worth the Effort
3n
184
28k
Transcript
リンカを変えてgo buildを 速く出来るか hatena.go #1 Jan. 31 2024 - nasa
© 2024 Wantedly, Inc.
今日話すこと • 速いリンカに切り替えるモチベーション • リンカとは • moldの紹介 • 任意のリンカをgolangのビルドに利用する方法 •
golangのリンカを高速リンカmoldに変更してみた結果 © 2024 Wantedly, Inc.
モチベーション なぜより速いリンカが欲しいのか • コンパイル時間を減らしたいから • goは差分コンパイル • なのですべてが再コンパイルされるわけじゃないがリンクは毎回実行される • リンク時間が短くなれば試行回数が増やせる
© 2024 Wantedly, Inc.
リンカとは プログラムの断片をつなぎ合わせ実行ファイルを作る © 2024 Wantedly, Inc. 引用元: https://speakerdeck.com/nasa_desu/rogukaraxue-bugo-build
moldとは © 2024 Wantedly, Inc. Rui Ueyama さんが開発してる高速リンカ 引用元: https://github.com/rui314/mold/blob/main/docs/comparison.png
任意のリンカを利用する © 2024 Wantedly, Inc.
内部リンカ・外部リンカ • 内部リンカ、外部リンカのどちらかでリンクされる • 内部リンカ ◦ golang用にスクラッチで実装されたものが内部リンカ • 外部リンカ ◦
clang, GCCなど © 2024 Wantedly, Inc.
内部リンカ・外部リンカ • なぜ2つを使い分けるのか ◦ internal linkerだけではリンクできない状況が存在する ◦ 具体的な条件は分からなかった。。。 ◦ 詳しい人が居たら教えてください
◦ CGOが絡むと外部リンカを使っているっぽい © 2024 Wantedly, Inc.
任意の外部リンカを使う © 2024 Wantedly, Inc. • 右図が全体像 ◦ 1. go
build ◦ 2. go tool link ◦ 3. external linker(frontend) ◦ 4. linker
任意の外部リンカを使う © 2024 Wantedly, Inc. • go buildからgo tool linkを呼んでる
• `ldflags`オプションでlinkコマンドのフラグ指定 • linkmodeでリンカ内部リンカ・外部リンカのどちらを使 用するか決める $ go build -ldflags="-linkmode=external
任意の外部リンカを使う © 2024 Wantedly, Inc. • extldflagsでコンパイラで外部リンカに任意のフラグを 渡せる • gccでは`-fuse-ld`でリンカを指定できる
$ go build -ldflags="-linkmode=external -extldflags=-fuse-ld=mold -extld=gcc"
任意の外部リンカを使う © 2024 Wantedly, Inc. go build → go tool
link → gcc → 任意のリンカという依存関係
どのリンカが速いのか計測 © 2024 Wantedly, Inc.
環境 © 2024 Wantedly, Inc. • 実行環境 ◦ AWS EC2
◦ インスタンスタイプ: c5.xlarge ◦ CPU: 4 ◦ Memory: 8GiB
環境 © 2024 Wantedly, Inc. • versions ◦ go1.21.4 linux/amd64
◦ gcc 11.4.0 ◦ lld 14.0.0 ◦ gold 1.16 ◦ mold 2.4.0
環境 © 2024 Wantedly, Inc. • 計測方法 ◦ go build
-x -workで必要なファイルを生成 + linkコマンドを把握 ◦ -x: go tool linkコマンドがログに出るのでメモる ◦ -work: 中間結果を保存する • /usr/local/go/pkg/tool/linux_amd64/linkを実行 • この時間を計測した(10回の平均)
計測結果 internal gold lld mold hello world 179ms 274ms 370ms
271ms k9s 4.5s 7.3s 5.5s 5.4s jujud 11.6s 17.4s 14.3s 14.9s © 2024 Wantedly, Inc. ※ k8sの誤字ではないです
計測結果 internal linkerが一番速い 代替のリンカはまだ無いみたい © 2024 Wantedly, Inc.
計測結果 internal lld mold hello world 179ms 370ms 271ms k9s
4.5s 5.5s 5.4s jujud 11.6s 14.3s 14.9s © 2024 Wantedly, Inc. ところでinternalとmoldで2,4秒もの差が出るんだろう?
続・リンカを変えてgo buildを速 く出来るか © 2024 Wantedly, Inc.
whoami © 2024 Wantedly, Inc. nasa (Asan Kondo) • Wantedly,
Inc. (2021-04-) ◦ 推薦基盤チーム ◦ Backend中心にあれこれやってる • ハンドルネームnasaを追い求める民 ◦ X(Twitter): @nasa_desu ◦ GitHub: @k-nasa
we are めっちゃ hiring © 2024 Wantedly, Inc.