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
AGIについてChatGPTに聞いてみた
blueb
0
130
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
250
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
120
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.2k
いざ、BSC討伐の旅
nikinusu
2
780
ハイパーパラメータチューニングって何をしているの
toridori_dev
0
140
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
A designer walks into a library…
pauljervisheath
203
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Typedesign – Prime Four
hannesfritz
40
2.4k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
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