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
810
うわっ…もしかして私のコー ド、遅すぎ…?
RyuNen344
December 10, 2022
Tweet
Share
More Decks by RyuNen344
See All by RyuNen344
サプライチェーン攻撃に備える
ryunen344
0
1.6k
Okioに愛を込めて
ryunen344
3
1.8k
Firebase Hostingを使ってPasskeyをandroidアプリで試してみよう
ryunen344
2
1.5k
Xcode Meets Gradle
ryunen344
0
310
わかった気になる、Coroutine1.7.0のチャンネルのパフォーマンス向上
ryunen344
0
330
K/NとNSKeyedArchiverと私
ryunen344
0
450
ComposeのModalBottomSheetは (まだ)沼
ryunen344
1
1.6k
K/NでCryptoKit使いたかった
ryunen344
0
380
SwiftPM with KMMをprivateなGitHub Releaseで運用する
ryunen344
0
340
Other Decks in Technology
See All in Technology
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
280
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
27
12k
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
450
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
330
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
110
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
310
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
180
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
530
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Art, The Web, and Tiny UX
lynnandtonic
298
20k
How to Ace a Technical Interview
jacobian
276
23k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
The Cult of Friendly URLs
andyhume
78
6.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
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