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.5k
リンカを変えて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
450
RustでOS開発はじめの一歩
nasa_desu
8
7.3k
ログから学ぶgo build
nasa_desu
4
1.2k
goのメモリアロケーターの話
nasa_desu
1
650
GoとRust - 並行処理編
nasa_desu
5
4k
Other Decks in Technology
See All in Technology
AWSサービスアップデート2025年2月分
yuobayashi
2
120
エンジニア採用と 技術広報の実践/acaricsummit2025
nishiuma
1
190
バックエンドエンジニアによるフロントエンドテスト拡充の具体的手法
kinosuke01
1
180
AWS のポリシー言語 Cedar を活用した高速かつスケーラブルな認可技術の探求 #phperkaigi / PHPerKaigi 2025
ytaka23
3
600
S3成長記録 in 2024 - オレたちのS3はどこに向かうのか?- @Storage-JAWS#7
p0n
1
100
neoAI_千葉講演資料_250311_配布用.pdf
iotcomjpadmin
0
230
組織のスケールを見据えたプロジェクトリードエンジニア制度の実践 / Project Lead Engineer for Scaling Engineering Organization
ohbarye
11
2.7k
30代エンジニアが考える、エンジニア生存戦略~~セキュリティを添えて~~
masakiokuda
2
360
「backlog-exporter」とAIの連携による業務効率化
shuntatoda
0
370
Autonomous Database - Dedicated 技術詳細 / adb-d_technical_detail_jp
oracle4engineer
PRO
4
8.1k
【ServiceNow SNUG Meetup LT deck】ServiceNow「検索性の進化」ZingからNow Assistまで
niwato
0
180
痒い所に手が届く!要約モデルのつくり方
sakusakumura
1
260
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Building Applications with DynamoDB
mza
93
6.3k
Producing Creativity
orderedlist
PRO
344
40k
Being A Developer After 40
akosma
89
590k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Docker and Python
trallard
44
3.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
420
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.