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

Go1.19で採用された Pattern-defeating Quicksort の紹介

po3rin
August 15, 2022

Go1.19で採用された Pattern-defeating Quicksort の紹介

今回はPattern-defeating Quicksortの論文を読んでいき、Goでどのように実装されているか簡単に見ていく

po3rin

August 15, 2022
Tweet

More Decks by po3rin

Other Decks in Programming

Transcript

  1. (PͰ࠾༻͞Εͨ 1BUUFSOEFGFBUJOH2VJDLTPSUͷ঺հ )JSPNV/BLBNVSB !QPSJO 1BUUFSOEFGFBUJOH2VJDLTPSUJO(P

  2. தଜ߂෢ ΤϜεϦʔͷ"*ɾػցֶशνʔϜͷιϑτ΢ΣΞ ΤϯδχΞͰओʹݕࡧج൫पΓΛ୲౰͍ͯ͠Δɻ ֤छΞΧ΢ϯτQPSJOͰ΍ͬͯ·͢ɻ )*SPNV/BLBNVSB The Gopher was designed by

    Renée French
  3. *OUSPEVDUJPO

  4. ϦϦʔεϊʔυݟΔͱTPSUύοέʔδͷ಺෦ΞϧΰϦζϜ͕Կ΍Βมߋ͞ΕͨͬΆ͍ (PͷTPSUύοέʔδͷมߋ 

  5. ࣮ࡍʹTPSU[TPSUGVODHPͱTPSU[TPSUJOUFSGBDFHPʹͦΕͧΕQERTPSU@GVODɺQERTPSUͱ͍͏ ؔ਺͕௥Ճ͞Ε͍ͯΔɻݺͼग़͍ͯ͠Δͷ͸TPSU4PSUͱTPSU4MJDF (PͷTPSUύοέʔδͷมߋ 

  6. ࠓճ͸1BUUFSOEFGFBUJOH2VJDLTPSU<> ͷ࿦จΛಡΜͰ͍͖ɺ(PͰͲͷΑ͏ʹ࣮ ૷͞Ε͍ͯΔ͔؆୯ʹݟ͍ͯ͘ 1BUUFSOEFGFBUJOH2VJDLTPSU [1] https://arxiv.org/abs/2106.05123

  7. 1BUUFSOEFGFBUJOH2VJDLTPSUͷͬ͘͟Γͱͨ͠ཧղ ূ໌ͳͲࡉ͔͍࿩͸εΩοϓͯ͠ͳΜͱͳ͘ԿΛ΍ͬͯΔͷ͔ͷཧղΛ໨ࢦ͢ ͜ͷൃදͰಘΕΔ΋ͷ

  8. 1BUUFSOEFGFBUJOH2VJDLTPSU

  9. *OUSPTPSUͷ෮श 1BUUFSOEFGFBUJOH2VJDLTPSU͸Πϯτϩιʔτͷվྑ൛ͳͷͰɺ؆୯ʹΠϯτϩιʔτΛ෮ श͓͖ͯ͠·͢ɻ Πϯτϩιʔτ͸ۙ೥࠷΋࢖༻͞Ε͍ͯΔϋΠϒϦουιʔτΞϧΰϦζϜͰ͋Γɺૠೖιʔ τɺώʔϓιʔτɺΫΠοΫιʔτΛ૊Έ߹ΘͤͨϋΠϒϦουΞϧΰϦζϜͰ͢ɻ ώϡʔϦεςΟοΫͳํ๏Ͱɺ͍ͭͲͷઓུΛ࠾༻͢Δ͔Λܾఆ͠·͢ɻྫ͑͹ɺ࠶ؼͷਂ͞ ͕ਂ͘ͳΓ͗͢Δͱώʔϓιʔτʹ੾Γସ͑ͨΓɺύʔςΟγϣϯͷαΠζ͕খ͘͞ͳΓ͗͢ Δͱૠೖιʔτʹ੾Γସ͑ͨΓ͠·͢ɻ

  10. 1BUUFSOEFGFBUJOH2VJDLTPSU͸ΠϯτϩιʔτΛ֦ு͓Αͼվྑͨ͠΋ͷͰ͢ɻ ޙͰৄ͘͠આ໌͍͖ͯ͠·͕͢ɺ1BUUFSOEFGFBUJOH2VJDLTPSUͷ؊͸؆୯ʹݴ͏ͱ ύʔςΟγϣϯॲཧΛ͍͍ײ͡ʹͨ͠ ࠷ѱέʔεΛ্ख͘ݕ஌ͯ͠ϑΥʔϧόοΫ͢ΔΑ͏ʹͨ͠ ͷͭͰ͢ɻ 1BUUFSOEFGFBUJOH2VJDLTPSU֓ཁ 

  11. ॏෳΛؚΉ໿ཁૉͷγʔέϯεΛιʔτ͢Δ1BUUFSOEFGFBUJOH2VJDLTPSUͷࢹ֮Խɻ 1BUUFSOEFGFBUJOH2VJDLTPSU֓ཁ 

  12. 1BSUJUJPOJOH

  13. 1BUUFSOEFGFBUJOH2VJDLTPSU͸Φϥϯμࠃض໰୊ͷߟ͑ํ͕ϕʔεʹͳ͍ͬͯΔͷͰɺ·ͣ͸Φϥϯμ ࠃض໰୊Λ෮श͠·͢ɻ Φϥϯμࠃض໰୊͸ɺɺͷΈΛؚΉγʔέϯεΛಉ͡ཁૉͰύʔςΟγϣϯ͢Δ໰୊Ͱ͢ɻྫ͑͹Լ هͷJOΛPVUʹม׵͢Δ໰୊Ͱ͢ɻ      

          JO PVU 1BSUJUJPOJOH 
  14. 1BSUJUJPOJOH  ΞϧΰϦζϜ͸୯७Ͱઢܗ࣌ؒͰղ͚·͢ɻNJEͱMPXΛ഑ྻͷ࠷ॳͷཁૉͷJOEFYʹηοτ͠ɺIJHIΛ࠷ ޙͷཁૉͷJOEFYʹηοτ͠·͢ɻ w ΋͠NJEͳΒMPXͱNJEͷཁૉΛSFQMBDFͯ͠IJHIͰσΫϦϝϯτ w ΋͠NJEͳΒNJEΛΠϯΫϦϝϯτ w ΋͠NJEͳΒIJHIͱNJEͷཁૉΛSFQMBDFͯ͠IJHIΛσΫϦϝϯτ

  15.       MPX NJE IJHI

  16.       MPX NJE IJHI 

         MPX NJE IJHI NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI
  17.       MPX NJE IJHI 

         MPX NJE IJHI       MPX NJE IJHI NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJEͳͷͰɺNJE 
  18.       MPX NJE IJHI 

         MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJEͳͷͰɺNJE  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX 
  19.       MPX NJE IJHI 

         MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJEͳͷͰɺNJE  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX 
  20.       MPX NJE IJHI 

         MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJEͳͷͰɺNJE  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX  NJEͳͷͰɺNJE 
  21.       MPX NJE IJHI 

         MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI       MPX NJE IJHI MPX NJE IJHI       NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJEͳͷͰɺNJE  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX  NJEͳͷͰɺMPXͱSFQMBDFͯ͠NJE  MPX  NJEͳͷͰɺNJE  NJEͳͷͰɺIJHIͱSFQMBDFͯ͠IJHI NJE͕IJHIΛ௒͑ͨͷͰऴྃ
  22. 1BSUJUJPOJOHJO1BUUFSOEFGFBUJOH2VJDLTPSU         

     ୯७ͳΫΠοΫιʔτͷ࣮૷Ͱ͸ɺ౳͍͠ൺֱཁૉΛಉ͡ύʔςΟγϣϯʹ഑ஔ͢Δ͜ͱʹ ΑΓϺ O? ͷ࠷ѱͷέʔε͕Ҿ͖ى͜͞Ε·͢ɻ ౳͍͠ཁૉΛޮ཰తʹॲཧ͢Δʹ͸ɺ౳͍͠ཁૉΛूΊͯɺͦΕΑΓେ͖͍ཁૉɺখ͍͞ཁ ૉͷ෼ׂʹͰ͖Ε͹ɺͦͷޙͷιʔτͰ౳͍͠ཁૉΛؚΉύʔςΟγϣϯͷιʔτΛε ΩοϓͰ͖·͢ɻ·͞ʹΦϥϯμࠃض໰୊ͱಉ౳ͷࡾ෼ׂͷߟ͑ํ͕࢖͑ͦ͏Ͱ͢ɻ ͜ͷΑ͏ʹύʔςΟγϣϯΛ෼ׂͰ͖Δͱخ͍͠ 
  23. 1BSUJUJPOJOHJO1BUUFSOEFGFBUJOH2VJDLTPSU  ͜Ε·Ͱʹઆ໌ͨ͠Φϥϯμͷࠃض໰୊ͷղ๏Λ୯७ʹద༻͢Δͱɺ͢΂ͯͷཁૉΛεϫο ϓ͢ΔલʹϐϘοτͱ౳͍͔͠Ͳ͏͔Λ໌ࣔతʹνΣοΫ͢Δඞཁ͕͋Γ·͢ɻ͜Ε͸ɺ౳ ͍͠ཁૉ͕ଟ਺ଘࡏ͢Δ͔Ͳ͏͔ʹؔ܎ͳ͘ൃੜ͠ɺฏۉతͳέʔεͰ͸ύϑΥʔϚϯε͕ ௿Լ͠·͢ɻ ͜ͷ࿦จͰ͸͜ͷ໰୊ʹରॲ͢Δ৽͍͠ղܾࡦΛٞ࿦͓ͯ͠ΓɺLݸͷҟͳΔཁૉΛ࣋ͭೖ ྗʹରͯ͠0 OL ͷ࠷ѱέʔεʹऩ·Δख๏ΛఏҊ͍ͯ͠·͢ɻ1BUUFSOEFGFBUJOH

    2VJDLTPSUͰ͸͜ͷख๏ΛϕʔεʹΠϯτϩιʔτΛվྑ͍ͯ͠·͢ɻ
  24. 1BSUJUJPO-FGU3JHIU  1   1  QBSUJUJPO@SJHIU QBSUJUJPO@MFGU 1BUUFSOEFGFBUJOH2VJDLTPSU͸ͭͷύʔςΟγϣϯ༻ؔ਺Λ૊Έ߹ΘͤͯઌఔͷύʔςΟγϣϯ

    Λୡ੒͠·͢ɻͭ͸ϐϘοτQʹ౳͍͠ཁૉΛࠨͷύʔςΟγϣϯʹάϧʔϓԽ QBSUJUJPOMFGU ͠ɺ΋͏ͭ͸ϐϘοτʹ౳͍͠ཁૉΛӈͷύʔςΟγϣϯ QBSUJUJPOSJHIU ʹάϧʔϓԽ͠·͢ɻ ͜ͷύʔςΟγϣϯͷ૊Έํͷར఺͸ʮཁૉ͕ϐϘοτͷ஋Ҏ্͔Ͳ͏͔ʯͱ͍͏৚݅ҰݸͰ൑ఆͰ ͖Δ͜ͱͰ͢ɻ
  25. 1BUJUJPOJO(P (PͰ͸TPSUύοέʔδ಺ͷQBUJUJPOؔ਺͕ύʔςΟγϣϯΛ୲͏ɻ࣮ࡍʹؔ਺ΛFYQPSUͯ͠Ұճͩ ͚ಈ͔ͯ͠ΈΔͱ͔֬ʹQBUJUJPO@MFGUͷ݁ՌΛಘΒΕ͍ͯΔɻ

  26. 1BSUJUJPO-FGU3JHIU QBSUJUJPOؔ਺Λར༻ͯ͠࠶ؼతʹγʔέϯεΛύʔςΟγϣϯ͍͖ͯ͠·͢ɻ ྫ͑͹ԼهਤͷΑ͏ʹɺϐϘοτQͰЋΛύʔςΟγϣϯ͠ɺ࠶ؼతʹӈଆͷύʔςΟγϣϯΛϐ ϘοτRͱͯ͠ЌΛύʔςΟγϣϯ͍͖ͯ͠·͢ɻ Q͕Rʹ౳͘͠ͳ͍৔߹͸ɺ্هͷεςοϓΛ࠶ؼతʹద༻͠·͢ɻ࠷ऴతʹQRͱͳͬͨ࣌͸ɺಉ ͡஋ΛਅΜதʹूΊͨߏ੒ʹͰ͖͍ͯ·͢Լਤɻ ͪͳΈʹɺ͋Δ఺RͰЌ͕ۭʹͳͬͨ৔߹ɺQʹ౳͍͠ཁૉ͸ͳ͘ɺЋΛ࠷ॳʹ෼ׂͨ͠ͱ͖ʹ ෼ׂ͕ߦΘΕͨͱ݁࿦෇͚Δ͜ͱ͕Ͱ͖·͢ɻ

  27. https://www.youtube.com/watch?v=jz-PBiWwNjc ͔ΒҾ༻

  28. 1JWPU4FMFDUJPO

  29. +PIO5VLFZ`TOJOUIFS ϐϘοτͷબ୒ʹ͓͍ͯɺෆۉߧύʔςΟγϣϯॲཧΛආ͚ΔͨΊʹɺͳΔ΂͘தԝ஋Λબ୒͢Δ͜ ͱ͕ཧ૝Ͱ͢ɻҰํͰେ͖ͳೖྗͷ৔߹͸ద੾ͳϐϘοτΛબ୒͢Δͷ͸ύϑΥʔϚϯεͷ໰୊͕ൃ ੜ͠·͢ɻͦ͜Ͱ͜ͷ࿦จͰ͸༗໊ͳ+PIO5VLFZ`TOJOUIFSͱ͍͏ख๏ͰϐϘοτΛબ୒͠·͢ɻ ݫີͳதԝ஋Λܭࢉ͢Δʹ͸͢΂ͯͷσʔλϙΠϯτΛอଘ͠ɺͦΕΒΛฒ΂ସ͑ͯதؒ஋Λબ୒͢ Δඞཁ͕͋Γ·͕͢ɺ͜ͷํ๏Ͱ͋Ε͹ૉૣ͘େࡶ೺ͳதԝ஋ͷۙࣅ͕ಘΒΕ·͢ɻ

  30. +PIO5VLFZ`TOJOUIFSJO(P ࣮ࡍͷ(Pͷ࣮૷Ͱ͸ϐϘοτΛબ୒͢Δαϒγʔέϯεͷ௕͞ʹΑͬͯϐϘοτબ୒ํ๏Λ੾Γସ ͑·͢ɻ௕͕͞ҎԼͳΒ୯७ʹதԝ஋ΛͱΓɺҎԼͳΒ୯७ʹ࠷ॳɺதԝɺ࠷ޙͷͭͷ஋ͷத ԝ஋ΛͱΓɺҎ্ͳΒ+PIO5VLFZ`TOJOUIFSΛར༻͠·͢ɻ

  31. #BE1BUJUJPO

  32. #BE1BUJUJPO  1BUUFSOEFGFBUJOH2VJDLTPSU͸ɺࢦఆͨ͠ϐϘοτύʔηϯλΠϧQ ೚ҙͷ஋ ΑΓ΋ෆۉߧͳ෼ׂ ૢ࡞ΛෆྑύʔςΟγϣϯͱݺͼ·͢ ͪͳΈʹϐϘοτύʔηϯλΠϧQ͕ͩͱཧ૝త͸ύʔ ςΟγϣϯʹͳΓ·͢ ɻྫ͑͹ԼهͷΑ͏ͳγʔέϯεͰϐϘοτύʔηϯλΠϧͷϙΠϯτ ࠓճ͸

    Λ࠾༻ͯ͠͠·͏ͱ΄ͱΜͲύʔςΟγϣϯ͕ػೳ͠·ͤΜɻ
  33. #BE1BUJUJPO  :VWBM'JMNVT<>͸Qͷ࠷దͳέʔεͱൺֱͯ͠ΫΠοΫιʔτͷ଎౓௿ԼΛௐ΂·ͨ͠ɻ γϟϊϯͷ஋ΤϯτϩϐʔΛར༻ͯ͠ɺԼهͷΑ͏ͳάϥϑ͕ॻ͚·͢ɻ ͜ͷάϥϑ͔Βɺྫ͑͹Qͷ৔߹͚ͩͷ৔߹͸ɺཧ૝తͳ৔߹ͱൺ΂͚ͯͩ஗͘ͳΔ͜ͱ ͕Θ͔Γ·͢ɻQΛԼճΔͱύϑΥʔϚϯεͷ௿Լ͕ܹ͘͠ͳΔ͜ͱ΋Θ͔Γ·͢ɻ͜ͷͨΊɺ Qͷᮢ஋Λઃఆ͠ɺͦΕΑΓԼճΔෆྑύʔςΟγϣϯͷέʔε͕ଟ͚Ε͹ϑΥʔϧόοΫ͢Δઓུ ΛऔΓ·͢ɻ [3] https://cs.stackexchange.com/questions/31928/solving-recurrence-relation-with-two-recursive-calls/31930#31930

  34. #BE1BUJUJPO  ෆྑύʔςΟγϣϯΛෳ਺ճૺ۰͢ΔͷΛճආ͢ΔͨΊʹԼهεςοϓΛߦ͍·͢ɻ w ࠷ॳʹɺΧ΢ϯλʔͱͯ͠MPH αϒγʔέϯε௕ ʹઃఆ͠·͢ w ෆྑύʔςΟγϣϯʹૺ۰͢ΔͨͼʹɺΧ΢ϯλʔΛσΫϦϝϯτ͠·͢ w

    ࠶ؼݺͼग़͠ͷ։࢝࣌ʹΧ΢ϯλʔ͕ͷ৔߹ɺΫΠοΫιʔτͰ͸ͳ͘ɺώʔϓιʔτΛ࢖༻ ࿦จͰ͸͜ͷϐϘοτύʔηϯλΠϧQͷᮢ஋Λͱ͓ͯ͠Γ(P΋ʹͳ͍ͬͯ·͢ɻ ͜ͷεΩʔϜͷར఺͸ɺΞʔΩςΫνϟ͕มߋ͞Εͨ৔߹ ώʔϓιʔτͷ୅ΘΓʹผͷ࠷ѱͷέʔε ͷιʔτΞϧΰϦζϜΛ࢖༻͍ͯ͠Δ৔߹ͳͲ ʹɺͦΕʹ߹ΘͤͯQΛௐ੔Ͱ͖Δ͜ͱͰ͢ (Pͷ৔ ߹͸͕ϋʔυίʔυ͞Ε͍ͯͯมߋ͸Ͱ͖ͳ͍ ɻ
  35. #BE1BUJUJPO  (PͰ͸QERTPSUؔ਺ͷMJNJUҾ਺͕Χ΢ϯλʔ ʹ͋ͨΓɺ͜Ε͕ʹͳΔͱώʔϓιʔτʹ੾ ΓସΘΓ·͢ɻ

  36. #BE1BUJUJPO  ͪͳΈʹͪΐͬͱͨ͠޻෉ͱͯ͠Χ΢ϯλʔ͸γʔέϯεͷ௕͞ͷॴཁϏοτ਺ͱͯؒ͠ ઀తʹMPH@ O Λܭࢉ͍ͯ͠Δɻ

  37. 1BUUFSOEFGFBUJOH2VJDLTPSUJO(P

  38. 1BUUFSOEFGFBUJOH2VJDLTPSUJO(P (Pʹ࣮૷͞Εͨ1BUUFSOEFGFBUJOH2VJDLTPSU͸࿦จͱൺ΂ͯԼهͷΑ͏ͳมߋ͕Ճ͑ΒΕͯ ͍·͢ɻ w #MPDL2VJDLTPSUΛ࢖ͬͨ࠷దԽΛແޮ কདྷ௥Ճ͞ΕΔՄೳੑ͸͋Γͦ͏  w ϕϯνϚʔΫ݁Ռʹैͬͯɺ͍͔ͭ͘ͷύϥϝʔλʔΛมߋ

  39. (Pʹ͓͚ΔϕϯνϚʔΫ͸ JTTVFͰఏग़͞Ε͍ͯ·͢ɻ΄ͱ ΜͲͷέʔεͰύϑΥʔϚϯε͕վળ͠ ͍ͯ·͢ɻ

  40. $PODMVTJPO

  41. $PODMVTJPO (Pʹ࣮૷͞Εͨ1BUUFSOEFGFBUJOH2VJDLTPSU͸ϕϯνϚʔΫ্༷ʑͳύλʔϯͰύϑΥʔϚ ϯε͕վળ͍ͯ͠Δɻ 1BUUFSOEFGFBUJOH2VJDLTPSU͸ओʹύʔςΟγϣϯॲཧ΍ϐϘοτબ୒ɺෆྑύʔςΟγϣϯ ݕ஌Ͱɺ2VJDLTPSUͷ࠷ѱέʔεΛճආͰ͖ΔΑ͏ʹ޻෉͞Ε͍ͯΔɻ (PʹΞοϓάϨʔυͯ͠TPSU͕ͲΕ͘Β͍ૣ͘ͳ͔ͬͨੋඇࢼͯ͠Έ͍ͯͩ͘͞ɻΑΓࡉ ͔͍޻෉΍ɺৄ͍͠ূ໌ͳͲ͸࿦จΛࢀর͍ͩ͘͞ɻ

  42. ΤϜεϦʔͰ͸ࢸΔॴͰ(PΛ࠾༻ͯ͠։ൃΛ͍ͯ͠Δɻ΋ͪΖΜ(PΛ࣮຿Ͱ΍ͬͨ ͜ͱ͕ͳͯ͘΋׻ܴɻ ֤छ"1* ΞϓϦέʔγϣϯɺݕࡧɺϨίϝϯυɺϦϥϯΩϯά౳  Ϩίϝϯυ"1* σʔλ࿈ܞ#BUDI 1VTI௨஌ج൫ ֤छศརπʔϧͷ։ൃ (Pͷษڧձ΋։͔Ε͓ͯΓɺݱࡏ͸ʮ࣮༻(PݴޠʕγεςϜ։ൃͷݱ৔Ͱ஌͓ͬͯ

    ͖͍ͨΞυόΠεʯ<>ͷྠಡձ΋։͔Ε͍ͯΔɻιϑτ΢ΣΞΤϯδχΞ͚ͩͰͳ ͘ɺ.-ΤϯδχΞ΋(P͕ॻ͚Δͱ͍͏ҟ࣭ͳ؀ڥɻ ΤϜεϦʔ͸(PͰ։ൃ͍ͨ͠ϝϯόʔΛืूத [3] https://www.amazon.co.jp/dp/4873119693/ref=cm_sw_em_r_mt_dp_G4Z5RYS2FDVTVWXVJEYS?_encoding=UTF8&psc=1
  43. (PͰ࠾༻͞Εͨ 1BUUFSOEFGFBUJOH2VJDLTPSUͷ঺հ )JSPNV/BLBNVSB !QPSJO 1BUUFSOEFGFBUJOH2VJDLTPSUJO(P