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
800
うわっ…もしかして私のコー ド、遅すぎ…?
RyuNen344
December 10, 2022
Tweet
Share
More Decks by RyuNen344
See All by RyuNen344
サプライチェーン攻撃に備える
ryunen344
0
1.4k
Okioに愛を込めて
ryunen344
3
1.7k
Firebase Hostingを使ってPasskeyをandroidアプリで試してみよう
ryunen344
2
1.4k
Xcode Meets Gradle
ryunen344
0
300
わかった気になる、Coroutine1.7.0のチャンネルのパフォーマンス向上
ryunen344
0
330
K/NとNSKeyedArchiverと私
ryunen344
0
440
ComposeのModalBottomSheetは (まだ)沼
ryunen344
1
1.5k
K/NでCryptoKit使いたかった
ryunen344
0
360
SwiftPM with KMMをprivateなGitHub Releaseで運用する
ryunen344
0
320
Other Decks in Technology
See All in Technology
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
120
TypeScript、上達の瞬間
sadnessojisan
46
13k
Taming you application's environments
salaboy
0
190
Lexical Analysis
shigashiyama
1
150
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
組織成長を加速させるオンボーディングの取り組み
sudoakiy
2
120
The Rise of LLMOps
asei
7
1.5k
複雑なState管理からの脱却
sansantech
PRO
1
140
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
3
200
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Practical Orchestrator
shlominoach
186
10k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Scaling GitHub
holman
458
140k
For a Future-Friendly Web
brad_frost
175
9.4k
4 Signs Your Business is Dying
shpigford
180
21k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
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