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
OCI Success Journey OCIの何が評価されてる?疑問に答える事例セミナー(2025年2月実施)
oracle4engineer
PRO
2
150
技術スタックだけじゃない、業務ドメイン知識のオンボーディングも同じくらいの量が必要な話
niftycorp
PRO
0
100
ABWG2024採択者が語るエンジニアとしての自分自身の見つけ方〜発信して、つながって、世界を広げていく〜
maimyyym
1
160
AWSを活用したIoTにおけるセキュリティ対策のご紹介
kwskyk
0
350
Apache Iceberg Case Study in LY Corporation
lycorptech_jp
PRO
0
320
システム・ML活用を広げるdbtのデータモデリング / Expanding System & ML Use with dbt Modeling
i125
1
320
AIエージェント入門
minorun365
PRO
31
18k
いまからでも遅くない!コンテナでWebアプリを動かしてみよう!コンテナハンズオン編
nomu
0
150
Iceberg Meetup Japan #1 : Iceberg and Databricks
databricksjapan
0
360
Autonomous Database Serverless 技術詳細 / adb-s_technical_detail_jp
oracle4engineer
PRO
17
45k
"TEAM"を導入したら最高のエンジニア"Team"を実現できた / Deploying "TEAM" and Building the Best Engineering "Team"
yuj1osm
1
160
MIMEと文字コードの闇
hirachan
2
1.4k
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
GraphQLとの向き合い方2022年版
quramy
44
14k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Designing for humans not robots
tammielis
250
25k
Embracing the Ebb and Flow
colly
84
4.6k
We Have a Design System, Now What?
morganepeng
51
7.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
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.