Ruby 2.4 のハッシュテーブル高速化を理解する

Ruby 2.4 のハッシュテーブル高速化を理解する

第2回 meguro.rb LT で Ruby 2.4 のハッシュテーブル実装について話しました
https://megurorb.connpass.com/event/55107/

2b4f9f20b554d4c77c46fe6a9930d6fe?s=128

Nao Minami

April 20, 2017
Tweet

Transcript

  1. 3VCZͷϋογϡςʔϒϧ ߴ଎ԽΛཧղ͢Δ  /BP.JOBNJ !TPVUI

  2. ࣗݾ঺հ

  3. /BP.JOBNJ!TPVUI !NJOBNJP 4PGUXBSFFOHJOFFS !8BOUFEMZ *OD

  4. w 3VCZͱύϑΥʔϚϯε w 3VCZͰͷϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ·ͱΊ ࠓ೔࿩͢಺༰

  5. w 3VCZͱύϑΥʔϚϯε w 3VCZͰͷϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ·ͱΊ ࠓ೔࿩͢಺༰

  6. 3VCZY

  7. w ೥·Ͱʹ3VCZ͸ഒ଎͘ͳΔCZ!NBU[ 3VCZͱύϑΥʔϚϯε w .VMUJ5ISFBE +*5 FUD w 3VCZY w

    3VCZͰͷϋογϡςʔϒϧߴ଎Խ /&8
  8. w 3VCZͱύϑΥʔϚϯε w 3VCZͰͷϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ·ͱΊ ࠓ೔࿩͢಺༰

  9. ϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ

  10. ϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ

  11. ϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w 36#:#&/$) w ߴ଎Խ w ϝϞϦ࢖༻ྔ࡟ݮ 1000000.times.map{|i| a={}; 8.times{|j|

    a[j]=j}; a} IUUQTSVCZCFODIPSHSVCZSVCZSFMFBTFT SFTVMU@UZQFIBTI@TNBMM
  12. ϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w QJDP@IUUQ@QBSTFS w ߴ଎Խ IUUQLB[FCVSPIBUFOBCMPHDPNFOUSZ ྘ʜ3VCZ ੨ʜ3VCZQSF

  13. ܶతʹ଎͘ͳͬͯΔ

  14. w 3VCZͱύϑΥʔϚϯε w 3VCZͰͷϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ·ͱΊ ࠓ೔࿩͢಺༰

  15. ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ϋογϡςʔϒϧͷিಥ࣌ͷڍಈΛมߋ $IBJOJOH 0QFO"ESFTTJOH w ϙΠϯτ͸ʮσʔλͷہॴੑͷ޲্ʯ

  16. σʔλͷہॴੑ͸ͳͥॏཁ͔ w 1SPDFTTPSʹ͸ଟஈΩϟογϡ͕ଘࡏ w ΩϟογϡʹIJU͢ΔͱύϑΥʔϚϯε޲্ $16 L# .BJO.FNPSZ -$BDIF -$BDIF

    -$BDIF .# (# L# # dOT IUUQTUBDLPWFSqPXDPNRVFTUJPOTBQQSPYJNBUFDPTUUPBDDFTTWBSJPVTDBDIFTBOENBJONFNPSZ dOT
  17. σʔλͷہॴੑ͸ͳͥॏཁ͔ w 1SPDFTTPSʹ͸ଟஈΩϟογϡ͕ଘࡏ w σʔλͷہॴੑ͕ॏཁ $16 L# .BJO.FNPSZ -$BDIF -$BDIF

    -$BDIF .# (# L# # dOT IUUQTUBDLPWFSqPXDPNRVFTUJPOTBQQSPYJNBUFDPTUUPBDDFTTWBSJPVTDBDIFTBOENBJONFNPSZ dOT
  18. 0QFO"ESFTTJOHͰσʔλͷہॴੑ্͕͕Δͷ͸ͳͥʁ w $IBJOJOH 0QFO"ESFTTJOHͭͷΞϧΰϦζϜΛൺֱ w ૬ҧ఺ϋογϡςʔϒϧͷিಥ࣌ͷڍಈ w $IBJOJOH࿈݁ϦετΛḷͬͯ୳ࡧ w 0QFO"ESFTTJOH"SSBZ্ͰJOEFYΛม͑ͯ୳ࡧ

    w ڞ௨఺ w ϋογϡςʔϒϧ͸LFZͷIBTI஋ΛJOEFYʹར༻
  19. $IBJOJOHͷ࢓૊Έ w UBCMF͸࿈݁Ϧετ΁ͷϙΠϯλΛอ࣋ w ಉ͡IBTIΛ࣋ͭ৔߹͸ɺ࿈݁ϦετΛḷΔ IUUQXXXBMHPMJTUOFU%BUB@TUSVDUVSFT)BTI@UBCMF$IBJOJOH FOUSZ UBCMF LFZ

  20. $IBJOJOHͷ໰୊఺ w ࿈݁ϦετͷFOUSZ͸ɺNFNPSZ্Ͱ཭Εͯ഑ஔ w σʔλͷہॴੑ͕௿͍ FOUSZ UBCMF LFZ

  21. 3VCZҎલͷ$IBJOJOH w ૒ํ޲Ϧετʹͳ͓ͬͯΓɺσʔλͷہॴੑ͕௿͍ ͚ͩͰͳ͘QSFWϙΠϯλͳͲͷ෼͚ͩFOUSZͷσʔ λ͕Ͱ͔͍ͷ΋໰୊ IUUQTEFWFMPQFSTSFEIBUDPNCMPHUPXBSETGBTUFSSVCZIBTIUBCMFT UBCMF FOUSZ FOUSZ FOUSZ

  22. 3VCZҎલͷ$IBJOJOH w ૒ํ޲Ϧετʹͳ͓ͬͯΓɺσʔλͷہॴੑ͕௿͍ ͚ͩͰͳ͘QSFWϙΠϯλͳͲͷ෼͚ͩFOUSZͷσʔ λ͕Ͱ͔͍ͷ΋໰୊ IUUQTEFWFMPQFSTSFEIBUDPNCMPHUPXBSETGBTUFSSVCZIBTIUBCMFT UBCMF FOUSZ FOUSZ FOUSZ

    ஗͍
  23. 0QFO"ESFTTJOHͷ࢓૊Έ w UBCMF͸FOUSZ΁ͷQPJOUFSΛ௚઀อ࣋ w ಉ͡IBTIΛ࣋ͭ৔߹͸ɺJOEFYΛͣΒ͢ FOUSZ UBCMF LFZ IUUQXXXBMHPMJTUOFU%BUB@TUSVDUVSFT)BTI@UBCMF0QFO@BEESFTTJOH

  24. 3VCZͷ0QFO"ESFTTJOH UBCMF FOUSJFT TUBSU CPVOE w UBCMF FOUSJFT͕྆ํ"SSBZʹͳ͓ͬͯΓσʔλͷہॴ ੑ͕ߴ͘ɺͭͭͷFOUSZͷσʔλαΠζ΋খ͍͞ IUUQTEFWFMPQFSTSFEIBUDPNCMPHUPXBSETGBTUFSSVCZIBTIUBCMFT

  25. 3VCZͷ0QFO"ESFTTJOH UBCMF FOUSJFT TUBSU CPVOE w UBCMF FOUSJFT͕྆ํ"SSBZʹͳ͓ͬͯΓσʔλͷہॴ ੑ͕ߴ͘ɺͭͭͷFOUSZͷσʔλαΠζ΋খ͍͞ ߴ଎Խ

  26. 3VCZͰ͸ߋʹࡉ͔͘࠷దԽ w ݅਺͕গͳ͍࣌ͷ࠷దԽʢলϝϞϦɺߴ଎Խʣ w ݅਺͕গͳ͍࣌͸BSSBZͭͰMJOFBSTFBSDI w ݅਺͕গͳ͍࣌͸CJU CJUͳͲͰJOEFYΛදݱ w 'VMMDZDMFMJOFBSDPOHSVFOUJBMHFOFSBUPSΛ

    TFDPOEBSZIBTIͱͯ͠ར༻ w ߴ଎͔ͭIBTIͱͯ͠ͷੑೳ΋ྑ͍
  27. w 3VCZͱύϑΥʔϚϯε w 3VCZͰͷϋογϡςʔϒϧߴ଎ԽͱͦͷԸܙ w ߴ଎Խ͸Ͳ͏࣮ݱ͞Εͨͷ͔ʁ w ·ͱΊ ࠓ೔࿩͢಺༰

  28. ·ͱΊ w 3VCZ͸ϋογϡςʔϒϧ͕ܶతʹߴ଎Խɻ࣮ࡍ ʹΞϓϦέʔγϣϯ΋ߴ଎Խɻ w 3VCZʹͯ͠շదͳ3VCZϥΠϑΛૹΖ͏ʂ

  29. ࢀߟϦϯΫ w 3VCZϦϦʔε w IUUQTXXXSVCZMBOHPSHKBOFXT SVCZSFMFBTFE w 'FBUVSF)BTIUBCMFTXJUIPQFOBESFTTJOH w IUUQTCVHTSVCZMBOHPSHJTTVFT

    w 5PXBSET'BTUFS3VCZ)BTI5BCMFT w IUUQTEFWFMPQFSTSFEIBUDPNCMPHUPXBSET GBTUFSSVCZIBTIUBCMFT