Save 37% off PRO during our Black Friday Sale! »

High Performance FastAPI

0881bf028854bea223770e6bfc0a4065?s=47 Ikuo Suyama
October 15, 2021

High Performance FastAPI

PyCon JP 2021 発表資料です。

0881bf028854bea223770e6bfc0a4065?s=128

Ikuo Suyama

October 15, 2021
Tweet

Transcript

  1. )JHI1FSGPSNBODF 'BTU"1* *LVP4VZBNB

  2. ಃࢁҭஉ*LVP4VZBNB !NBSUJO@MPWFS@TF 4NBSU/FXT *OD ✤"ET#BDLFOE&OHJOFFS ✤Πϯλʔωοτ޿ࠂ͕ઐ໳ ✤zࡾ౓ͷ൧ΑΓ޿ࠂ͕޷͖z ✤+7.࢖͍ 1ZUIPOྺ೥͘Β͍ ✤͓खॊΒ͔ʹʜ🙇

  3. ͜ͷηογϣϯ͔ΒಘΒΕΔ͜ͱ ✤ ϓϩϑΝΠϦϯάख๏ͱϘτϧωοΫͷಛఆ ✤զʑͷ؀ڥͰൃੜͨ͠۩ମతͳ՝୊ͱͦͷରࡦ 1ZUIPO8FC"QQMJDBUJPO ύϑΥʔϚϯενϡʔχϯάͷ۩ମతͳख๏

  4. λΠτϧ͔Β෼͔ΔΑ͏ʹ  ͜ͷຊʹ େม͓ੈ࿩ʹͳΓ·ͨ͠🙇

  5.  *OUSPEVDUJPO  ෛՙςετͱϓϩϑΝΠϦϯά  ௚໘ͨ͠՝୊ͱରࡦ  ·ͱΊ "HFOEB

  6. *OUSPEVDUJPO  ෛՙςετͱϓϩϑΝΠϦϯά  ௚໘ͨ͠՝୊ͱରࡦ  ·ͱΊ "HFOEB

  7. "CPVU4NBSU/FXT  ݄ؒ 2,000ສਓ ɹɹɹར༻ऀ਺ 1೔໿16.7෼ར༻ ※2 1ਓ͋ͨΓ ʢ2019೥8݄ ೔ถ߹ࢉʣ

    No.1 χϡʔεΞϓϦ
 Ϣʔβʔ਺※1 ੜ׆ऀͷʮຖ೔ͷश׳ʯ
 ೔ຊ࠷େͷχϡʔεΞϓϦ ※1. Nielsen Mobile NetView 2021೥1݄࣌఺ ʢεϚʔτχϡʔεͷར༻ऀ਺ΛΞϓϦέʔγϣϯϨϕϧͰूܭʣ ※2. ࣗࣾ਺஋2021೥1݄ฏۉ
  8. 1ZUIPOJO4NBSU/FXT$PVQPO$IBOOFM OFXTTFSWJDF DPVQPOTFSWJDF BQJHBUFXBZ DPVQPOBENJO 3FEJT 3%# 8FCαʔόʔʹ'BTU"1*Λ࠾༻

  9. • 'MBTLMJLFͳΞϊςʔγϣϯϕʔεͷϧʔςΟϯά"1* • 0QFO"1*ରԠ • ؆қతͳ%*ػೳ • ֮͑Δ͜ͱ͕গͳ͘ɺૉૣ͘։ൃΛ্ཱͪ͛ΒΕΔ l'BTU"1*͸ɺ1ZUIPOͷඪ४Ͱ͋Δܕώϯτʹج͍ͮͯ 1ZUIPOҎ߱Ͱ"1*Λߏங͢ΔͨΊͷɺ

    ϞμϯͰɺߴ଎ ߴύϑΥʔϚϯε ͳɺ8FCϑϨʔϜϫʔΫͰ͢ɻz Š'BTU"1* αʔϏεͷਝ଎ͳ্ཱͪ͛Λ࣮ݱʂ
  10. 'BTU"1*ඦฉ͸Ұߦʹ೗͔ͣ

  11. 1IBTFE3PMMPVUʜ OFXTTFSWJDF DPVQPOTFSWJDF BQJHBUFXBZ DPVQPOBENJO 3FEJT 3%#

  12. 'VMMZ3PMMPVU OFXTTFSWJDF DPVQPOTFSWJDF BQJHBUFXBZ DPVQPOBENJO 3FEJT 3%# εϚχϡʔͷશྗʂʂ

  13. 'VMMZ3PMMPVU OFXTTFSWJDF DPVQPOTFSWJDF BQJHBUFXBZ DPVQPOBENJO 3FEJT 3%# εϚχϡʔͷશྗʂʂ 🔥 🔥

    🔥 🔥
  14. 'VMMZ3PMMPVU OFXTTFSWJDF DPVQPOTFSWJDF BQJHBUFXBZ DPVQPOBENJO 3FEJT 3%# εϚχϡʔͷશྗʂʂ 🔥 🔥

    🔥 🔥 ύϑΥʔϚϯενϡʔχϯάͷཱྀ΁ʜ
  15.  *OUSPEVDUJPO ෛՙςετͱϓϩϑΝΠϦϯά  ௚໘ͨ͠՝୊ͱରࡦ  ·ͱΊ "HFOEB

  16. ύϑΥʔϚϯενϡʔχϯάͷ৺ಘ ̍ɽੑೳ໨ඪΛܾΊΔ ̎ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ̏ɽϘτϧωοΫΛղফ͢Δ ੑೳ໨ඪΛୡ੒ͨ͠ʁ &/%🎉 :&4 /0

  17. 8FC"QQMJDBUJPOʹ͓͚Δʮੑೳʯ ໨ඪ͕ͳ͍ͱແݶʹ΍Γଓ͚ͯ͠·͏ʂ εϧʔϓοτ ୯Ґ࣌ؒʹͲΕ͘Β͍ͨ͘͞Μ ϦΫΤετΛ͞͹͚Δ͔ʁ ? ָ͗͢͠ΔͷͰ ϨΠςϯγ ̍ͭͷϦΫΤετʹ ͲΕ͘Β͍͕͔͔࣌ؒΔ͔ʁ

    ɽੑೳ໨ඪΛܾΊΔ
  18. -ʜฏۉ଴ͪϓϩηε਺ Еʜ ୯Ґ࣌ؒ͋ͨΓ ౸ண਺ 8ʜฏۉ଺ࡏ࣌ؒ 8FC"QQMJDBUJPOੑೳͷ࿩ʹஔ͖׵͑Δͱʜ WIP(Parallerism) = Throughput ×

    Latency L = λW Ϧτϧͷ๏ଇ ɽੑೳ໨ඪΛܾΊΔ
  19. ྫɿ SQTͷγεςϜΛDPSFͰࡹ͖͍ͨ ɹDPSFͷΠϯελϯε ͋Δ͍͸1PET Λ୆ฒ΂Δͱͯ͠ ̍୆͋ͨΓSQTࡹ͚͹ྑ͍ͷͰ  ͭ·Γ̍Πϯελϯεͷੑೳ໨ඪΛ εϧʔϓοτSQT Ҏ্

    ɺϨΠςϯγNT ҎԼ ɺͱઃఆ͢Δ 5(WIP)/100rps = 0.05s = 50ms ஫ɿ࣮ࡍʹ͸$16Λ࢖͍͖ΕΔΘ͚Ͱ͸ͳ͍͠ɺ*0΍$PODVSSFODZ΋͋Δ͠ɺ ,Tͷ1PEͰ͸ར༻Ͱ͖Δ$16ίΞ਺ͱMJNJU͕Ұக͠ͳ͍͠ɺ ʜͳͲͳͲͳͲ΋ͬͱ࿩͸ෳࡶͰ͕͢ɺେମͷ໨҆ͱͯ͠ ੑೳ໨ඪͷܾΊํɿྫ ɽੑೳ໨ඪΛܾΊΔ
  20. ϧʔϧϓϩάϥϜ͕Ͳ͜Ͱ࣌ؒΛফඅ͢Δ͜ͱʹͳΔ͔஌Δ͜ͱ͸Ͱ͖ͳ͍ɻ ϘτϧωοΫ͸ڻ͘΂͖ՕॴͰى͜Δ΋ͷͰ͋Δɻ ͕ͨͬͯ͠ɺͲ͕͜ϘτϧωοΫͳͷ͔Λ͸͖ͬΓͤ͞Δ·Ͱ͸ɺ ਪଌΛߦͬͨΓɺεϐʔυϋοΫΛͯ͠͸ͳΒͳ͍ɻ ϧʔϧܭଌ͢΂͠ɻܭଌ͢Δ·Ͱ͸଎౓ͷͨΊͷௐ੔Λͯ͠͸ͳΒͳ͍ɻ ίʔυͷҰ෦͕࢒ΓΛѹ౗͠ͳ͍ͷͰ͋Ε͹ɺͳ͓͞ΒͰ͋Δɻ Š3PC1JLF/PUFTPO1SPHSBNNJOHJO$ Š<8JLJQFEJB>ύΠΫ$ϓϩάϥϛϯάʹؔ͢Δ֮͑ॻ͖ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ϘτϧωοΫҎ֎͸νϡʔχϯάͯ͠΋ແବແବ

    ਪଌ͢ΔͳܭଌͤΑ ܭଌͤͣϘτϧωοΫΛ஌Δ͜ͱ͸Ͱ͖ͳ͍͠ɺ
  21. ̍ɽੑೳ໨ඪΛܾΊΔ ̎ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ̏ɽϘτϧωοΫΛղফ͢Δ ੑೳ໨ඪΛୡ੒ͨ͠ʁ &/%🎉 :&4 /0 ෛՙΛ͔͚ͯ 4ZTUFNશମͷෛՙঢ়گΛ֬ೝ "QQMJDBUJPOͷෛՙঢ়گΛ֬ೝ

    -PDBMͰͰ͖ΔݶΓಉ͡ঢ়گΛ࠶ݱ͠ɺ ෛՙΛ͔͚ͭͭܭଌ ϘτϧωοΫͷ ࠷΋ٙΘ͍͠ՕॴΛ௚͢ ϘτϧωοΫ͕ղফ͞Ε͔ͨʁ ੑೳ͕޲্͔ͨ͠ʁ ෛՙࢼݧ؀ڥʹ σϓϩΠͯ͠ܭଌ ϘτϧωοΫ͸ "QQMJDBUJPO ෛՙࢼݧʹར༻Ͱ͖Δ ؀ڥΛ४උ͢Δ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ "QQMJDBUJPO͕ ϘτϧωοΫʹͳΔ·Ͱमਖ਼ :&4 /0 :&4 /0 ܭଌͷखॱͱπʔϧͨͪ Datadog APM • fastapi_profiler • py-spy ϑΟʔυόοΫϧʔϓΛ ૣ͍ͨ͘͠
  22. • 1ZUIPO੡ͷ͍ΘΏΔϕϯνϚʔΫɾෛՙࢼݧπʔϧ • 1ZUIPO"1*ͰγφϦΦΛهड़ • γϯϓϧ͔ͭඞཁे෼ͳઃఆ • ΫϥελԽͯ͠େ͖ͳෛՙʹ΋ରԠ -PDVTUJTBOFBTZUPVTF TDSJQUBCMFBOE

    TDBMBCMFQFSGPSNBODFUFTUJOHUPPM Š-0$645 ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ෛՙΛ͔͚Δɿ-0$645
  23. ෛՙΛ͔͚Δɿ-0$645 ಉ࣌઀ଓ਺Λࢦఆ rps,median/95% tile latencyΛάϥϑԽ ಉ࣌઀ଓ਺Λ૿΍͠ͳ͕ΒϨΠςϯγ͕ѱԽ͢Δݶք·ͰෛՙΛ͔͚ɺ ࠷େεϧʔϓοτͱͦͷ࣌ͷϨΠςϯγΛ֬ೝ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ

  24. • ෛՙΛ͔͚ͨঢ়ଶͰͷγεςϜશମͷϦιʔεঢ়گΛ֬ೝ • 2ΞϓϦέʔγϣϯ͕ϘτϧωοΫʹͳ͍ͬͯΔ͔ʁ • ϘτϧωοΫ͕ΞϓϦέʔγϣϯʹҠΔ·Ͱ͸ɺΞϓϦέʔγϣϯͷνϡʔχϯά Λͯ͠΋ແବແବ • ͍͍ͩͨ%BUBCBTFͳͲͷϛυϧ΢ΣΞ͕ઌʹϘτϧωοΫʹͳΔ •

    6QTUSFBNͷαʔϏε΍઀ଓ͢Δϛυϧ΢ΣΞ౳ͷϦιʔεʹ༨༟͕͋Δ͕ɺΞϓ Ϧέʔγϣϯͷεϧʔϓοτ্͕͕Βͳ͍ঢ়ଶ·Ͱνϡʔχϯά • ΞϓϦέʔγϣϯҎ֎ͷνϡʔχϯά͸ࠓճ͸ׂѪ🙇 γεςϜશମͷෛՙঢ়گΛ֬ೝɿ%BUBEPH Ҏޙͷ࿩͸ΞϓϦέʔγϣϯ͕ϘτϧωοΫͰ͋Δલఏ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ
  25. αʔϏεຖͷϦΫΤετ਺΍ CPU/MemoryͳͲͷར༻ঢ়گΛ֬ೝ ϛυϧ΢ΣΞͷCPU Usage ΍ ίωΫγϣϯ਺ͷมԽΛ֬ೝ ࡉ͔͍ঢ়گ͸ίϯςφʹBUUBDIͯ֬͠ೝͨ͠Γ΋͢Δ͕ɺ શମͷ܏޲Λ௫Ήͷʹେมศར ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ

  26. • ηοτΞοϓ؆୯ɺαΠυΧʔͱ਺ߦͷىಈίʔυ • ࣮ࡍʹՔಇ͍ͯ͠Δ؀ڥͰͷσʔλ͕खʹೖΔ • ඞཁͳ৘ใ͸େମἧ͓ͬͯΓɺϘτϧωοΫͷൃݟʹ໾ཱͭ ΞϓϦέʔγϣϯܭଌᶃɿ%BUBEPH"1. %BUBEPH"1.ͱ$POUJOVPVT1SPGJMFS͸ɺ8FCαʔϏεɺΩϡʔɺσʔλϕʔε͕ ϦΫΤετɺΤϥʔɺϨΠςϯγʔΛ؂ࢹ͢ΔͨΊͷඪ४ͷύϑΥʔϚϯεμογϡ ϘʔυΛ࢖༻ͯ͠ɺΞϓϦέʔγϣϯΛৄࡉʹՄࢹԽ͠·͢ɻ

    Š%BUBEPH"1. ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ
  27. ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ෛՙ؀ڥΞϓϦέʔγϣϯܭଌɿ%BUBEPH"1. Latency histgram CPU time by scripts CPU time

    by function ͍͕ͭ͜Ұ൪࣌ؒΛ࢖͓ͬͯΓɺ ϘτϧωοΫީิ ͕ͩߴ͍💸ɺಛఆͷΠϯελϯεͷΈʹదԠ͢ΔͳͲͷ޻෉Λʜ
  28. -PDBMΞϓϦέʔγϣϯܭଌᶃɿGBTUBQJ@QSPGJMFS ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ "'BTU"1*.JEEMFXBSFPGQZJOTUSVNFOUUP DIFDLZPVSTFSWJDFDPEFQFSGPSNBODF ŠGBTUBQJ@QSPGJMFS • 'BTU"1*ϛυϧ΢ΣΞͱͯ͠QZJOTUSVNFOUΛ౷߹ • ϩʔΧϧͰίʔυΛमਖ਼ͭͭ͠ɺ$16࣌ؒΛܭଌ͢Δͱ͖ʹศར •

    ύϑΥʔϚϯεӨڹ͕େ͖͍ͨΊɺຊ൪΍ෛՙঢ়گԼͰ͸࢖͑ͳ͍
  29. -PDBMΞϓϦέʔγϣϯܭଌᶃɿGBTUBQJ@QSPGJMFS ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ add_middleware ͢Δ͚ͩͰOK sortϝιου͕ ஗͍ʹϘτϧωοΫީิ

  30. • 3VTU੡ͷϓϩϑΝΠϥɺ௒௿Φʔόʔϔου • ϩʔΧϧ؀ڥෛՙ؀ڥͰͬ͞ͱ$16Λܭଌ͍ͨ͠ͱ͖ʹศར • (*-΍ϚϧνεϨουॲཧͷϘτϧωοΫ͕ٙΘ͍͠ͱ͖ʹ΋ศར • GBTUBQJ@QSPGJMFS͸GBTUBQJ؅ཧԼʹͳ͍εϨουΛܭଌͰ͖ͳ͍ʜ -PDBMΞϓϦέʔγϣϯܭଌᶄɿQZTQZ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ

    *UMFUTZPVWJTVBMJ[FXIBUZPVS1ZUIPOQSPHSBN JTTQFOEJOHUJNFPOXJUIPVUSFTUBSUJOHUIF QSPHSBNPSNPEJGZJOHUIFDPEFJOBOZXBZ ŠQZTQZ
  31. ͜ͷϝιου͕ΤϯυϙΠϯτ ॲཧͷຆͲͷ࣌ؒΛ઎Ί͍ͯΔ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ -PDBMΞϓϦέʔγϣϯܭଌᶄɿQZTQZ

  32.  *OUSPEVDUJPO  ෛՙςετͱϓϩϑΝΠϦϯά ௚໘ͨ͠՝୊ͱରࡦ  ·ͱΊ "HFOEB

  33. 'BTU"1*ͷσϓϩΠ࣌ߏ੒ͱෛՙಛੑ (VOJDPSOTVQQPSUTXPSLJOHBTBQSPDFTTNBOBHFSBOEBMMPXJOHVTFSTUPUFMMJUXIJDITQFDJGJDXPSLFS QSPDFTTDMBTTUPVTF5IFO(VOJDPSOXPVMETUBSUPOFPSNPSFXPSLFSQSPDFTTFTVTJOHUIBUDMBTT "OE6WJDPSOIBTB(VOJDPSODPNQBUJCMFXPSLFSDMBTT Š4FSWFS8PSLFST(VOJDPSOXJUI6WJDPSO <Process Manager> <ASGI Server> <ASGI

    Framework> <Web Framework> ϓϩηεͷىಈͱGPSL  TPDLFUΛXPSLFSʹ౉͢ ϦΫΤετϨεϙϯε ඇಉظॲཧ Request Response Func call callback Extends <Application> Worker ΞϓϦέʔγϣϯ ϏδωεϩδοΫ
  34. 'BTU"1*ͷσϓϩΠ࣌ߏ੒ͱෛՙಛੑ (VOJDPSOTVQQPSUTXPSLJOHBTBQSPDFTTNBOBHFSBOEBMMPXJOHVTFSTUPUFMMJUXIJDITQFDJGJDXPSLFS QSPDFTTDMBTTUPVTF5IFO(VOJDPSOXPVMETUBSUPOFPSNPSFXPSLFSQSPDFTTFTVTJOHUIBUDMBTT "OE6WJDPSOIBTB(VOJDPSODPNQBUJCMFXPSLFSDMBTT Š4FSWFS8PSLFST(VOJDPSOXJUI6WJDPSO <Process Manager> <ASGI Server> <ASGI

    Framework> <Web Framework> Request Response Func call callback Extends <Application> Worker ϓϩηε؅ཧͷΈͳͷͰ ύϑΥʔϚϯεӨڹͳ͠ 4UBSMFUUFΑΓ͸ૣ ͘ͳΒͳ͍͕ 1ZUIPO"4(*ք ࠷଎ 4UBSMFUUFΑΓԼ͕ϘτϧωοΫʹͳΔ͜ͱ͸ࠓճ͸ͳ͔ͬͨ
  35.  *0CPVOEͳॲཧ͕஗͍  $16CPVOEͳॲཧ͕஗͍  .VMUJ5ISFBE͕஗͍  ॲཧܥͷมߋʹΑΔશମతͳੑೳ޲্ ̏ɽ௚໘ͨ͠՝୊

  36. *0CPVOEͳॲཧ͕஗͍ ✤ ௚໘ͨ͠՝୊  3FEJTͷSFBEΞΫηε͕஗͍ ✤ ঱ঢ় • $16Λ࢖͍੾ͬͯͳ͍ͷʹεϧʔϓοτ্͕͕Βͳ͍ •

    UPQ WNTUBU౳ͰDQVVTΛ֬ೝ ✤ ݪҼ • %#ΞΫηεͳͲɺωοτϫʔΫ*0Λཁ͢Δॲཧ • ϑΝΠϧॻ͖ࠐΈ
  37. • ωοτϫʔΫ͕བྷΉͱ͜Ζ͸BTZODBXBJUΛ࢖͏ • ରԠϥΠϒϥϦ΋ଟ͍ • FHBJPSFEJTIUUQYʜ *0CPVOE3FEJTΞΫηε͕஗͍ ରࡦᶃɿBDZODJPΛ࢖͏ BTZODJP͸BTZODBXBJUߏจΛ࢖͍ฒߦॲཧͷ ίʔυΛॻͨ͘ΊͷϥΠϒϥϦͰ͢ɻ

    ŠBTZODJP
  38. ରࡦᶃɿBDZODJPΛ࢖͏ fastAPIͰ͸ϧʔςΟϯά ϝιουΛasyncʹ͢Δ͚ͩ async method ͷ಺෦Ͱawait *0CPVOE3FEJTΞΫηε͕஗͍

  39. • ͦΕͰ΋ωοτϫʔΫ͕བྷΉͱ͜Ζ͸جຊతʹΊͪΌ஗͍ • ϝϞϦࢀরͱൺ΂Δͱdഒ͕͔͔ۙ࣌ؒ͘Δ • ݁Ռ੔߹ੑͷڐ༰౓͕ߴ͍৔߹ɺΞϓϦέʔγϣϯϝϞϦʹऔ ಘͨ݁͠ՌΛΩϟογϡ͢Δ ରࡦᶄɿDBDIF͢Δ Latency Comparison

    Numbers (~2012) ---------------------------------- : Main memory reference 100 ns : Round trip within same datacenter 500,000 ns 500 us — Latency Numbers Every Programmer Should Know *0CPVOE3FEJTΞΫηε͕஗͍
  40. 3FEJTΞΫηε͕஗͍ʜରࡦᶄɿDBDIF͢Δ OrderedDictΛར༻ͨ͠ ؆қͳLRUCache Cache͕ͳ͔ͬͨ৔߹ͷΈ RedisʹΞΫηε method͝ͱΩϟγϡͯ͘͠ΕΔ lru_cacheΞϊςʔγϣϯ΋ศར *0CPVOE3FEJTΞΫηε͕஗͍

  41. ✤ ௚໘ͨ͠՝୊  63-Τϯίʔυॲཧ͕஗͍  Ґஔ৘ใͷHFPIBTIͷܭࢉ͕஗͍  Ґஔ৘ใ͔Βͷڑ཭ͷܭࢉ͕஗͍ ✤ ঱ঢ়

    • UPQ WNTUBU౳ͰVTFSUJNF͕ுΓ෇͘ɺ-"͕ߴ͍ • LT؀ڥͰ͋Ε͹5ISPUUMF͞ΕΔ ✤ ओͳݪҼ • ුಈখ਺఺ܭࢉͳͲγϯϓϧʹॏ͍ܭࢉ • ඇޮ཰ͳΞϧΰϦζϜ $16CPVOEͳॲཧ͕஗͍
  42. ରࡦɿࣄલܭࢉDBDIF͢Δ τϥϯβΫγϣϯ͝ͱʹ݁Ռ͕มΘΒͳ͍΋ͷ͸ ΦϑϥΠϯͰࣄલʹܭࢉ͓͔ͯ͘͠ɺ ܭࢉ݁ՌΛDBDIF͢Δ͜ͱͰॏ͍ܭࢉࣗମΛόΠύε͢Δ urllib/parse.py Ͱଟ͘ͷ࣌ؒΛ࢖͍ͬͯΔ Urlͷܭࢉ݁ՌΛ Cache͢Δ $16CPVOE63-Τϯίʔυॲཧ

  43. • ϦΫΤετ͝ͱʹΦϯϥΠϯͰܭࢉ͠ͳ͚Ε͹ͳΒͣɺ͔ͭܭࢉෛՙͷ ߴ͍΋ͷ • (FPIBTIʜҐஔ৘ใ MBUMPO ΛಛఆͷྖҬ͝ͱʹIBTIԽ͢Δ • $ݴޠͳͲͷ௿ਫ४ݴޠʹ༧ΊίϯύΠϧͯ͠͠·͏ •

    ؔ਺͝ͱͳͲɺ෦෼తʹదԠ͢Δ͜ͱ͕Մೳ ରࡦɿ$ZUIPO $ZUIPOJTBOPQUJNJTJOHTUBUJDDPNQJMFSGPSCPUIUIF1ZUIPO QSPHSBNNJOHMBOHVBHFBOEUIFFYUFOEFE$ZUIPO QSPHSBNNJOHMBOHVBHF Š$ZUIPO $16CPVOEHFPIBTIͷܭࢉ͕஗͍
  44. ࢦఆ͞ΕͨҐஔ৘ใ͔Β geohash(maptile)Λܭࢉ͢Δؔ਺ .pyxʹॻ͍ͯίϯύΠϧ͢Δ ݺͼग़͠ଆ͸௨ৗͷPythonͱಉ͡ ͜Μͳײ͡ͷ setup.pyΛ४උͯ͠ docker build͢Δͱ͖ʹ ίϯύΠϧ͍ͯ͠Δ ରࡦɿ$ZUIPO

    $16CPVOEHFPIBTIͷܭࢉ͕஗͍
  45. • ϦΫΤετͷҐஔ৘ใͱɺۙ͘ͷΫʔϙϯͱͷڑ཭ͷܭࢉ • ۙ͘ʹ͋ΔΫʔϙϯͷ਺͚ͩϧʔϓͯ͠ܭࢉ͍ͯͨ͠ • ϧʔϓॲཧΛɺOVNQZͷϕΫτϧܭࢉΛ༻͍ͯ$16DBDIFͷར༻Λ࠷దԽ͢Δ • $16$BDIF͔Βͷϩʔυ͸3".ΑΓ΋ང͔ʹߴ଎ ରࡦɿOVNQZ $16CPVOEڑ཭ͷܭࢉ͕஗͍

    Latency Comparison Numbers (~2012) ---------------------------------- L1 cache reference 0.5 ns L2 cache reference 7 ns 14x L1 cache : Main memory reference 100 ns 20x L2 cache, 200x L1 cache — Latency Numbers Every Programmer Should Know
  46. ϕΫτϧԽͯ͠ɺ Ҏޙͷܭࢉ͸ϕΫτϧܭࢉ ϦΫΤετͷҐஔ৘ใͱͦΕͧΕͷ Ϋʔϙϯͷڑ཭Λ ΦϯϥΠϯͰܭࢉ͍ͨ͠ ରࡦɿOVNQZ $16CPVOEڑ཭ͷܭࢉ͕஗͍

  47. .VMUJ5ISFBE͕஗͍ ✤ ௚໘ͨ͠՝୊  -PHHJOH͕஗͍ ✤ ঱ঢ় • 'BTU"1*Ҏ֎ͷ5ISFBEͰ$16Λ࢖͍ͬͯΔ •

    %BUBEPH"1.Ͱ֬ೝ • QZTQZUPQͰ(*-ͷ஋͕௿͍ ✤ ݪҼ • ࠓճ͸MPHSVͱ͍͏ϥΠϒϥϦ͕.VMUJ5ISFBEͰϩάΛॻ͍͍ͯͯɺͦ ͜ʹ͕͔͔͍࣌ؒͬͯͨ • 1ZUIPO͸5ISFBEΛѻ͑Δ͕ɺ(*-͕͋ΔͨΊޮ཰͕ѱ͍
  48. .VMUJ5ISFBE͕஗͍ *O$1ZUIPO UIFHMPCBMJOUFSQSFUFSMPDL PS(*- JTBNVUFY UIBUQSPUFDUTBDDFTTUP1ZUIPOPCKFDUT QSFWFOUJOHNVMUJQMF UISFBETGSPNFYFDVUJOH1ZUIPOCZUFDPEFTBUPODF Š(MPCBM*OUFSQSFUFS-PDL (*-(MPCBM*OUFSQSFUFS-PDL

    • 5ISFBEΛཱͯͯ΋ಉ࣌ʹ࣮ߦ͞ΕΔͷ͸ϓϩηε಺Ͱ̍ͭ • 1ZUIPOͰ͸ฒྻॲཧ͸.VJUM1SPDFTTJOH ͜Εʹฒߦॲཧ BTZODJPΛ૊Έ߹Θͤͯରॲ͢Δ
  49. .VMUJ5ISFBE͕஗͍ Log৘ใͷγϦΞϥΠζͱ emmitʹ͕͔͔͍࣌ؒͬͯΔ Queue͔Βऔಘͯ͠σγ ϦΞϥΠζ͍ͯ͠Δ໛༷ _enqueue͕true(default) ͩͱɺSimpleQueueͱlogग़ྗ༻ ͷThreadΛͨͯΔ multiprocessing.SimpleQueue ͷ࣮ମ͸Processؒ௨৴ͷPipe

    8SJUFS5ISFBEͱͷσʔλड͚౉͕͠஗ͦ͏
  50. .VMUJ5ISFBE-PHHJOH͕஗͍ ରࡦɿϩΪϯάͷ཈੍ͱϥΠϒϥϦͷมߋ w GBTUMPHHJOHʹ৐Γ׵͑ͨ w ͦͦ͜͜༰ྔͷେ͖͍MPHΛు͖͍͕ͨɺະղܾ

  51. • ීஈ࢖͍ͬͯΔ$1ZUIPOҎ֎ʹ΋༷ʑͳॲཧܥ͕͋Δ • ௿ڃݴޠʹίϯύΠϧ͢Δ΋ͷɺ+*5ίϯύΠϥΛ࣋ͭ΋ͷɺFUDʜ • ࠓճ͸ͭΛࢼͯ͠ɺ1Z1ZΛ࠾༻ͨ͠  DJOEFS  1Z1Z

    • $16͕ϘτϧωοΫͰ͋Ε͹ɺશମతͳੑೳ޲্͕ظ଴Ͱ͖Δ ॲཧܥͷมߋʹΑΔશମతͳੑೳ޲্
  52. ॲཧܥͷมߋDJOEFS $JOEFSJT*OTUBHSBNTJOUFSOBMQFSGPSNBODFPSJFOUFEQSPEVDUJPO WFSTJPOPG$1ZUIPO *UDPOUBJOTBOVNCFSPGQFSGPSNBODFPQUJNJ[BUJPOT JODMVEJOH CZUFDPEFJOMJOFDBDIJOH FBHFSFWBMVBUJPOPGDPSPVUJOFT ʜ ŠDJOEFS •

    'BDFCPPLۘ੡ͷύϑΥʔϚϯεվળ͞Εͨ$1ZUIPO • զʑͷ؀ڥͰ͸ɺ ఔ౓ͷੑೳ޲্͕ݟΒΕͨ • $1ZUIPOͱͷޓ׵ੑ͕ߴ͘ɺϥΠϒϥϦ΋΄ͱΜͲ࢖͑Δ • ͕ɺࣗલͰϏϧυ͢Δඞཁ͕͋ͬͨΓ(JU)VC΋׆ൃͰͳ͔ͬͨΓυΩϡϝϯτ͕օແͩͬͨ Γͱɺӡ༻໘ͰͭΒ͘அ೦
  53. ॲཧܥͷมߋ1Z1Z "GBTU DPNQMJBOUBMUFSOBUJWFJNQMFNFOUBUJPOPG1ZUIPO 0OBWFSBHF 1Z1ZJTUJNFTGBTUFS  UIBO$1ZUIPO Š1Z1Z • +*5ίϯύΠϥ*ODNJOJNBSL($

    • $1ZUIPOͱͷޓ׵ੑ΋͔ͳΓߴ͍ • ΋ͪΖΜσϝϦοτ΋ͦΕͳΓʹ͋Δ • ͕ɺੑೳ໘ͰͷϝϦοτ͕େ͖͔ͬͨͨΊɺݱࡏຊ൪Քಇத • ͍ۙੑೳ޲্Λ֬ೝ
  54. ۜͷ஄ؙͰ͸ͳ͍ ✤ ࡌͤସ͑ɾӡ༻ʹ͋ͨͬͯ௚໘ͨ͠՝୊  ࠷৽൛͕1ZUIPO  Ұ෦ͷϥΠϒϥϦ͕࢖͑ͳ͍  ($0QUJPOࢦఆ࿙ΕͰ00.ࢮ 

    'BTU"1*ͱͷ૊Έ߹ΘͤͰϝϞϦϦʔΫʁ ॲཧܥͷมߋ1Z1Z
  55. ՝୊ᶃɿ࠷৽൛͕1ZUIPO ✤ ՝୊ɿݱࡏͷ࠷৽൛͕1ZUIPO • CFUB DPNNJOHTPPO ✤ ରԠ • ϕʔεͰ։ൃ͍ͯͨ͠ͷͰɺҰ෦ͷՕॴΛμ΢ϯάϨʔυ

    • ηΠ΢νԋࢉࢠ:= • Ґஔઐ༻Ҿ਺def huga(hoge, /, …) • ࣮࣭͋Μ·Γࠔ͍ͬͯͳ͍ ॲཧܥͷมߋ1Z1Z
  56. ՝୊ᶄɿҰ෦ͷϥΠϒϥϦ͕࢖͑ͳ͍ ✤ ՝୊ɿ1&1֎෦ιʔεͷϏϧυΛ൐͏ϥΠϒϥϦ͸΄΅/( • FH • PSKTPOʜSVTU੡ߴ଎+40/γϦΞϥΠβ😭 • EEUSBDFQZʜ%BUB%PH"1.ܭଌ༻😭 •

    GBTUBQJQSPGJMFSʜ͜͜·Ͱେ׆༂ͷϓϩϑΝΠϥ😭 • CMBDL DBVTFECZUZQFEBTU ʜ-JOUFS'PSNBUUFS QZTFOܦ༝Ͱར༻😭 ✤ ରԠ • ผͷϥΠϒϥϦΛ࢖͏ • ։ൃ؀ڥ༻ͱσϓϩΠ༻ͰผʑͷॲཧܥΛར༻͢Δ ॲཧܥͷมߋ1Z1Z
  57. ՝୊ᶄɿҰ෦ͷϥΠϒϥϦ͕࢖͑ͳ͍ $1ZUIPOͰ͸ಈ͕͘1Z1ZͰ͸ಈ͔ͳ͍ͱ͍ͬͨෆ۩߹Λճආ͢ΔͨΊɺ $%ͷࡍʹຊ൪༻ίϯςφΛ༻͍ͯFFςετΛ࣮ࢪ͍ͯ͠Δ ։ൃ༻ / CPython3.7 CI্ͰͷUT΋ͪ͜ΒͰ࣮ߦ ຊ൪༻ / PyPy3.7

    ॲཧܥͷมߋ1Z1Z
  58. ՝୊ᶅɿ($0QUJPOࢦఆ࿙ΕͰ00.ࢮ ✤ ՝୊ɿϝϞϦ͕ϞϦϞϦ੒௕͠ɺ00.,JMMFSʹΑΓࢮ • 1Z1Zͷ($͸JODNJOJNBSL • ($0QUJPOͷઃఆ͕ඞཁ ✤ ରԠ •

    1:1:@($@%&#6(Λࢦఆͯ͠($-PHΛग़ྗ͢Δ • 1:1:@($@."9Λࢦఆͯ͠ώʔϓ্ݶ஋Λ੍ݶ͢Δ ॲཧܥͷมߋ1Z1Z
  59. ՝୊ᶅɿ($0QUJPOࢦఆ࿙ΕͰ00.ࢮ MaxWorker x PYPY_GC_MAX Λ֬อ ࠷௿ݶ PYPY_GC_MAX͸ࢦఆ ॲཧܥͷมߋ1Z1Z

  60. ՝୊ᶆɿ'BTU"1*ͱͷ૊Έ߹ΘͤͰϝϞϦϦʔΫʁ ✤ ՝୊ɿϝϞϦ͕ϞϦϞϦ੒௕͠ɺ00.,JMMFSͰࢮʢ̎ʣ • &DIPαʔόʔͰ΋ɺෛՙΛ͔͚ΔͱϝϞϦ͕୯ௐ૿Ճ͢Δ • Ϧόϓϩ͍ͯͨ͠OHJOYͰ,FFQ"MJWF͍ͯ͠ͳ͔ͬͨʜ ✤ ରԠ •

    )551઀ଓʢ%FGBVMU,FFQ"MJWFʣʹ͢Δͱղফͨ͠ • VWJDPSOͷUJNFPVULFFQBMJWF΋ઃఆ͓ͯ͘͠ • ͋·Γਂ௥͍͍ͯ͠ͳ͍͕ɺDPOOFDUJPOपΓͷ໰୊Ά͍ͷͰɺ 'BTU"1*ͱ͍͏ΑΓ͸VWJDPSOͷ໰୊͔ ॲཧܥͷมߋ1Z1Z
  61. ̏ɽ௚໘ͨ͠՝୊ <5ISPVHIQVU> SQT <UJMF-BUFODZ> NT <5ISPVHIQVU> SQT ‐  <UJMF-BUFODZ>

    NT ‐ ͢΂ͯରԠͨ͠ޙɺΠϯελϯεͰͷੑೳվળ
  62.  *OUSPEVDUJPO  ෛՙςετͱϓϩϑΝΠϦϯά  ௚໘ͨ͠՝୊ͱରࡦ ·ͱΊ "HFOEB

  63. ·ͱΊ ✤ ෛՙςετͱϓϩϑΝΠϦϯά • ໨ඪΛܾΊΔˠܭଌ͢ΔˠϘτϧωοΫʹରԠ͢Δ • ໾ཱͭπʔϧͨͪɿMPDVTU EBUBEPH GBTUBQJQSPGJMFS QZTQZ

    ✤ ՝୊ͱରԠ • *0CPVOEBTZODJP DBDIF • $16CPVOEDBDIF $ZUIPO OVNQZ • ॲཧܥͷมߋ1Z1Z
  64. )JHI1FSGPSNBODFͳ 'BTU"1*-JGFΛ

  65. Thank You for Your Kind Attention! UXJUUFS!NBSUJO@MPWFS@TF