Upgrade to Pro — share decks privately, control downloads, hide ads and more …

うわっ…もしかして私のコー ド、遅すぎ…?

RyuNen344
December 10, 2022

うわっ…もしかして私のコー ド、遅すぎ…?

RyuNen344

December 10, 2022
Tweet

More Decks by RyuNen344

Other Decks in Technology

Transcript

  1. WARNING • JMHࣗମͷઆ໌ͱඃΔݸॴ͕ଟʑ͋Δ • android, iOS, watchOS • ݱঢ়͏·͘ಈ͔͢ํ๏͕ͳ͍ •

    android͸JMHपΓͷ஌ࣝΛ׆͔ͤΔ • Experimentalঢ়ଶͳͷͰWASMʹ͸৮Εͳ͍ • (ࢲͷखݩͰ͸ͦ΋ͦ΋ಈ͔ͤͳ͔ͬͨɾɾɾ)
  2. About JMH(Java Microbenchmark Harness) • Java, JVM޲͚ͷܭଌπʔϧ • 2013೥ΑΓ։ൃ͞ΕͯΔOpenJDKϓϩδΣΫτͷ1෦ •

    Annotation Processor, ASMΛ࢖༻ͯ͠ܭଌ༻ͷίʔυΛੜ੒͢Δ • ੜ੒͞Εͨίʔυ࣮ߦͯ͠ॲཧ࣌ؒΛܭଌ͢Δ • kotlinx-benchmark΋͢΂ͯͷϓϥοτϑΥʔϜʹ͓͍ͯܭଌ༻ͷί ʔυΛੜ੒ͯ͠ܭଌ͢Δ
  3. Gradle Plugin, Moduleͷ௥Ճ • Gradle Plugin • + kotlinx-benchmark gradle

    plugin • + kotlin-all-open gradle plugin(for JVM) • Module • + kotlinx-benchmark-runtime
  4. BenchmarksExtension Deep Dive • warmups(common) • JIT༻ύϥϝʔλ • ࣮ࡍͷܭଌલʹॲཧΛ૸ΒͤͯJITίϯύΠϥʔΛಇ͔ͤΔ •

    JITίϯύΠϥʔʹม׵͞ΕͨίʔυͰϕϯνϚʔΫΛऔΔΑ͏ʹ ͢Δ • (ART͸ಛघ͕ͩJetpack BenchmarkΛ࢖͏ͱΑ͠ͳʹͰ͖Δ)
  5. BenchmarksExtension Deep Dive • nativeFork(native) • ͋ΔܭଌΛಉ͡ϓϩηεͰ࣮ߦ͢Δ͔ผϓϩηεͰ࣮ߦ͢Δ͔Λઃఆ͢Δ • =K/N RuntimeͷಡΈࠐΈ͔Β࢝ΊΔ͔Ͳ͏͔

    • nativeGCAfterIteration(native) • ܭଌޙʹGarbage CollectΛ࣮ߦ͢Δ͔ઃఆ͢Δ • GCͷ࣮ߦ͸ίετ͕ߴ͍ͨΊσϑΥϧτ͸OFFʹͳ͍ͬͯΔ
  6. Tips In Benchmark Code • @BenchmarkΛ෇༩ͨؔ͠਺಺Ͱݺͼग़͢ॲཧ͸ܭଌ͍ͨ͠ॲཧͷΈ ʹͳΕ͹ͳΔ΄Ͳྑ͍(ʹ݁Ռʹ༨ܭͳཁҼ͕ೖΒͳ͍) • 1ͭͷΫϥεɺؔ਺͸1ͭͷ໾ׂΛ୲͏Α͏ʹ͢Δ •

    ΫϥεࣗମΛૄ݁߹ʹͳΔΑ͏ʹઃܭ͢Δ(=CoroutineDispatcher ͳͲ͸injectͰ͖ΔΑ͏ʹ࡞Δɺetc…) ςετΛॻ͖΍͘͢͢ΔͱϕϯνϚʔΫ΋औΓ΍͘͢ͳΔ
  7. Why In Benchmark Code • ίϯύΠϧ࣌఺ͰৗʹฦΓ஋Λ࢖༻͠ͳ͍ίʔυ͸࠷దԽର৅ͱͯ͠ ίʔυ࡟আ͞ΕΔՄೳੑ͕͋Δ • ࠷దԽର৅ʹͳΒͳ͍ίʔυʹ͢Δʹ͸ʁ •

    ܭଌର৅ͷฦΓ஋ΛϕϯνϚʔΫϝιουͷฦΓ஋ʹ͢Δ • ܭଌର৅ϝιουͷฦΓ஋ΛBlackholeʹٵΘͤΔ Blackhole͸࠷దԽର৅ʹͤ͞ͳ͍ͨΊͷIF
  8. Tailrec Optimization • Kotlinͷ࠶ؼ຤ඌΛ࠷దԽ͢Δम০ࢠ • Stack Over fl owͷ৺഑͕ͳ͘ͳΔ •

    ߴ଎Ͱޮ཰తͳLoopϕʔεͷ࣮૷ʹࠩ͠มΘΔ • https://kotlinlang.org/docs/functions.html#tail-recursive- functions w w
  9. ࢀߟࢿྉ • 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