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
うわっ…もしかして私のコー ド、遅すぎ…?
Search
RyuNen344
December 10, 2022
Technology
1
830
うわっ…もしかして私のコー ド、遅すぎ…?
RyuNen344
December 10, 2022
Tweet
Share
More Decks by RyuNen344
See All by RyuNen344
サプライチェーン攻撃に備える
ryunen344
0
1.7k
Okioに愛を込めて
ryunen344
3
1.9k
Firebase Hostingを使ってPasskeyをandroidアプリで試してみよう
ryunen344
2
1.5k
Xcode Meets Gradle
ryunen344
0
320
わかった気になる、Coroutine1.7.0のチャンネルのパフォーマンス向上
ryunen344
0
340
K/NとNSKeyedArchiverと私
ryunen344
0
470
ComposeのModalBottomSheetは (まだ)沼
ryunen344
1
1.6k
K/NでCryptoKit使いたかった
ryunen344
0
390
SwiftPM with KMMをprivateなGitHub Releaseで運用する
ryunen344
0
350
Other Decks in Technology
See All in Technology
カップ麺の待ち時間(3分)でわかるPartyRockアップデート
ryutakondo
0
140
My small contributions - Fujiwara Tech Conference 2025
ijin
0
1.5k
タイミーのデータ活用を支えるdbt Cloud導入とこれから
ttccddtoki
1
260
駆け出しリーダーとしての第一歩〜開発チームとの新しい関わり方〜 / Beginning Journey as Team Leader
kaonavi
0
130
2024AWSで個人的にアツかったアップデート
nagisa53
1
110
.NET 最新アップデート ~ AI とクラウド時代のアプリモダナイゼーション
chack411
0
200
今から、 今だからこそ始める Terraform で Azure 管理 / Managing Azure with Terraform: The Perfect Time to Start
nnstt1
0
240
CDKのコードレビューを楽にするパッケージcdk-mentorを作ってみた/cdk-mentor
tomoki10
0
210
Copilotの力を実感!3ヶ月間の生成AI研修の試行錯誤&成功事例をご紹介。果たして得たものとは・・?
ktc_shiori
0
360
月間60万ユーザーを抱える 個人開発サービス「Walica」の 技術スタック変遷
miyachin
1
150
あなたの人生も変わるかも?AWS認定2つで始まったウソみたいな話
iwamot
3
860
「隙間家具OSS」に至る道/Fujiwara Tech Conference 2025
fujiwara3
7
6.5k
Featured
See All Featured
Building Your Own Lightsaber
phodgson
104
6.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Faster Mobile Websites
deanohume
305
30k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
The World Runs on Bad Software
bkeepers
PRO
66
11k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
3
180
Site-Speed That Sticks
csswizardry
3
270
Practical Orchestrator
shlominoach
186
10k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Bash Introduction
62gerente
610
210k
Transcript
͏Θͬ…͔ͯ͠͠ࢲͷίʔ υɺ͗͢…ʁ 2022.12.10 Kotlin Fest RyuNen344/Bunjiro Miyoshi
About Me • teamLab.Incॴଐ • ීஈandroidͱKMMϝΠϯ • DroidKaigiΞϓϦͷόοΫΤϯυͬͯ·͢ • લճ։࠵(2019)࣌ʹKotlinΛॻ͍ͯ͢Β͍ͳ͔ͬͨ
͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ૣͰ͕͢
ͳΜ͔͜ͷॲཧɺ͍ͳɾɾɾɾ ͳͥɾɾɾɾɾɾ
JavaͷStreamͱKotlinͷSequence ͲͬͪΛ͍͍͑ͷɾɾɾʁ
ॲཧվળͰ͖ͨɾɾɾɾɾ ͣʂʂ(ຊ͔ɾɾʁʁ)
ܰʹܭଌ͠·͠ΐ͏ w w w w
Outline • kotlinx-benchmarkͱ • JMH(Java Microbenchmark Harness)ͱ • ΦʔιυοΫεͳ͍ํ •
ύϥϝʔλͱམͱ݀͠ • ϓϥοτϑΥʔϜ͝ͱͷҧ͍
WARNING
WARNING • JMHࣗମͷઆ໌ͱඃΔݸॴ͕ଟʑ͋Δ • android, iOS, watchOS • ݱঢ়͏·͘ಈ͔͢ํ๏͕ͳ͍ •
androidJMHपΓͷࣝΛ׆͔ͤΔ • Experimentalঢ়ଶͳͷͰWASMʹ৮Εͳ͍ • (ࢲͷखݩͰͦͦಈ͔ͤͳ͔ͬͨɾɾɾ)
ຊฤ
About kotlinx-benchmark • JetBrains͕։ൃ͍ͯ͠ΔMPPରԠͷϕϯνϚʔΫϥΠϒϥϦ • JMHͷIFΛ౿ऻͯͭ͘͠ΒΕ͍ͯΔ • PlatformʹΑͬͯJMHʹ͋Δػೳ͕͑ͳ͍ύλʔϯ͕͋Δ • Scopeͷछྨ͕͘͢ͳ͔ͬͨΓɾɾɾɾModeͷछྨ͕͘͢ͳ͔ͬ
ͨΓɾɾɾ
About JMH(Java Microbenchmark Harness) • Java, JVM͚ͷܭଌπʔϧ • 2013ΑΓ։ൃ͞ΕͯΔOpenJDKϓϩδΣΫτͷ1෦ •
Annotation Processor, ASMΛ༻ͯ͠ܭଌ༻ͷίʔυΛੜ͢Δ • ੜ͞Εͨίʔυ࣮ߦͯ͠ॲཧ࣌ؒΛܭଌ͢Δ • kotlinx-benchmarkͯ͢ͷϓϥοτϑΥʔϜʹ͓͍ͯܭଌ༻ͷί ʔυΛੜͯ͠ܭଌ͢Δ
How To Use
Gradle Plugin, ModuleͷՃ • Gradle Plugin • + kotlinx-benchmark gradle
plugin • + kotlin-all-open gradle plugin(for JVM) • Module • + kotlinx-benchmark-runtime
Gradle Plugin, ModuleͷՃ
Gradle Plugin, ModuleͷՃ benchmarkͱallopenͷgradle pluginΛՃ
Gradle Plugin, ModuleͷՃ JMHbenchmark codeΛoverrideͯ͠ܭଌίʔυΛੜ͢Δ kotlinͷclassσϑΥϧτͩͱ fi nalʹͳΔͷͰall-openΛ͋ͯΔ
Gradle Plugin, ModuleͷՃ
Gradle Plugin, ModuleͷՃ targetͷՃ
Gradle Plugin, ModuleͷՃ common SourceSetʹruntime libraryΛՃ͢Δ
KMM͚ͷۤͷࡦ • benchmarkܭଌ༻ίʔυΛੜ͠ɺ࣮ߦ͢Δ͜ͱͰܭଌ͢Δ • 0.4.6࣌Ͱandroid, iOSͳͲͷPlatformͰ࣮ߦ͢Δ࣮͕ߦΘΕͯ ͓Βͣಈ͔ͤͳ͍ • ͋Μ·ΓʹͳͷͰͤΊͯPure Kotlinͳίʔυ͚ͩͰܭଌͰ͖ΔΑ
͏ʹ͢ΔͱJVMͱNativeͰͷڍಈࠩΛ࣮ࡍʹνΣοΫ͢Δ͜ͱ͕Ͱ͖ Δ
KMM͚ͷۤͷࡦ
KMM͚ͷۤͷࡦ ඇandroidίʔυʹͷΈґଘ͢ΔJVMͷSourceSetΛ࡞͢Δ androidίʔυΛJVMͷSourceSetʹґଘͤ͞Δ
KMM͚ͷۤͷࡦ ྆ํͰ༻͢ΔSourceSetsΛ࡞͢Δ macOS, iOSΛ࡞ͨ͠SourceSetsʹґଘͤ͞Δ
Settings of BenchmarksExtension ઃఆΛม͑ͨϓϦηοτΛ࡞Ͱ͖Δ
Settings of BenchmarksExtension ରͷSourceSetsΛొ͢Δ
Impl of Benchmark Code
Impl of Benchmark Code @StateΛ༩ͯ͠ϕϯνϚʔΫΫϥε Ͱ͋Δ͜ͱΛίϯύΠϥʔʹ͑Δ
Impl of Benchmark Code ύϥϝʔλͷ४උͱϦηοτΛߦ͏ؔʹ@Setupɺ @TearDownΛ༩͢Δ
Impl of Benchmark Code ܭଌ͢ΔίʔυΛݺͼग़͢ϝιουʹ @BenchmarkΛ༩͢Δ
݁Ռ(txt)ͷ֬ೝ ଞʹjson, csv, scsvͷϑΥʔϚοτͰॻ͖ग़ͤΔ
Parameters And Traps
BenchmarksExtension Deep Dive • warmups(common) • JIT༻ύϥϝʔλ
BenchmarksExtension Deep Dive • warmups(common) • JIT༻ύϥϝʔλ JITͷઆ໌͍ͤͯͩ͘͞͞
Just In Time Compiler
Just In Time Compiler JIT͡Όͳ͍߹ Interpreter͕classϑΝΠϧΛ ஞ࣍ಡΈࠐΈɺػցޠʹม͠ ͳ͕Β࣮ߦ͢Δ
Just In Time Compiler JITͷ߹ ઌʹclassϑΝΠϧΛػցޠʹ ม͓ͯ͘͠
Just In Time Compiler Կݺͼग़͞ΕΔॲཧઌʹ Native CodeʹCompile͞ΕΔ
BenchmarksExtension Deep Dive • warmups(common) • JIT༻ύϥϝʔλ • ࣮ࡍͷܭଌલʹॲཧΛΒͤͯJITίϯύΠϥʔΛಇ͔ͤΔ •
JITίϯύΠϥʔʹม͞ΕͨίʔυͰϕϯνϚʔΫΛऔΔΑ͏ʹ ͢Δ • (ARTಛघ͕ͩJetpack BenchmarkΛ͏ͱΑ͠ͳʹͰ͖Δ)
BenchmarksExtension Deep Dive • nativeFork(native) • ͋ΔܭଌΛಉ͡ϓϩηεͰ࣮ߦ͢Δ͔ผϓϩηεͰ࣮ߦ͢Δ͔Λઃఆ͢Δ • =K/N RuntimeͷಡΈࠐΈ͔Β࢝ΊΔ͔Ͳ͏͔
• nativeGCAfterIteration(native) • ܭଌޙʹGarbage CollectΛ࣮ߦ͢Δ͔ઃఆ͢Δ • GCͷ࣮ߦίετ͕ߴ͍ͨΊσϑΥϧτOFFʹͳ͍ͬͯΔ
Tips In Benchmark Code
Tips In Benchmark Code ࣅͨΑ͏ͳߏΛݟͨ͜ͱ͋Γ·ͤΜ͔ʁ
Tips In Benchmark Code ࣅͨΑ͏ͳߏΛݟͨ͜ͱ͋Γ·ͤΜ͔ʁ JUnitͷςετίʔυʹࣅ͍ͯΔʂʂ
Tips In Benchmark Code • JUnitͷςετίʔυʹࣅ͍ͯΔߏ • ςετΛ࣮͢ΔࡍͷTips͕׆͔ͤΔ
Tips In Benchmark Code • @BenchmarkΛ༩ͨؔ͠Ͱݺͼग़͢ॲཧܭଌ͍ͨ͠ॲཧͷΈ ʹͳΕͳΔ΄Ͳྑ͍(ʹ݁Ռʹ༨ܭͳཁҼ͕ೖΒͳ͍) • 1ͭͷΫϥεɺؔ1ͭͷׂΛ୲͏Α͏ʹ͢Δ •
ΫϥεࣗମΛૄ݁߹ʹͳΔΑ͏ʹઃܭ͢Δ(=CoroutineDispatcher ͳͲinjectͰ͖ΔΑ͏ʹ࡞Δɺetc…) ςετΛॻ͖͘͢͢ΔͱϕϯνϚʔΫऔΓ͘͢ͳΔ
Why In Benchmark Code
Why In Benchmark Code varͰએݴ͍ͯ͠ΔͷɺؾʹͳΓ·͢ΑͶʁ
Why In Benchmark Code • ίϯύΠϧ࣌ͰৗʹݻఆʹͳΔίʔυ࠷దԽରͱͯ͠ίʔυ লུ͞ΕΔՄೳੑ͕͋Δ • ࠷దԽରʹͳΒͳ͍ίʔυʹ͢Δʹʁ •
ܭଌରίʔυʹ͢มvarͰ࣋ͬͯ@SetupͰઃఆ͢Δ
Why In Benchmark Code ܭଌରͷฦΓΛBlackholeʹٵ Θ͍ͤͯΔͷؾʹͳΓ·͢ΑͶʁ
Why In Benchmark Code • ίϯύΠϧ࣌ͰৗʹฦΓΛ༻͠ͳ͍ίʔυ࠷దԽରͱͯ͠ ίʔυআ͞ΕΔՄೳੑ͕͋Δ • ࠷దԽରʹͳΒͳ͍ίʔυʹ͢Δʹʁ •
ܭଌରͷฦΓΛϕϯνϚʔΫϝιουͷฦΓʹ͢Δ • ܭଌରϝιουͷฦΓΛBlackholeʹٵΘͤΔ Blackhole࠷దԽରʹͤ͞ͳ͍ͨΊͷIF
Differences In Each Platform
JVM • JMHͷAlias͕ுΒΕ͍ͯΔ • JVMʹݶͬͯݴ͑JMHͷػೳϑϧͰ͑Δ
Native • ࣌ؒΛΧϯτ͢ΔίʔυΛKotlin/Native(C++)Ͱ࣮͍ͯ͠Δ
Native kotlinx.benchmark.native.NativeExecutor
Native K/NͰ࣌ؒपΓͷܭࢉΛ࣮͍ͯ͠Δ ਂງΓ͢ΔͱcppίʔυʹͨͲΓண͘
Native • ࣌ؒΛΧϯτ͢ΔίʔυΛKotlin/Native(C++)Ͱ࣮͍ͯ͠Δ • ReleaseϏϧυͰܭଌ͢ΔΑ͏ʹλεΫ͕࡞ΒΕΔͷͰσόοάγϯϘϧ ʹΑΔύϑΥʔϚϯεྼԽؾʹ͠ͳͯ͘Α͍ • nativeGCAfterIterationΛtrueʹ͢ΔҎ֎ͰGarbage CollectΛΩοΫ͠ ͍ͯΔՕॴͳ͍ͷͰ͏ࡍϝϞϦʔώʔϓʹؾΛ͏ํ͕Αͦ͞͏
• Blackholeͷ࣮pinϝιουΛݺͼग़͍ͯ͠Δ
JS • Benchmark.js͕෦Ͱಈ͍͍ͯΔ • ࣌ؒͷܭଌؚΊͯBenchmark.jsͰߦ͍ͬͯΔ • Blackholeͷ࣮nopeʹͳ͍ͬͯΔ • ݱঢ়JSͰBlackhole͕ਖ਼͘͠ಈ࡞͠ͳ͍ͷͰҙ͕ඞཁ
Ex.) Language Optimization
Tailrec Optimization • Kotlinͷ࠶ؼඌΛ࠷దԽ͢Δम০ࢠ • Stack Over fl owͷ৺͕ͳ͘ͳΔ •
ߴͰޮతͳLoopϕʔεͷ࣮ʹࠩ͠มΘΔ • https://kotlinlang.org/docs/functions.html#tail-recursive- functions w w
ͲΕ͙Β͍ߴͳΜͩΖ͏͔ʁ
Tailrec Optimization tailrec͋Γɾͳ͠ඌ࠶ؼͷϑΟϘφονؔ
Tailrec Optimization Decompileίʔυ
Tailrec OptimizationɿJVM tailrecΛ༩͢Δͱ͕ؔ2ഒҎ্͘ಈ࡞ͨ͠ Stack Over fl owͰॲཧ͕ࣦഊ͢Δ͜ͱͳ͔ͬͨ
Tailrec OptimizationɿNative JVM΄Ͳͷ༏Ґ͕ࠩ͋Δ݁ՌΛಘΒΕͳ͔͕ͬͨ ޮՌ͋Γͦ͏
Tailrec OptimizationɿNative • ผ్ ͷඌ࠶ؼΛ࡞ͯͨ͠Ίͨ͠ͱ͜ΖɺtailrecΛ༩ͨؔ͠ ͕1.25ഒ΄Ͳ͘ͳͬͨ • Ծઆ1ɿϧʔϓͩΖ͏͕࠶ؼͩΖ͏͕native͔ͩΒΊͪΌͪ͘Όૣ͍ʁ • Ծઆ2ɿLLVMͰͷcompile࣌ʹ࠷దԽ͕͔͔͍ͬͯΔ͔͠Εͳ
͍ʁLLVMͰͷ࠷దԽ͕ޮ͘ɾޮ͔ͳ͍͕͋Δͷ͔ɾɾɾɾ n ∑ k=1 k
Tailrec OptimizationɿJS JVM΄Ͳͷ༏Ґࠩͳ͍͕ޮՌൈ܈ Stack Over fl owͰॲཧ͕ࣦഊ͢Δ͜ͱͳ͔ͬͨ
Tailrec OptimizationɿJS • ผ్ ͷඌ࠶ؼΛࢼͨ͠ͱ͜ΖɺJVMNativeͷΑ͏ʹtailrecΛ ༩ͨ͠΄͏͕໌Β͔ʹ͘ͳΔͱ͍͏͜ͱͳ͔ͬͨ • ԾઆɿJSNumberͷൣғ͕JVMɺNativeͱҟͳΔͷͰͦͷลΓ͕Ө ڹ͍ͯ͠Δ͔ʁ n
∑ k=1 k kotlinx-benchmarkͷ͓ӄͰ͓खܰʹௐࠪͰ͖ͨ
·ͱΊ • kotlinx-benchmarkΛ༻͢ΔͱखܰʹMPPͰϕϯνϚʔΫΛͱΔ͜ͱ ͕Ͱ͖Δ • ݱঢ়android, iOSͰ͑ͳ͍(TaskΛ࡞Εߦ͚Δͣ) • ςετ͕ॻ͖͍࣮͢Λ͓ͯ͘͠ͱϕϯνϚʔΫ࣮͘͢͠ͳΔ •
JVMͰಛʹίϯύΠϥʔʹΑΔ࠷దԽJITΛҙࣝͨ͠ύϥϝʔλઃ ఆͱ࣮͕ඞཁ
͝ࢹௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ ܰʹܭଌ͠·͠ΐ͏ w w w w
ࢀߟࢿྉ • kotlin/kotlinx-benchmark • https://github.com/Kotlin/kotlinx-benchmark • openjdk/jmh • https://github.com/openjdk/jmh •
Benchmark.js • https://benchmarkjs.com/ • tailrec • https://kotlinlang.org/docs/functions.html#tail-recursive-functions