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

実行時におけるJVMバイトコード最適化手法

 実行時におけるJVMバイトコード最適化手法

todesking

March 26, 2016
Tweet

More Decks by todesking

Other Decks in Technology

Transcript

  1. "SSPXʹΑΔදݱ + z-1 val z_1 = init(0.0) val plus =

    arr { case (a, b) => a + b } val out = in >>> (z_1 &&& id) >>> plus
  2. "SSPXʹΑΔදݱ + z-1 val z_1 = init(0.0) val plus =

    arr { case (a, b) => a + b } val out = in >>> (z_1 &&& id) >>> (plus *** (arr(_._1) >>> z_1)) >>> plus + z-1 (´ŋ_ŋ`)???
  3. "SSPXʹΑΔදݱ XJUITZOUBYTVHBS + z-1 val z_1 = init(0.0) val plus

    = arr { case (a, b) => a + b } val out = for { a <- z_1 -< in b <- plus -< (in, a) c <- z_1 -< a d <- plus -< (b, c) } yield d + z-1 ʋ(´ʔʆ)ϊಡΊΔಡΊΔ a b c d in
  4. 4DBMBίϯύΠϥʹΑΔ ࠷దԽ w ಛघԽ w ܕύϥϝʔλʹ@specializedΞϊςʔγϣϯΛ͚ͭΔ͜ͱͰࢦఆ w ܕύϥϝʔλ͕ϓϦϛςΟϒܕͷ৔߹ʹରͯ͠ɺઐ༻ͷϝιουΛ ੜ੒͢Δ͜ͱͰCPYJOHΛෆཁʹ w

    ૊Έ߹Θͤͷ਺͚ͩϝιουΫϥε͕૿͑Δͱ͍͏σϝϦοτ͋Γ w ૊Έ߹Θͤ਺ΛݮΒ͢NJOJCPYJOHͱ͍͏ख๏΋͋Δ ͨͩ͠4DBMB ຊՈʹ͸ೖͬͯͳ͠ trait Function1[ @specialized(scala.Int, scala.Long, scala.Float, scala.Double) -T1, @specialized(scala.Unit, scala.Boolean, scala.Int, scala.Float, scala.Long, scala.Double) +R ] extends AnyRef { self => ྫ: scala.Function1ͷఆٛ
  5. 4DBMBίϯύΠϥʹΑΔ ࠷దԽ w &MJNJOBUFOPOFTDBQJOHCPYFT UVQMFT BOESFGT w ίϯύΠϧ࣌ʹɺϝιου಺Ͱ׬͍݁ͯ͠Δ CPYJOHUVQMJOHΛআڈ͢Δ w

    ͰೖΔΒ͍͠Ͱ͢ w ϝιουΛ·͍ͨͩ࠷దԽͰ͸ͳ͍ͷͰɺޮՌ͸͔ ͳΓݶఆతͰ͸ͱ͍͏ؾ͕͠·͢
  6. +7.ʹΑΔ࠷దԽ w *OMJOJOH w Α͘ݺͼग़͞ΕΔখ͍͞ϝιουΛ࣮ߦ࣌ʹΠϯ ϥΠϯల։͢Δ w ݺͼग़͞ΕΔϝιουͷީิ͕ݶΒΕ͍ͯΔ৔߹ɺ Ծ૝ؔ਺ݺͼग़͠ΛܕνΣοΫ ௚઀ݺͼग़͠ʹม

    ׵ w +*5ίϯύΠϥͷৄࡉʹؔ͢Δࢿྉ͸๡͍͠ͷͰɺ ΘΓͱṖʹแ·Ε͍ͯ·͢ 0QFO+%,ͷίʔυΛ ͕Μ͹ͬͯಡΊ͹͍͍ΜͰ͕͢ʜʜ
  7. ؔ਺߹੒ͷ ύϑΥʔϚϯε w YTMPXFS standardF ௨ৗͷؔ਺߹੒ 4515.900 ± 154.250 baseline

    ߹੒ͤͣ௚઀ݺͿ 21772.108 ± 154.944 val f1: Int => Int = { x => x + 1 } val f2: Int => Double = { x => x + 10.0 } val f3: Double => Int = { x => (x * 100).toInt } val f4: Int => Double = { x => x + 1.5 } val f5: Double => Double = { x => x * 0.01 } val f6: Double => Double = { x => x - 200.0 } val f7: Double => Int = { x => x.toInt } val f8: Int => Int = { x => x + 10 } ! val baseline = { def F(x: Int) = f8(f7(f6(f5(f4(f3(f2(f1(x)))))))) x: Int => F(F(F(F(x)))) } ! val standardF = { def F = f1 andThen f2 andThen f3 andThen f4 andThen f5 andThen f6 andThen f7 andThen f8 F andThen F andThen F andThen F }
  8. 4DBMBʹ͓͚Δؔ਺߹੒ class Comopse[A, B, C]( f: Function1[A, B], g: Function1[B,

    C] ) extends Function1[A, B] { def apply(x: A) = g(f(a)) } Compose Function1 Compose Function1 Compose Function1 Compose Function1 Function1
  9. ࠷దԽϑΣʔζͷಋೖ def main() = { // ΦϒδΣΫτάϥϑͷߏங val arrow: Long

    => Double = buildArrow() // ࠷దԽ val optimized = optimize(arrow) // ࣮ߦ while(true) { run(optimized) } }
  10. Ϋϥεੜ੒ʹΑΔ࣮૷ w +BWBTTJTUϥΠϒϥϦͰ͜͏͍͏ΫϥεΛಈతʹ ࡞Δ clas Compose5 extends Function1 { val

    f1: Function1 val f2: Function1 // ... def apply(a: Any) = apply$ID(a) def apply$ID(a: Int): Double = { f5$ID(f4$II(f3$DI(f2$ID(f1$II(a)))) } // ... }
  11. ϕϯνϚʔΫ݁Ռ w ͪΐͬͱ଎͘ͳΓ·ͨ͠Ͷ  w .FUIPE)BOEMF࢖͑͹Ϋϥεੜ੒ͳ͠ͰΦʔό ϔου࡟ݮͰ͖ΔͷͰ͓ಘ w ͔͠͠CBTFMJOF·Ͱ͸ԕ͍ standard

    ௨ৗͷؔ਺߹੒ 4515.900 ± 154.250 fast ߹੒Ϋϥεͷੜ੒ 5499.803 ± 183.904 fastMH MethodHandleʹΑΔ߹੒ 5476.340 ± 45.937 baseline ߹੒ͤͣ௚઀ݺͿ 21772.108 ± 154.944 ୯Ґ: k ops/sec
  12. ϑΟʔϧυͷ༥߹ w Ծ૝ؔ਺ݺͼग़͠Λ࣮ؔ਺ݺͼग़͠ʹม׵Ͱ͖Δ w +*5ίϯύΠϧͰͷ࠷దԽ΋͖͖΍͘͢ͳΔ͔΋ class A(b: B) { def

    foo = b.bar + 1 } class B(c: C) { def bar = 99 + c.baz } class C { def baz = 111 } class A_opt(b: B) extends A{ def foo = b_bar + 1 def b_bar = 99 + b_c_baz def b_c_baz = 111 }
  13. ϝιουͷΠϯϥΠϯԽ w ϑΟʔϧυ༥߹ޙɺಉҰΠϯελϯε಺Ͱͷϝιο υΠϯϥΠϯԽΛߦ͏ class A_opt(b: B) extends A{ def

    foo = b_bar + 1 def b_bar = 99 + b_c_baz def b_c_baz = 111 } class A_opt2(b: B) extends A{ def foo = 99 + 111 + 1 }
  14. ϕϯν݁Ռ w ر๬͕ݟ͖͑ͯ·ͨ͠Ͷ w ؔ਺ݺͼग़͠ίετͷపఈͨ͠࡟ݮͱɺ+*5ίϯύΠϧʹΑΔ ࠷దԽ͕ޮ͖΍͍͢ܗࣜʹͳͬͨ͜ͱ͕଎౓ʹͭͳ͕ͬͨͱݟ ͍ͯ·͢ w CBTFMJOFͱͷࠩ͸ɺCPYJOH͕ফͤͯͳ͍ͷ͕ؔ܎ͯͦ͠͏ standard

    ௨ৗͷؔ਺߹੒ 4640.914 ± 77.983 fused Field fusionద༻ 7187.380 ± 321.257 fuseInlined Field Fusion + Method Inlining 12924.076 ± 207.785 baseline ߹੒ͤͣ௚઀ݺͿ 21841.596 ± 223.906 ୯Ґ: k ops/sec
  15. Ϋϥεੜ੒ ίϯετϥΫλղੳ w $ͷαϒΫϥεΛఆ͍ٛͨ͠ w "Yͷ஋͸"ίϯετϥΫλͷୈҰҾ਺͕࢖ΘΕΔ w #Zͷ஋͸#ίϯετϥΫλͷୈҰҐҾ਺͕࢖ΘΕΔ w #ίϯετϥΫλ͸"

     ΛݺͿ w $ίϯετϥΫλͷҾ਺͸Ͱ͋Δ΂͖ w ͱ͍͏͜ͱΛɺϑΟʔϧυͷ஋ͱ֤Ϋϥεఆ͔ٛΒٻΊΔ class A(val x: Int) class B(val y: Int) extends A(1) class C(val x: Int) extends B(x) ! optimize(new C(1))
  16. ࠓޙͷల๬ w ·ͩαϙʔτͯ͠ͳ͍ཁૉ͕ଟ͍ w OFXͱ͔ྫ֎ϋϯυϥͱ͔ w ΠϯλϑΣʔεͷσϑΥϧτ࣮૷΍*O%Z w 4DBMBͰ࢖ͬͯͳ͍͍͍͔͠ͱࢥͬͨΒɺ࠾༻ ͷಈ͖͕

    w ҆શͳॻ͖׵͑ͷอো w ͜ͷॻ͖׵͑͸Ͳ͏͍ͬͨ৚݅ͷ΋ͱͰ҆શͳͷ ͔ ͱ͍͏ͷΛ։ൃऀͷצʹཔ͍ͬͯΔ w ܗࣜతख๏ɺಋೖ͍ͨ͠Ͱ͢Ͷʜʜ
  17. ؔ࿈ࢿྉ w 4PPU w όΠτίʔυղੳ࠷దԽϥΠϒϥϦͷ࿝ฮ w ͪ͜Β͸੩తͳม׵Λલఏͱ͍ͯ͠ΔΑ͏Ͱ͢ w 4USFBN+*5 w

    $PNNFOTBM$PNQJMFS֓೦ w .FUIPE)BOEMFͷݩωλ w ੴ࡚Ұ໌ࢯͷ֤छࢿྉ w +7.͕࠾༻͍ͯ͠Δ࠷దԽख๏ʹ͍ͭͯͷوॏͳࢿྉ w ೥୅લ൒ʹॻ͔Εͨ΋ͷͰ͕͢ɺ+*5ίϯύΠϥͷॲཧʹͭ ͍ͯॻ͔Εͨࢿྉ͸ຊ౰ʹগͳ͍ͷͰوॏ w (JPSHJE[F (FPSHF.PEVMBS4ZOUIFTJ[FS1SPHSBNNJOHJO)BTLFMM%JTT.4D%JTTFSUBUJPO  %FQBSUNFOUPG$PNQVUFS4DJFODFBOE&OHJOFFSJOH %JWJTJPOPG$PNQVUJOH4DJFODF $IBMNFST 6OJWFSTJUZPG5FDIOPMPHZBOE5IF6OJWFSTJUZPG(PUIFOCVSH (PUIFOCVSH 4XFEFO  w )BLFMM :BNQBͰϞδϡϥʔγϯη࡞Δ࿩