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
740
うわっ…もしかして私のコー ド、遅すぎ…?
RyuNen344
December 10, 2022
Tweet
Share
More Decks by RyuNen344
See All by RyuNen344
Okioに愛を込めて
ryunen344
3
1.5k
Firebase Hostingを使ってPasskeyをandroidアプリで試してみよう
ryunen344
2
1.3k
Xcode Meets Gradle
ryunen344
0
270
わかった気になる、Coroutine1.7.0のチャンネルのパフォーマンス向上
ryunen344
0
300
K/NとNSKeyedArchiverと私
ryunen344
0
420
ComposeのModalBottomSheetは (まだ)沼
ryunen344
1
1.4k
K/NでCryptoKit使いたかった
ryunen344
0
320
SwiftPM with KMMをprivateなGitHub Releaseで運用する
ryunen344
0
300
Kotlin Nativeってandroid向けにもかけるんですよ
ryunen344
0
580
Other Decks in Technology
See All in Technology
MySQLのあらたしいリリースモデル LTSとIR
sakaik
1
120
突撃! 隣のAmazon Bedrockユーザー 〜YouはどうしてAWSで?〜
minorun365
PRO
2
200
[RSJ24] Object Segmentation from Open-Vocabulary Manipulation Instructions Based on Optimal Transport Polygon Matching with Foundation Models
keio_smilab
PRO
0
130
脆弱星に導かれて
nishimunea
1
1.6k
SORACOMで実現するIoTのマルチクラウド対応 - IoTでのクリーンアーキテクチャの実現 -
kenichirokimura
0
280
Namespace, Now and Then
tagomoris
0
170
目標設定と習慣化で今よりも一歩生産性を上げる
sansantech
PRO
7
2.4k
Binary Authorizationと友達になろう / Let's be friends with Binary Authorization
iselegant
2
140
[RSJ24] Object Retrieval in Large-Scale Indoor Environments Using Dense Text with a Multi-Modal Large Language Model
keio_smilab
PRO
0
230
LLMに日本語テキストを学習させる意義
ksaito
13
3.6k
ロボットアームを遠隔制御の話 & LLMをつかったIoTの話もしたい
soracom
PRO
1
210
Building Static Websites with Sculpin
opdavies
0
1.5k
Featured
See All Featured
Thoughts on Productivity
jonyablonski
66
4.2k
The Invisible Side of Design
smashingmag
295
50k
What's new in Ruby 2.0
geeforr
340
31k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
22
1.7k
The Cost Of JavaScript in 2023
addyosmani
40
5.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
A Modern Web Designer's Workflow
chriscoyier
690
190k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
27
8.9k
Producing Creativity
orderedlist
PRO
340
39k
Agile that works and the tools we love
rasmusluckow
327
20k
A Tale of Four Properties
chriscoyier
155
22k
Why You Should Never Use an ORM
jnunemaker
PRO
53
8.9k
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