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
270
鉄は熱いうちに打て - Kaigi Effect LT大会
nasa_desu
2
460
RustでOS開発はじめの一歩
nasa_desu
8
7.3k
ログから学ぶgo build
nasa_desu
4
1.2k
goのメモリアロケーターの話
nasa_desu
1
660
GoとRust - 並行処理編
nasa_desu
5
4k
Other Decks in Technology
See All in Technology
データベースで見る『家族アルバム みてね』の変遷 / The Evolution of Family Album Through the Lens of Databases
kohbis
4
1.1k
AIで進化するソフトウェアテスト:mablの最新生成AI機能でQAを加速!
mfunaki
0
110
古き良き Laravel のシステムは関数型スタイルでリファクタできるのか
leveragestech
1
630
”知のインストール”戦略:テキスト資産をAIの文脈理解に活かす
kworkdev
PRO
9
4.1k
Webアプリを Lambdaで動かすまでに考えること / How to implement monolithic Lambda Web Application
_kensh
7
1.2k
20250413_湘南kaggler会_音声認識で使うのってメルス・・・なんだっけ?
sugupoko
1
340
NLP2025 参加報告会 / NLP2025
sansan_randd
4
500
MCPを活用した検索システムの作り方/How to implement search systems with MCP #catalks
quiver
1
500
試験は暗記より理解 〜効果的な試験勉強とその後への活かし方〜
fukazawashun
0
340
ウォンテッドリーにおける Platform Engineering
bgpat
0
180
LangfuseでAIエージェントの 可観測性を高めよう!/Enhancing AI Agent Observability with Langfuse!
jnymyk
0
170
Cursor AgentによるパーソナルAIアシスタント育成入門―業務のプロンプト化・MCPの活用
os1ma
7
1.8k
Featured
See All Featured
A designer walks into a library…
pauljervisheath
205
24k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Code Review Best Practice
trishagee
67
18k
The Pragmatic Product Professional
lauravandoore
33
6.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
5
520
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
104
19k
Java REST API Framework Comparison - PWX 2021
mraible
30
8.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
30k
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.