äŸ
folly::fbvector<int> numbers({0, 1, 2, 3}); numbers.reserve(10); for (int i = 4; i < 10; i++) { numbers.push_back(i * 2); } assert(numbers[6] == 12);
ããæ°
std :: vectorã¯ãå€ãã®äººãC ++ã§åçã«å²ãåœãŠãããé åã«äœ¿çšããã確ç«ãããæœè±¡æŠå¿µã§ãã ãŸããæãæåã§æã䜿çšãããŠããã³ã³ããã§ãã 倧ããªé©ãã¯ããã®æšæºå®è£ ã«ããããã¯ã¿ãŒã®äœ¿çšå¹çãæ¹åããå€ãã®æ©äŒãæ®ãããŠããããšã§ãã ãã®ããã¥ã¡ã³ãã§ã¯ãfolly :: fbvectorã®å®è£ ãstd :: vectorã®ããã€ãã®åŽé¢ãã©ã®ããã«æ¹åãããã«ã€ããŠèª¬æããŸãã folly / test / FBVectorTest.cppã®ãã¹ãã䜿çšããŠãstd :: vectorãšfolly :: fbvectorã®ããã©ãŒãã³ã¹ãæ¯èŒã§ããŸãã
ã¡ã¢ãªç®¡ç
std :: vectorã¯ææ°é¢æ°çã«ïŒäžå®ã®æé·ä¿æ°ã§ïŒæé·ããããšãç¥ãããŠããŸãã ãã®å®æ°ã®æ£ããéžæã«ãããéèŠãªãã¥ã¢ã³ã¹ã æ°å€ãå°ãããããšé »ç¹ãªè²©å£²ã«ã€ãªãããæ°å€ã倧ãããããšå®éã«å¿ èŠä»¥äžã®ã¡ã¢ãªãæ¶è²»ããŸãã Stepanovã®åæå®è£ ã§ã¯å®æ°2ã䜿çšããŸããïŒã€ãŸããpush_backåŒã³åºãã§ãã¯ãã«æ¡åŒµãå¿ èŠã«ãªããã³ã«ã容éã2åã«ãªããŸããïŒã
æéã®çµéãšãšãã«ãäžéšã®ã³ã³ãã€ã©ã¯ãã®å®æ°ã1.5ã«æžãããŸããããgccã¯2ãæ°žç¶çã«äœ¿çšããŸããå®éãå®æ°2ããã¯ãã«ã«ãã£ãŠä»¥åã«å²ãåœãŠãããã¡ã¢ãªãåå©çšã§ããªããããå®æ°2ããã¹ãŠã®å¯èœãªå€ã®äžã§ææªã§ããããšãæ°åŠçã«èšŒæã§ããŸãã
ããããªããããªã®ããç解ããããã«ããã§ã«å®å šã«æºãããããµã€ãºnãã€ãã®ãã¯ãã«ãæ³åããŠãã ãããããã«å¥ã®èŠçŽ ãè¿œå ããããšããŠããŸãã ããã«ãããstd :: vectorã®å®è£ ã«åŸã£ãŠã次ã®æé ãå®è¡ãããŸãã
- 2 * nãã€ãã®ã¡ã¢ãªãå²ãåœãŠãããŸãã æãå¯èœæ§ãé«ãã®ã¯ãçŸåšã®ãã¯ãã«ã®ã¢ãã¬ã¹ç©ºéã«å²ãåœãŠãããããšã§ãïŒããããããã°ããããŠããããåŸãã«ãªãå¯èœæ§ããããŸãïŒã
- å€ããã¯ãã«ã¯æ°ãããã¯ãã«ã®å é ã«ã³ããŒãããŸãã
- æ°ãããã¯ã¿ãŒã«æ°ããèŠçŽ ãè¿œå ãããŸãã
- å€ããã¯ã¿ãŒãåé€ãããŸãã
ãã®æ°ãããã¯ãã«ãå¢ããå Žåã¯ã4 * nã«å¢ããã次ã«8 * nã«å¢ãããŸãã åã¹ãããkã§ïŒ2 ^ kïŒ* nãã€ãã®ã¡ã¢ãªãå¿ èŠã§ãããããã¯åžžã«åèšïŒ2 ^ïŒk-1ïŒïŒãã倧ãããããã¡ã¢ãªã®æ°ããå²ãåœãŠããšã«ãçµåããããã¹ãŠã®ä»¥åã®ãã¯ãã«ãããå€ãå¿ èŠã«ãªããŸã* n +ïŒ2 ^ïŒk-2ïŒïŒ* n ... +ïŒïŒ2 ^ 0ïŒïŒ* n
ã€ãŸã ã¡ã¢ãªãããŒãžã£ã¯ã以åã«ãã¯ã¿ãŒã«å²ãåœãŠãããã¡ã¢ãªãåå©çšããããšã¯ã§ãããåžžã«ãæ°ãããã¡ã¢ãªã®å²ãåœãŠã匷å¶ãããŸãã ãããæ¬åœã«ç§ãã¡ãæãã§ããããšã§ã¯ãªãã§ãããã ãããã£ãŠã2æªæºã®æ°å€ã¯ããã¯ã¿ãŒã®å®¹éãå¢ããããã€ãã®ã¹ãããã§ãæ°ããã¢ãã¬ã¹ç©ºéã«ã¡ã¢ãªãŒãå²ãåœãŠãããšã¯ã§ããŸãããããã®ãã¯ã¿ãŒã«æ¢ã«å²ãåœãŠãããŠããã¡ã¢ãªãŒãåå©çšããããšãä¿èšŒããŸãã
ã°ã©ãã¯ã容éå¢å å®æ°ã1.5ïŒéè²ã®ç·ïŒã®å Žåããã¯ãã«ãå¢å ãã4çªç®ã®ã¹ãããã®åŸã3çªç®ã®åŸã«1.45ïŒèµ€ãç·ïŒã2çªç®ã®åŸã«1.3ïŒé»ãç·ïŒã§ã¡ã¢ãªãåå©çšã§ããããšã瀺ããŠããŸãã
ãã¡ãããäžèšã®ã°ã©ãã¯ãã¡ã¢ãªã¢ãã±ãŒã¿ãå®éã«ã©ã®ããã«æ©èœãããã«ã€ããŠããã€ãã®åçŽåãè¡ã£ãŠããŸãããéžæãããgccå®æ°2ãçè«çã«ææªã®å Žåã¯å€ãããŸããã fbvectorã¯å®æ°1.5ã䜿çšããŸãã ãŸããfbvectorãjemallocãšçžäºäœçšããæ¹æ³ã®ãããå°ããªãã¯ãã«ãµã€ãºã§ã®ããã©ãŒãã³ã¹ã«ã¯åœ±é¿ããŸããã
jemallocãšã®çžäºäœçš
ã»ãšãã©ãã¹ãŠã®ææ°ã®ã¡ã¢ãªã¢ãã±ãŒã¿ã¯ãã¡ã¢ãªç®¡çã®ãªãŒããŒããããæå°éã«æãããšåæã«ãå²ãåœãŠãããå°ããªãµã€ãºã®ãããã¯ã§è¯å¥œãªããã©ãŒãã³ã¹ãæäŸããããã«éžæãããç¹å®ã®éã§ã¡ã¢ãªãå²ãåœãŠãŸãã ããšãã°ãã¢ãã±ãŒã¿ãŒã¯æ¬¡ã®ãããªãããã¯ãéžæã§ããŸãïŒ32ã64ã128ã... 4096ã1 MBãŸã§ã®ããŒãžãµã€ãºã«æ¯äŸããŠã512 KBã®å¢åãªã©ã
äžã§èª¬æããããã«ãstd :: vectorã«ã¯ã¡ã¢ãªã®éååãå¿ èŠã§ãã 次ã®ã¯ã©ã³ã¿ã ã®ãµã€ãºã¯ããã¯ãã«ã®çŸåšã®å®¹éãšæé·å®æ°ã«åºã¥ããŠæ±ºå®ãããŸãã ãããã£ãŠãã»ãŒãã¹ãŠã®ç¬éã«ããã¯ã¿ãŒã®æåŸã«çŸåšäœ¿çšãããŠããªãäžå®éã®ã¡ã¢ãªãããããã®çŽåŸã«ã¢ãã±ãŒã¿ã«ãã£ãŠå²ãåœãŠãããã¡ã¢ãªãããã¯ã®çµããã«äžå®éã®æªäœ¿çšã¡ã¢ãªããããŸãã çµè«èªäœã¯ããã¯ãã«ã¢ãã±ãŒã¿ãŒãšå ±æã¡ã¢ãªã¢ãã±ãŒã¿ãŒã®çµåã«ããRAMã®æ¶è²»ãæé©åã§ããããšã瀺åããŠããŸãããã¯ãã«ã¯ã¢ãã±ãŒã¿ãŒã«ãçæ³çãªããµã€ãºã®ãããã¯ãèŠæ±ããã¢ãã±ãŒã¿ãŒã«ãã£ãŠå²ãåœãŠããããããã¯ã®æªäœ¿çšã¡ã¢ãªãŒãåé€ã§ããããã§ãã ãŸããäžè¬ã«ããã¯ãã«ãšã¢ãã±ãŒã¿ãŒã«ããã¡ã¢ãªã®å²ãåœãŠã®äžè¬çãªæŠç¥ã¯ãäž¡æ¹ã®æ£ç¢ºãªå®è£ ãç¥ã£ãŠããã°ãããè¯ãããããšãã§ããŸãã ããã¯ãfbvectorãè¡ãããšãšãŸã£ããåãã§ããjemallocã®äœ¿çšãèªåçã«æ±ºå®ããã¡ã¢ãªå²ãåœãŠã¢ã«ãŽãªãºã ã«åãããŠèª¿æŽããŸãã
äžéšã®ã¡ã¢ãªã¢ãã±ãŒã¿ã¯ãã€ã³ãã¬ãŒã¹å²ãåœãŠããµããŒãããŠããŸããïŒãã ããã»ãšãã©ã®ã¡ã¢ãªã¢ãã±ãŒã¿ã¯ãµããŒãããŠããŸãïŒã ããã¯ã以åã«å²ãåœãŠãããã¡ã¢ãªãåå©çšããããæ°ããã¡ã¢ãªãå²ãåœãŠãããããã«ããŒã¿ãã³ããŒããŠå€ãã¡ã¢ãªã解æŸãããã決å®ããreallocïŒïŒé¢æ°ã®èšèšãããŸãæåããŠããªãããã§ãã ãã®ã¡ã¢ãªå²ãåœãŠã®å¶åŸ¡ã®æ¬ åŠã¯ãC ++ã®newæŒç®åãšstdïŒallocatorã®åäœã®äž¡æ¹ã«åœ±é¿ããŸãã ããã¯ããã©ãŒãã³ã¹ã«é倧ãªææãäžããŸããã€ã³ãã¬ãŒã¹å±éã¯éåžžã«å®äŸ¡ã§ãããããã¡ã¢ãªæ¶è²»ãå¢ããããã®æ»æçãªæŠç¥ãã¯ããã«å°ãªãããã§ãã
ãªããžã§ã¯ãã®å²ãåœãŠ
C ++ãªããžã§ã¯ããã¡ã¢ãªã«é 眮ããéã®éèŠãªåé¡ã®1ã€ã¯ãããã©ã«ãã§ã¯äžåãšèŠãªãããããšã§ãã ã¿ã€ãã®ãªããžã§ã¯ãã¯ç§»åå¯èœã§ãããšèŠãªããããããåäœã®ã³ããŒã«ãã£ãŠå¥ã®ã¡ã¢ãªäœçœ®ã«ã³ããŒã§ããåæã«æ°ããå Žæã§ãªããžã§ã¯ãã¯ãã®æå¹æ§ãšå®å šæ§ãå®å šã«ä¿æããŸãã ããšãã°ãint32ã¯ç§»åå¯èœã§ããããã¯ã4ãã€ãã32ããã笊å·ä»ãæ°å€ã®ç¶æ ãå®å šã«èšè¿°ããŠããããããããã®4ãã€ããå¥ã®ã¡ã¢ãªäœçœ®ã«ã³ããŒãããšããã®ã¡ã¢ãªãããã¯ãå®å šã«åãæ°å€ãšããŠå®å šã«è§£éã§ããããã§ãã
ãªããžã§ã¯ãã移åäžå¯èœã§ãããšããC ++ã®ä»®å®ã¯ããã¹ãŠã®äººã«å®³ãåãŒããç©è°ãéžãããã€ãã®ã¢ãŒããã¯ãã£äžã®åé¡ã®ããã«ã®ã¿ããããŸãã ãªããžã§ã¯ãã®ç§»åã«ã¯ãæ°ãããªããžã§ã¯ãã®ã¡ã¢ãªã®å²ãåœãŠãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®åŒã³åºããå€ããªããžã§ã¯ãã®ç Žæ£ãå«ãŸããŸãã ããã¯ããŸãå¿«é©ã§ã¯ãªããäžè¬çã«åžžèã«åããŸãã ãã£ããã³ãã«ãŒããšæççãªãšã€ãªã¢ã³ã®çè«çãªäŒè©±ãæ³åããŠãã ããã
æççãªãšã€ãªã¢ã³ ïŒããã§ãããªãã®ãã®ãã¬ããŒã-ããã¯ã©ã®ããã«æ©èœããŸããïŒ
ãã«ãŒã ïŒåœŒã¯ç·ãé£ããŠè¡ããããå Žæã§ç©è³ªåã解é€ããå¥ã®å Žæã§ç©è³ªåãã
æççãªãšã€ãªã¢ã³ ïŒããŒã...ããã¯å®å šã§ããïŒ
Picard ïŒã¯ããæåã®ã¢ãã«ã®çå®ã¯å°ããªæ¬ é¥ã§ããã æåã¯ã人ã¯åçŽã«ã¯ããŒã³åãããæ°ããå Žæã§äœæãããŸããã ãã®åŸããã¬ããŒããªãã¬ãŒã¿ãŒã¯ãªãªãžãã«ãæåã§æ®åœ±ããå¿ èŠããããŸããã ãªãã©ã€ãšã³ã«èããŠãã ããã圌ã¯ãã®åœ¹è·ã§ã€ã³ã¿ãŒã³ãšããŠåããŠããŸããã å®è£ ã¯ããŸããšã¬ã¬ã³ãã§ã¯ãããŸããã§ããã
ïŒ ç¿»èš³è ã®ã¡ã¢ ïŒã©ããããããã¯ææ°ã®C ++æšæºã§ã ãŒãã³ã³ã¹ãã©ã¯ã¿ãŒãå°å ¥ãããåã«æžãããããã§ãïŒã
å®éããªããžã§ã¯ãã®äžéšã®ã¿ãæ¬åœã«åããªããã®ã§ãã
- å€éšãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒãæã€ãã®
- ä»ã®ãªããžã§ã¯ãããã®ãã€ã³ã¿ãŒãæããã®
æåã®ã¿ã€ãã®ãªããžã§ã¯ãã¯ãæå°éã®ã³ã¹ãã§ãã€ã§ãããçŽãããšãã§ããŸãã 2çªç®ã®ã¿ã€ãã®ãªããžã§ã¯ãã¯ãnewæŒç®åã§äœæããããdeleteæŒç®åã§åé€ãããããªãã§ãã ãããã¹ããŒããã€ã³ã¿ãŒã§å¶åŸ¡ããå¿ èŠããããåé¡ã¯ãããŸããã
移åå¯èœãªãªããžã§ã¯ãã¯ãstd :: vectorã«ãšã£ãŠéåžžã«èå³æ·±ããã®ã§ãããã¯ãã«å ã§ãªããžã§ã¯ãã移åããæ¹æ³ã«é¢ããç¥èã¯ããããã®ãªããžã§ã¯ãã®ãã¯ãã«ååžã®å¹çã«å€§ãã圱é¿ããããã§ãã äžèšã®ãã«ã«ãã移åã«ãŒãã®ä»£ããã«ããªããžã§ã¯ããåçŽãªmemcpyãŸãã¯memmoveã§ç§»åã§ããŸãã ããšãã°ããã¯ã¿ãŒ<vector>ãŸãã¯ãã¯ã¿ãŒ<hash_map <intãstring >>ãªã©ã®ã¿ã€ãã®æäœã¯ãå€§å¹ ã«é«éåã§ããŸãã
ãªããžã§ã¯ãã®å®å šãªç§»åããµããŒãããããã«ãfbvectorã¯ãfolly / Traits.hãã§å®çŸ©ãããŠããç¹æ§folly :: IsRelocatableã䜿çšããŸãã ããã©ã«ãã§ã¯ãfolly :: IsRelocatable :: valueã¯ä¿å®çã«falseãè¿ããŸãã ãŠã£ãžã§ããã¿ã€ããåé 眮å¯èœã§ããããšã確å®ã«ããã£ãŠããå Žåã¯ããã®ã¿ã€ãã宣èšããçŽåŸã«ä»¥äžãè¿œå ã§ããŸãã
namespace folly { struct IsRelocatable<Widget> : boost::true_type {}; }
ããããªããšãfbvectorã¯BOOST_STATIC_ASSERTã§ã³ã³ãã€ã«ãããŸããã
è¿œå ã®å¶é
ãåçŽãªãåïŒããæ£ç¢ºã«ã¯ãåçŽãªä»£å ¥æäœãæã€åïŒãŸãã¯äŸå€ãã¹ããŒããªãã³ã³ã¹ãã©ã¯ã¿ãŒãæã€åïŒããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãŒãã¹ããŒããªãïŒã«å¯ŸããŠããããã€ãã®æ¹åãå¯èœã§ãã 幞ããªããšã«ããããã®ç¹æ§ã¯C ++æšæºã«æ¢ã«ååšããŠããŸãïŒãŸãããŸãã¯Boostã«ãããŸãïŒã èŠçŽãããšãfbvectorã䜿çšããã«ã¯ããŠã£ãžã§ããã¿ã€ãã次ã®æ¡ä»¶ãæºããå¿ èŠããããŸãã
BOOST_STATIC_ASSERT( IsRelocatable::value && (boost::has_trivial_assign::value || boost::has_nothrow_constructor::value));
ãããã®ã¿ã€ãã¯å®éã«ã¯éåžžã«äŒŒãŠããŸã-æ¡ä»¶ã®äžéšãæºãããä»ã®éšåã«éåããã¯ã©ã¹ãæ§ç¯ããã®ã¯ããªãå°é£ã§ãïŒå®éã«äžçæžåœåªåããªãéãïŒã fbvectorã¯ããããã®åçŽãªæ¡ä»¶ã䜿çšããŠãåºæ¬çãªãã¯ãã«æäœïŒpush_backãinsertãresizeïŒã®ã³ããŒæäœãæå°åããŸãã
Traits.hã®fbvectorãšã®äºææ§ã®åãã§ãã¯ãç°¡çŽ åããããã«ããã¯ãFOLLY_ASSUME_FBVECTOR_COMPATIBLEããããŸãã
ãã®ä»
fbvectorã®å®è£ ã¯ãã¡ã¢ãªå¹çã«éç¹ã眮ããŠããŸãã memcpyã¯gccã®çµã¿èŸŒã¿é¢æ°ã§ã¯ãªãã倧ããªããŒã¿ãããã¯ã«å¯ŸããŠå®å šã«æ©èœããªããããå°æ¥ãã³ããŒã®å®è£ ãæ¹åãããå¯èœæ§ããããŸãã jemallocãšã®çžäºäœçšã®æ¹åãå¯èœã§ãã
fbvectorã䜿çšããŠé 匵ã£ãŠãã ããã