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
エンジニア主導の企画立案を可能にする組織とは?
recruitengineers
PRO
1
350
「頑張る」を「楽しむ」に変換する技術
tomoyakitaura
8
1.6k
リクルートのエンジニア組織を下支えする 新卒の育成の仕組み
recruitengineers
PRO
2
210
20250309 無冠のわたし これからどう先生きのこれる?
akiko_pusu
9
1.6k
Ruby on Railsで持続可能な開発を行うために取り組んでいること
am1157154
3
190
20250304_赤煉瓦倉庫_DeepSeek_Deep_Dive
hiouchiy
2
150
OCI IAM Identity Domains Entra IDとの認証連携設定手順 / Identity Domain Federation settings with Entra ID
oracle4engineer
PRO
1
1.3k
IAMのマニアックな話2025
nrinetcom
PRO
6
1.6k
貧民的プログラミングのすすめ
kakehashi
PRO
2
310
AWSアカウントのセキュリティ自動化、どこまで進める? 最適な設計と実践ポイント
yuobayashi
7
2k
AI自体のOps 〜LLMアプリの運用、AWSサービスとOSSの使い分け〜
minorun365
PRO
10
1.3k
あなたが人生で成功するための5つの普遍的法則 #jawsug #jawsdays2025 / 20250301 HEROZ
yoshidashingo
2
490
Featured
See All Featured
Speed Design
sergeychernyshev
28
820
Become a Pro
speakerdeck
PRO
26
5.2k
Embracing the Ebb and Flow
colly
84
4.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
28
1.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.6k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
It's Worth the Effort
3n
184
28k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
4 Signs Your Business is Dying
shpigford
183
22k
Music & Morning Musume
bryan
46
6.4k
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.