Pythonã§äœãèµ·ãã£ãŠããããç¥ãããšã§ããã®èšèªã®åäœã®äžéšãããããç解ããããšãã§ããŸãã ããã«ããã䜿çšãããã®èšèªã®å®è£ å ã§è¡ãããŠããèšå€§ãªäœæ¥ã«æè¬ããããã°ã©ã ãå¿ èŠãªæ¹æ³ã§æ£ç¢ºã«åäœããããã«ãªãããšãé¡ã£ãŠããŸãã
èšæ¶ã¯ç©ºã®æ¬ã§ã
ã³ã³ãã¥ãŒã¿ãŒã®ã¡ã¢ãªãŒã¯ããããæ±ãæåã®æ®µéã§ãçç·šå°èª¬ã察象ãšãã空ã®æ¬ã®åœ¢ã§è¡šãããšãã§ããŸãã ãããŸã§ã®ãšããããã®ããŒãžã«ã¯äœããããŸããããããã«ç©èªã®èè ãç»å Žããããããããã®æ¬ã«èªåã®ç©èªãæžããããšæã£ãŠããŸãã
ããã¹ããŒãªãŒãå¥ã®ã¹ããŒãªãŒã®äžã«æžãããšã¯ã§ããªããããèè ã¯æ¬ã®ã©ã®ããŒãžã«æžããã«ã€ããŠæ³šæããå¿ èŠããããŸãã äœããæžãåã«ã圌ãã¯ç·šéé·ãšçžè«ããŸãã 圌ã¯ãèè ãã¹ããŒãªãŒãæ£ç¢ºã«èšé²ã§ããå Žæã決å®ããŸãã
ç§ãã¡ã話ããŠããæ¬ã¯ããªãåããåºåã£ãŠããã®ã§ããã®äžã®ç©èªã®å€ãã¯ãã§ã«æ代é ãã§ãã 誰ãç©èªãèªãã§ããªãããäœåã§èšåããŠããªãå Žåããã®ç©èªã¯æ¬ããåé€ãããæ°ããç©èªã®äœå°ãã§ããŸãã
äžè¬ã«ãã³ã³ãã¥ãŒã¿ãŒã®ã¡ã¢ãªã¯ãã®ãããªæ¬ã«éåžžã«äŒŒãŠãããšèšããŸãã å®éãåºå®é·ã®é£ç¶ããã¡ã¢ãªãããã¯ã¯ããŒãžãšåŒã°ããããšããããããã¡ã¢ãªãšæ¬ãæ¯èŒããããšã¯éåžžã«æåãããšèããŠããŸãã
ç©èªãæ¬ã«æžãèè ã¯ãã¡ã¢ãªã«ããŒã¿ãä¿åããå¿ èŠãããããŸããŸãªã¢ããªã±ãŒã·ã§ã³ãŸãã¯ããã»ã¹ã§ãã èè ãå·çã§ããæ¬ã®ããŒãžã決å®ããç·šéé·ã¯ãã¡ã¢ãªç®¡çãæ±ãã¡ã«ããºã ã§ãã ãããŠãæ¬ããå€ãç©èªãåãé€ããæ°ããç©èªã®ããã®ã¹ããŒã¹ãäœã人ã¯ãã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã¡ã«ããºã ãšæ¯èŒããããšãã§ããŸãã
ã¡ã¢ãªç®¡çïŒéããããã°ã©ã ãžã®é
ã¡ã¢ãªç®¡çã¯ããã»ã¹ã§ãããå®è£ äžã«ããã°ã©ã ãã¡ã¢ãªã«ããŒã¿ãæžã蟌ã¿ãããããããŒã¿ãèªã¿åããŸãã ã¡ã¢ãªãããŒãžã£ã¯ãã¢ããªã±ãŒã·ã§ã³ãã¡ã¢ãªå ã®ããŒã¿ãæ£ç¢ºã«é 眮ã§ããå Žæã決å®ãããšã³ãã£ãã£ã§ãã ã¢ããªã±ãŒã·ã§ã³ã«å²ãåœãŠãããšãã§ããã¡ã¢ãªãã©ã°ã¡ã³ãã®æ°ã¯ç¡éã§ã¯ãªããããæžç±ã®ããŒãžæ°ãç¡éã§ã¯ãªãã®ãšåæ§ã«ãã¢ããªã±ãŒã·ã§ã³ãæäŸããã¡ã¢ãªãããŒãžã£ãŒã¯ã空ãã¡ã¢ãªãã©ã°ã¡ã³ããèŠã€ããŠã¢ããªã±ãŒã·ã§ã³ã«æäŸããå¿ èŠããããŸãã ã¡ã¢ãªãã¢ããªã±ãŒã·ã§ã³ã«å²ãåœãŠããããã®ããã»ã¹ã¯ãã¡ã¢ãªå²ãåœãŠãšåŒã°ããŸãã
äžæ¹ãäžéšã®ããŒã¿ãäžèŠã«ãªã£ãå Žåããã®ããŒã¿ãåé€ãããã€ãŸããå æããŠããã¡ã¢ãªã解æŸããããšãã§ããŸãã ããããèšæ¶ã«ã€ããŠè©±ããšãããæŸåºãããã³ã解æŸããšã¯æ£ç¢ºã«äœã§ããïŒ
ã³ã³ãã¥ãŒã¿ãŒã®ã©ããã«ãPythonããã°ã©ã ãåäœäžã«äœ¿çšããããŒã¿ãä¿åããç©çããã€ã¹ããããŸãã Pythonãªããžã§ã¯ããç©çã¡ã¢ãªã«æ ŒçŽãããåã«ãã³ãŒãã¯æœè±¡åã®å€ãã®ã¬ã€ã€ãŒãééããå¿ èŠããããŸãã
ããŒããŠã§ã¢ïŒRAMãããŒããã£ã¹ã¯ãªã©ïŒã®äžã«ãããã®ãããªäž»èŠãªã¬ã€ã€ãŒã®1ã€ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ïŒOSïŒã§ãã ã¡ã¢ãªããããŒã¿ãèªã¿åããã¡ã¢ãªã«ããŒã¿ãæžã蟌ãèŠæ±ãå®è¡ããŸãïŒãŸãã¯å®è¡ãæåŠããŸãïŒã
OSã®äžã«ã¯ãPythonå®è£ ã®1ã€ã§ããã¢ããªã±ãŒã·ã§ã³ããããŸãïŒOSã®äžéšã§ãããã python.orgããããŠã³ããŒãããããœãããŠã§ã¢ããã±ãŒãžã®å ŽåããããŸãïŒã ã¡ã¢ãªç®¡çãåŠçããPythonã³ãŒãã®åäœãä¿èšŒããã®ã¯ããã®ãœãããŠã§ã¢ããã±ãŒãžã§ãã ãã®èšäºã®çŠç¹ã¯ãPythonãã¡ã¢ãªã管çããããã«äœ¿çšããã¢ã«ãŽãªãºã ãšããŒã¿æ§é ã«ãããŸãã
Pythonãªãã¡ã¬ã³ã¹å®è£
ãªãã¡ã¬ã³ã¹Pythonå®è£ ã¯CPythonãšåŒã°ããŸãã Cã§æžãããŠããŸããæåã«èãããšããæåéãäžå®ã§ããã å¥ã®èšèªã§æžãããããã°ã©ãã³ã°èšèªã§ããïŒ ãŸããå®éã«ã¯ãããã¯å®å šã«çå®ã§ã¯ãããŸããã
ãã®ããã¥ã¡ã³ãã§ã¯ãPythonã®ä»æ§ã«ã€ããŠå¹³æãªè±èªã§èª¬æããŠããŸã ã ãã ãããã®ä»æ§ã ãã§ã¯ããã¡ããPythonã§èšè¿°ãããã³ãŒãã¯å®è¡ã§ããŸããã ãããè¡ãã«ã¯ããã®ä»æ§ã®ã«ãŒã«ã«åŸã£ãŠãPythonã§èšè¿°ãããã³ãŒãã解éã§ãããã®ãå¿ èŠã§ãã
ããã«ãã³ã³ãã¥ãŒã¿ãŒäžã§è§£éãããã³ãŒããå®è¡ã§ãããã®ãå¿ èŠã§ãã ãªãã¡ã¬ã³ã¹Pythonå®è£ ã¯ããããã®äž¡æ¹ã®ã¿ã¹ã¯ã解決ããŸãã ã³ãŒããåœä»€ã«å€æããä»®æ³ãã·ã³ã§å®è¡ããŸãã
ä»®æ³ãã·ã³ã¯ãã·ãªã³ã³ãéå±ããã®ä»ã®çŽ æã§äœãããéåžžã®ã³ã³ãã¥ãŒã¿ãŒã«äŒŒãŠããŸããããœãããŠã§ã¢ã§å®è£ ãããŠããŸãã 圌ãã¯éåžžãåºæ¬çãªåœä»€ã®åŠçã§å¿ããã ã¢ã»ã³ãã©ãŒã§æžãããåœä»€ã«äŒŒãŠããŸãã
Pythonã¯ã€ã³ã¿ãŒããªã¿ãŒèšèªã§ãã Pythonã§èšè¿°ãããã³ãŒãã¯ãã³ã³ãã¥ãŒã¿ãŒã䜿çšããã®ã«äŸ¿å©ãªäžé£ã®åœä»€ããããããã€ãã³ãŒãã«ã³ã³ãã€ã«ãããŸã ã ãããã®æ瀺ã¯ãããã°ã©ã ã®å®è¡æã«ä»®æ³ãã·ã³ã«ãã£ãŠè§£éãããŸãã
æ¡åŒµåã
.pyc
ãŸãã¯
__pycache__
ãã©ã«ããŒã®ãã¡ã€ã«ãèŠãããšã¯
__pycache__
ãŸããïŒ ãããã«ã¯ãä»®æ³ãã·ã³ã«ãã£ãŠè§£éãããåããã€ãã³ãŒããå«ãŸããŠããŸãã
CPythonã®ä»ã«ããPythonã®å®è£ ãååšããããšã«æ³šæããããšãéèŠã§ãã ããšãã°ã IronPythonã䜿çšããå Žåã Pythonã³ãŒãã¯Microsoft CLRã¹ããŒãã¡ã³ãã«ã³ã³ãã€ã«ãããŸãã Jythonã§ã¯ãã³ãŒãã¯Javaãã€ãã³ãŒãã«ã³ã³ãã€ã«ãããJavaä»®æ³ãã·ã³ã§å®è¡ãããŸãã Pythonã®äžçã«ã¯PyPyã®ãããªãã®ããããŸãããå¥ã®èšäºã«å€ããã®ã§ãããã§ããã«ã€ããŠèšåããŸãã
ãã®èšäºã§ã¯ãPythonãªãã¡ã¬ã³ã¹å®è£ ã§ããCPythonã§ã®ã¡ã¢ãªç®¡çã¡ã«ããºã ã®åäœã«çŠç¹ãåœãŠãŸãã
ããã§èª¬æããããšã®ã»ãšãã©ã¯Pythonã®æ°ããããŒãžã§ã³ã«ãåœãŠã¯ãŸããŸãããå°æ¥ã¯ç¶æ³ãå€ããå¯èœæ§ãããããšã«æ³šæããŠãã ããã ãããã£ãŠããã®èšäºã§ã¯ãå·çæç¹ã®ææ°ããŒãžã§ã³ã®PythonïŒ Python 3.7ïŒã«çŠç¹ãåãããŠããããšã«æ³šæããŠãã ããã
ãããã£ãŠãCPythonãœãããŠã§ã¢ããã±ãŒãžã¯Cã§èšè¿°ãããŠãããPythonãã€ãã³ãŒãã解éããŸãã ããã¯ã¡ã¢ãªç®¡çãšäœã®é¢ä¿ããããŸããïŒ å®éãã¡ã¢ãªç®¡çã«äœ¿çšãããã¢ã«ãŽãªãºã ãšããŒã¿æ§é ã¯ããã§ã«è¿°ã¹ãããã«ãCã§èšè¿°ãããCPythonã³ãŒãã«ååšããŸããã¡ã¢ãªç®¡çãPythonã§ã©ã®ããã«æ©èœããããç解ããã«ã¯ããŸãCPythonã«ã€ããŠå°ãç解ããå¿ èŠããããŸãã
CPythonãèšè¿°ãããŠããCèšèªã¯ããªããžã§ã¯ãæåããã°ã©ãã³ã°ããã€ãã£ãã«ãµããŒãããŠããŸããã ãã®ãããCPythonã³ãŒãã§ã¯å€ãã®èå³æ·±ãã¢ãŒããã¯ãã£ãœãªã¥ãŒã·ã§ã³ã䜿çšãããŠããŸãã
Pythonã®ãã¹ãŠããªããžã§ã¯ãã§ããã
int
ã
str
ãããªããªããã£ããªããŒã¿åã§ããããšãèããããšããããããããŸããã ãããŠãããã¯å®éã«CPythonã®èšèªå®è£ ã¬ãã«ã®å Žåã§ãã
PyObject
ãšåŒã°ããæ§é ããããCPythonã§äœæããããªããžã§ã¯ãã«ãã£ãŠäœ¿çšãããŸãã
æ§é ã¯ãããŸããŸãªã¿ã€ãã®ããŒã¿ãã°ã«ãŒãåã§ããè€åããŒã¿ã¿ã€ãã§ãã ããããªããžã§ã¯ãæåããã°ã©ãã³ã°ãšæ¯èŒãããšãæ§é ã¯å±æ§ã¯ãããã¡ãœããã¯ãªãã¯ã©ã¹ã«äŒŒãŠããŸãã
PyObject
ã¯ããã¹ãŠã®Pythonãªããžã§ã¯ãã®ç¥å ã§ãã ãã®æ§é ã«ã¯ã2ã€ã®ãã£ãŒã«ãã®ã¿ãå«ãŸããŸãã
-
ob_refcnt
åç §ã«ãŠã³ã¿ãŒã -
ob_type
ã¯ãå¥ã®åãžã®ãã€ã³ã¿ãŒã§ãã
åç §ã«ãŠã³ã¿ã¯ãã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã¡ã«ããºã ãå®è£ ããããã«äœ¿çšãããŸãã å¥ã®
PyObject
ãã£ãŒã«ãã¯ãç¹å®ã®ã¿ã€ãã®ãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒã§ãã ãã®åã¯ãPythonãªããžã§ã¯ããèšè¿°ããå¥ã®æ§é ã§è¡šãããŸãïŒããšãã°ã
dict
int
ãŸãã¯
int
ããããšãã§ããŸãïŒã
åãªããžã§ã¯ãã«ã¯ããã®ãããªãªããžã§ã¯ãã«åºæã®ç¬èªã®ã¡ã¢ãªå²ãåœãŠã¡ã«ããºã ãããããã®ãªããžã§ã¯ããæ ŒçŽããããã«å¿ èŠãªã¡ã¢ãªãååŸããæ¹æ³ãç¥ã£ãŠããŸãã ããã«ãåãªããžã§ã¯ãã«ã¯ã¡ã¢ãªã解æŸããç¬èªã®ã¡ã«ããºã ããããäžèŠã«ãªã£ãã¡ã¢ãªãã解æŸãããŸãã
ãã ããã¡ã¢ãªã®å²ãåœãŠãšè§£æŸã«é¢ããããããã¹ãŠã®äŒè©±ã«ã¯ã1ã€ã®éèŠãªèŠçŽ ãããããšã«æ³šæããŠãã ããã å®éãã³ã³ãã¥ãŒã¿ãŒã®ã¡ã¢ãªã¯å ±æãªãœãŒã¹ã§ãã åæã«ã2ã€ã®ç°ãªãããã»ã¹ãåãã¡ã¢ãªé åã«äœããæžã蟌ãããšãããšãäœãæªãããšãèµ·ããå¯èœæ§ããããŸãã
éèš³è ã°ããŒãã«ããã¯
ã°ããŒãã«ã€ã³ã¿ãŒããªã¿ãŒããã¯ïŒGILïŒã¯ãã¡ã¢ãªãªã©ã®å ±æã³ã³ãã¥ãŒã¿ãŒãªãœãŒã¹ãæäœãããšãã«çºçããäžè¬çãªåé¡ã®è§£æ±ºçã§ãã 2ã€ã®ã¹ã¬ãããåããªãœãŒã¹ãåæã«å€æŽããããšãããšããããã¯äºãã«ãè¡çªãããå¯èœæ§ããããŸãã çµæã¯æ··ä¹±ã«ãªããã¹ã¬ããã®1ã€ã¯ãããç®æããŠãããã®ãéæããŸããã
åã³æ¬ã®äŸãã«æ»ããŸãããã 2人ã®èè ããä»åºŠã¯ã¡ã¢ãåãçªã ãšæ±ºãããšæ³åããŠãã ããã ãããã圌ãã¯åãããŒãžã«åæã«èšé²ããããšã決ããŸããã
ãããã®ããããã¯ãä»ã圌ã®ç©èªãæžã蟌ãããšããŠãããšããäºå®ã«æ³šæãæããŸããã äžç·ã«åœŒãã¯ããŒãžã«ããã¹ããæžãå§ããŸãã ãã®çµæã2ã€ã®ã¹ããŒãªãŒãèšé²ãããäžæ¹ãä»æ¹ã®äžã«éããããããŒãžãå®å šã«èªããªããªããŸãã
ãã®åé¡ã®è§£æ±ºçã®1ã€ã¯ãç¹å®ã®ã¹ã¬ãããåäœããŠããå ±æãªãœãŒã¹ããããã¯ããåäžã®ã°ããŒãã«ã€ã³ã¿ãŒããªã¿ãŒã¡ã«ããºã ã§ãã ãã®äŸã§ã¯ãããã¯æ¬ã®ããŒãžãããããã¯ããããã¡ã«ããºã ãã§ãã ãã®ãããªã¡ã«ããºã ã«ããã2人ã®èè ãåãããŒãžã«åæã«ããã¹ããæžã蟌ããšããäžèšã®ç¶æ³ããªããªããŸãã
Pythonã®GILã¡ã«ããºã ã¯ãã€ã³ã¿ãŒããªã¿ãŒå šäœããããã¯ããããšã§ãããå®çŸããŸãã ãã®çµæãçŸåšã®ã¹ã¬ããã«å¹²æžãããã®ã¯äœããããŸããã ãŸããCPythonãã¡ã¢ãªãæäœããå ŽåãGILã䜿çšããŠããã®äœæ¥ãå®å šãã€å¹ççã«è¡ãããããã«ããŸãã
ãã®ã¢ãããŒãã«ã¯é·æãšçæããããGILã¯Pythonã³ãã¥ããã£ã§æ¿ããè°è«ã®å¯Ÿè±¡ãšãªã£ãŠããŸãã GILã®è©³çŽ°ã«ã€ããŠã¯ã ãã®è³æãã芧ãã ããã
ã¬ããŒãžã³ã¬ã¯ã·ã§ã³
æ¬ã®äŸãã«æ»ã£ãŠããã®æ¬ã«èšé²ãããŠããããã€ãã®ç©èªã絶æçã«ââå€ããªã£ãŠãããšæ³åããŠã¿ãŸãããã 誰ãããããèªãã ãã誰ãèšåãããããŸããã ãããŠããã誰ã圌ãã®äœåã®äžéšã®è³æãèªãã ãåç §ãããããªããã°ããã®è³æã¯å»æ£ãããæ°ããããã¹ãã®ããã®ã¹ããŒã¹ãäœãããšãã§ããŸãã
ãããã®å€ãå¿ããããç©èªã¯ãåç §ã«ãŠã³ãããŒãã®Pythonãªããžã§ã¯ããšæ¯èŒã§ããŸãã ãããã¯ã
PyObject
æ§é ã«ã€ããŠèª¬æãããšãã«èª¬æãããã®ãšåãã«ãŠã³ã¿ãŒã§ãã
åç §ã«ãŠã³ã¿ã¯ãããã€ãã®çç±ã§å¢å ããŸãã ããšãã°ãããå€æ°ã«æ ŒçŽãããŠãããªããžã§ã¯ããå¥ã®å€æ°ã«æžã蟌ãŸããå Žåãã«ãŠã³ã¿ãŒã¯ã€ã³ã¯ãªã¡ã³ããããŸãã
numbers = [1, 2, 3] # = 1 more_numbers = numbers # = 2
ãªããžã§ã¯ããåŒæ°ãšããŠé¢æ°ã«æž¡ãããå Žåã§ãå¢å ããŸãã
total = sum(numbers)
ãŸããåç §ã«ãŠã³ã¿ã®æ°ãå¢å ããç¶æ³ã®å¥ã®äŸã次ã«ç€ºããŸãã ããã¯ããªããžã§ã¯ãããªã¹ãã«å«ãŸããŠããå Žåã«çºçããŸãã
matrix = [numbers, numbers, numbers]
Pythonã§ã¯ãããã°ã©ãã
sys
ã¢ãžã¥ãŒã«ã䜿çšããŠç¹å®ã®ãªããžã§ã¯ãã®åç §ã«ãŠã³ãã®çŸåšã®å€ãèŠã€ããããšãã§ããŸãã ãã®ããã«ã次ã®æ§æã䜿çšãããŸãã
sys.getrefcount(numbers)
getfefcount()
ãã
getfefcount()
ããªããžã§ã¯ãã
getfefcount()
ã¡ãœããã«
getfefcount()
ãã«ãŠã³ã¿ãŒå€ã1å¢å ããããšãèŠããŠããå¿ èŠããããŸãã
ãããã«ããããªããžã§ã¯ãããŸã ã³ãŒãã®ã©ããã§äœ¿çšãããŠããå Žåããã®åç §ã«ãŠã³ã¿ãŒã¯0ãã倧ãããªããŸããã«ãŠã³ã¿ãŒã®å€ã0ã«äžãããšããªããžã§ã¯ããå æããŠããã¡ã¢ãªãã解æŸãããç¹å¥ãªæ©èœãåããŸãã ãã®ã¡ã¢ãªã¯ä»ã®ãªããžã§ã¯ãã§äœ¿çšã§ããŸãã
ããã§ããã¡ã¢ãªã®è§£æŸããšã¯äœããä»ã®ãªããžã§ã¯ãããã®ã¡ã¢ãªã䜿çšããæ¹æ³ã«ã€ããŠè³ªåããŸãã ãããã®è³ªåã«çããããã«ãCPythonã®ã¡ã¢ãªç®¡çã¡ã«ããºã ã«ã€ããŠè©±ããŸãããã
CPythonã®ã¡ã¢ãªç®¡çã¡ã«ããºã
ããã§ãCPythonã®ã¡ã¢ãªã¢ãŒããã¯ãã£ãšãããã§ã®ã¡ã¢ãªç®¡çã®æ¹æ³ã«ã€ããŠèª¬æããŸãã
ãã§ã«è¿°ã¹ãããã«ãCPythonãšç©çã¡ã¢ãªã®éã«ã¯æœè±¡åã®ããã€ãã®å±€ããããŸãã ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ç©çã¡ã¢ãªãæœè±¡åããã¢ããªã±ãŒã·ã§ã³ã䜿çšã§ããä»®æ³ã¡ã¢ãªã¬ã€ã€ãŒãäœæããŸãïŒããã¯Pythonã«ãåœãŠã¯ãŸããŸãïŒã
ç¹å®ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ä»®æ³ã¡ã¢ãªãããŒãžã£ãŒã¯ãPythonããã»ã¹ã«ã¡ã¢ãªãå²ãåœãŠãŸãã 次ã®ç»åã®æãç°è²ã®é åã¯ãPythonããã»ã¹ã«å±ããã¡ã¢ãªã®äžéšã§ãã
CPythonã䜿çšããã¡ã¢ãªé å
Pythonã¯ãå éšäœ¿çšã®ãããããã³ãªããžã§ã¯ããžã®ã¡ã¢ãªã®å²ãåœãŠã«é¢é£ããªãããŒãºã®ããã«ãäžå®éã®ã¡ã¢ãªã䜿çšããŸãã å¥ã®ã¡ã¢ãªããªããžã§ã¯ãã®æ ŒçŽã«äœ¿çšãããŸãïŒãããã¯
int
ã
dict
ãªã©ã®åã®å€ã§ãïŒã ããã¯ç°¡ç¥å³ã§ããããšã«æ³šæããŠãã ããã å šäœåãç¥ãããå Žåã¯ã CPythonã®ãœãŒã¹ã³ãŒããèŠãŠãã ãããããã§ã¯ãç§ãã¡ã話ããŠãããã¹ãŠã®ããšãèµ·ãã£ãŠããŸãã
CPythonã«ã¯ããªããžã§ã¯ãã«ã¡ã¢ãªãå²ãåœãŠãæ©èœããããŸããããã¯ããªããžã§ã¯ããæ ŒçŽããããã®é åã«ã¡ã¢ãªãå²ãåœãŠã圹å²ãæãããŸãã æãèå³æ·±ãã®ã¯ããã®ã¡ã«ããºã ãæ©èœãããšãã§ãã ãªããžã§ã¯ãã«ã¡ã¢ãªãå¿ èŠãªå ŽåããŸãã¯ã¡ã¢ãªã解æŸããå¿ èŠãããå Žåã«åŒã³åºãããŸãã
éåžžã
list
ã
int
ãªã©ã®Pythonãªããžã§ã¯ããžã®ããŒã¿ã®è¿œå ãŸãã¯åé€ã«ã¯ã倧éã®æ å ±ã®åæåŠçã¯å«ãŸããŸããã ãããã£ãŠãã¡ã¢ãªå²ãåœãŠããŒã«ã®ã¢ãŒããã¯ãã£ã¯ãå°éã®ããŒã¿ã®åŠçã«æ³šç®ããŠæ§ç¯ãããŠããŸãã ããã«ããã®ããŒã«ã¯ã絶察ã«å¿ èŠã§ããããšãæããã«ãªããŸã§ã¡ã¢ãªãå²ãåœãŠãªãããã«ããŸãã
ãœãŒã¹ã³ãŒãã®ã³ã¡ã³ãã¯ãã¡ã¢ãªå²ãåœãŠããŒã«ãããŠãããŒãµã«mallocã®äžéšã§äœ¿çšããããã«èšèšãããå°ããªãããã¯çšã®é«éã§å°çšã®ã¡ã¢ãªå²ãåœãŠããŒã«ããšèª¬æããŠããŸãã ãã®å Žåã
malloc
ã¯ã¡ã¢ãªãå²ãåœãŠãããã«èšèšãããCã©ã€ãã©ãªé¢æ°ã§ãã
CPythonã§äœ¿çšãããã¡ã¢ãªå²ãåœãŠæŠç¥ã«ã€ããŠèª¬æããŸãããã ãŸãã3ã€ã®ãšã³ãã£ãã£-ãããããããã¯ïŒãããã¯ïŒãããŒã«ïŒããŒã«ïŒãã¢ãªãŒãïŒã¢ãªãŒãïŒãããã³ãããã®çžäºé¢ä¿ã«ã€ããŠèª¬æããŸãã
ã¢ãªãŒãã¯èšæ¶ã®æ倧ã®æçã§ãã ãããã¯ãã¡ã¢ãªã®ããŒãžã®å¢çã«é 眮ãããŸãã ããŒãžå¢çã¯ãåºå®é·ã¡ã¢ãªã®é£ç¶ãããã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ãã£ãŠäœ¿çšäžã«çµäºããå Žæã§ãã Pythonã¯ãã¡ã¢ãªã®æäœäžã«ãã·ã¹ãã ã¡ã¢ãªããŒãžã®ãµã€ãºã256 KBã§ãããšæ³å®ããŠããŸãã
ã¢ãªãŒããããŒã«ããããã¯
ããŒã«ã¯ã4 KBã®ä»®æ³ã¡ã¢ãªããŒãžã§ããã¢ãªãŒãã«ãããŸãã ãããã¯ããã®äŸã®æ¬ã®ããŒãžã«äŒŒãŠããŸãã ããŒã«ã¯ã¡ã¢ãªã®å°ããªãããã¯ã«åå²ãããŸãã
åãããŒã«å ã®ãã¹ãŠã®ãããã¯ã¯ãåããµã€ãºã¯ã©ã¹ã«å±ããŸãã ãããã¯ãå±ãããµã€ãºã¯ã©ã¹ã«ãã£ãŠããã®ãããã¯ã®ãµã€ãºã決ãŸããŸãããã®ãµã€ãºã¯ãèŠæ±ãããã¡ã¢ãªãµã€ãºãèæ ®ããŠéžæãããŸãã 以äžã¯ããœãŒã¹ã³ãŒãããååŸããããŒãã«ã§ãã¹ãã¬ãŒãžèŠæ±ãã·ã¹ãã ã«ãã£ãŠåŠçãããããŒã¿ã®ããªã¥ãŒã ãå²ãåœãŠããããããã¯ã®ãµã€ãºããµã€ãºã¯ã©ã¹ã®èå¥åã瀺ããŠããŸãã
ãã€ãåäœã®ããŒã¿é
| ãããã¯ãµã€ãº
| ã¯ã©ã¹idx
|
1-8
| 8
| 0
|
9-16
| 16
| 1
|
17-24
| 24
| 2
|
25ã32
| 32
| 3
|
33-40
| 40
| 4
|
41-48
| 48
| 5
|
49-56
| 56
| 6
|
57-64
| 64
| 7
|
65-72
| 72
| 8
|
...
| ...
| ...
|
497-504
| 504
| 62
|
505-512
| 512
| 63
|
ããšãã°ã42ãã€ãã®ä¿åãèŠæ±ãããå ŽåãããŒã¿ã¯48ãã€ãã®ãããã¯ã«é 眮ãããŸãã
ããŒã«
ããŒã«ã¯ãåããµã€ãºã®ã¯ã©ã¹ã«å±ãããããã¯ã§æ§æãããŸãã åããŒã«ã¯ãäºéãªã³ã¯ãªã¹ãã¡ã«ããºã ã䜿çšããŠãåããµã€ãºã¯ã©ã¹ã®ãããã¯ãå«ãä»ã®ããŒã«ã«é¢é£ä»ããããŸãã ãã®ã¢ãããŒãã䜿çšãããšãã¡ã¢ãªå²ãåœãŠã¢ã«ãŽãªãºã ã¯ãç°ãªãããŒã«ã§ç©ºãé åãèŠã€ããããšã«ãªãå Žåã§ããç¹å®ã®ãµã€ãºã®ãããã¯ã®ç©ºãé åãç°¡åã«èŠã€ããããšãã§ããŸãã
usedpools
ãªã¹ãã䜿çšãããšãç¹å®ã®ãµã€ãºã¯ã©ã¹ã«å±ããããŒã¿ã®äœå°ããããã¹ãŠã®ããŒã«ã远跡ã§ããŸãã ç¹å®ã®ãµã€ãºã®ãããã¯ã®ä¿åãèŠæ±ããããšãã¢ã«ãŽãªãºã ã¯ãã®ãªã¹ãããã§ãã¯ããŠãå¿ èŠãªãµã€ãºã®ãããã¯ãæ ŒçŽããŠããããŒã«ã®ãªã¹ããæ¢ããŸãã
ããŒã«èªäœã¯ã3ã€ã®ç¶æ ã®ããããã«ãªããã°ãªããŸããã ã€ãŸãããããã¯äœ¿çšå¯èœïŒäœ¿çšæžã¿ç¶æ ïŒã§ãããå å¡«ïŒ
full
ïŒãŸãã¯ç©ºïŒ
empty
ïŒã§ãã 䜿çšæžã¿ããŒã«ã«ã¯ãé©åãªãµã€ãºã®ããŒã¿ââãä¿åã§ãã空ããããã¯ããããŸãã ãã£ã±ãã«ãªã£ãããŒã«ã®ãã¹ãŠã®ãããã¯ãããŒã¿ã«å²ãåœãŠãããŸãã 空ã®ããŒã«ã«ã¯ããŒã¿ãå«ãŸãããå¿ èŠã«å¿ããŠãä»»æã®ãµã€ãºã¯ã©ã¹ã«å±ãããããã¯ãæ ŒçŽããããã«å²ãåœãŠãããšãã§ããŸãã
freepools
ãªã¹ãã«ã¯ã
empty
ç¶æ ã«ãããã¹ãŠã®ããŒã«ã«é¢ããæ å ±ãæ ŒçŽãããŸãã ããšãã°ã8ãã€ããããã¯ïŒidx 0ã®ã¯ã©ã¹ïŒãæ ŒçŽããããŒã«ã«é¢ãã
usedpools
ãªã¹ãã«ãšã³ããªããªãå Žåãæ°ããããŒã«ã¯åæåããããã®ãããªãããã¯ãæ ŒçŽããããã®
empty
ç¶æ ã«ãªããŸãã ãã®æ°ããããŒã«ã¯
usedpools
ãªã¹ãã«è¿œå ãããäœæåŸã«åä¿¡ããããŒã¿ãä¿åããèŠæ±ãæºããããã«äœ¿çšã§ããŸãã
full
ç¶æ ã®ããŒã«ã§ãããã€ãã®ãããã¯ã解æŸããããšããŸãã ããã¯ããããã«ä¿åãããŠããããŒã¿ãäžèŠã«ãªã£ãããã§ãã ãã®ããŒã«ã¯ãåã³
usedpools
ãªã¹ãã«å«ãŸãã察å¿ãããµã€ãºã¯ã©ã¹ã®ããŒã¿ã«äœ¿çšã§ããŸãã
ãã®ã¢ã«ãŽãªãºã ã®ç¥èã«ãããæäœäžã«ããŒã«ã®ç¶æ ãã©ã®ããã«å€åãããïŒããã³ãµã€ãºã¯ã©ã¹ãã©ã®ããã«å€åããããããŒã«ã«æ ŒçŽã§ãããããã¯ãã©ã®ããã«å€åãããïŒãç解ã§ããŸãã
ãããã¯
䜿çšæžã¿ããã«ãããã³ç©ºã®ããŒã«
åã®å³ãããããããã«ãããŒã«ã«ã¯å«ãŸããŠããã¡ã¢ãªã®ã空ãããããã¯ãžã®ãã€ã³ã¿ãå«ãŸããŠããŸãã ãããã¯ã®æäœã«é¢ããŠã¯ã1ã€ã®å°ããªæ©èœã«æ³šæããå¿ èŠããããŸããããã¯ãœãŒã¹ã³ãŒãã§ç€ºãããŠããŸãã CPythonã§äœ¿çšãããã¡ã¢ãªç®¡çã·ã¹ãã ã¯ããã¹ãŠã®ã¬ãã«ïŒã¢ãªãŒããããŒã«ããããã¯ïŒã§ã絶察ã«å¿ èŠãªå Žåã«ã®ã¿ã¡ã¢ãªãå²ãåœãŠãããšããŸãã
ããã¯ãããŒã«ã«æ¬¡ã®3ã€ã®ç¶æ ã®ããããã®ãããã¯ãå«ããããšãã§ããããšãæå³ããŸãã
-
untouched
ã¯ããŸã å²ãåœãŠãããŠããªãã¡ã¢ãªã®éšåã§ãã -
free
ã¯æ¢ã«å²ãåœãŠãããã¡ã¢ãªã®äžéšã§ãããåŸã«CPythonã«ãã£ãŠãfreeãã«ããã貎éãªããŒã¿ãå«ãŸããªããªããŸããã -
allocated
ã¯ã貎éãªããŒã¿ãå«ãã¡ã¢ãªã®éšåã§ãã
freeblock
ãã€ã³ã¿ã¯ã空ãã¡ã¢ãªãããã¯ã®åäžãªã³ã¯ãªã¹ããæããŸãã ã€ãŸããããã¯ããŒã¿ãé 眮ã§ããå Žæã®ãªã¹ãã§ãã ããŒã¿ãé 眮ããããã«è€æ°ã®ç©ºããããã¯ãå¿ èŠãªå Žåãã¡ã¢ãªã¢ãã±ãŒã¿ãŒã¯ã
untouched
ç¶æ ã®ããŒã«ããããã€ãã®ãããã¯ãååŸããŸãã
ã¡ã¢ãªãããŒãžã£ããããã¯ããããªãŒãã«ãããšããããã¯ã
free
ç¶æ ãç²åŸãããšã
free
freeblock
ãªã¹ãã®å é ã«
freeblock
ãŸãã ãã®ãªã¹ãã«å«ãŸãããããã¯ã¯ãå¿ ãããåã®å³ã«ç€ºãããã®ãšåæ§ã®é£ç¶ããã¡ã¢ãªé åãè¡šããšã¯éããŸããã å®éã«ã¯ã次ã®ããã«ãªããŸãã
åäžã®ãªã³ã¯ãããããªãŒãããã¯ãªã¹ã
ã¢ãªãŒã
ã¢ãªãŒãã«ã¯ããŒã«ãå«ãŸããŸãã æ¢ã«è¿°ã¹ãããã«ããããã®ããŒã«ã¯ã
used
ã
full
ãŸãã¯
empty
ç¶æ ã«ãªããŸãã ã¢ãªãŒãã«ã¯ãããŒã«ã®ç¶æ ãšåæ§ã®ç¶æ ããªãããšã«æ³šæããŠãã ããã
ã¢ãªãŒãã¯ã
usable_arenas
ãšåŒã°ããäºéã«ãªã³ã¯ããããªã¹ãã«ç·šæãã
usable_arenas
ã ãã®ãªã¹ãã¯ã䜿çšå¯èœãªç©ºãããŒã«ã®æ°ã§ãœãŒããããŠããŸãã ã¢ãªãŒãã®ç©ºãããŒã«ãå°ãªãã»ã©ãã¢ãªãŒãã¯ãªã¹ãã®äžçªäžã«è¿ã¥ããŸãã
Usable_arenasãªã¹ã
ããã¯ãããŒã¿ã§æºããããä»ã®ã¢ãªãŒãããã匷ãã¢ãªãŒããéžæãããããã«æ°ããããŒã¿ãé 眮ãããããšãæå³ããŸãã ãããŠããªããã®éã§ã¯ãªãã®ã§ããïŒ ç©ºãé åãæãå€ãã¢ãªãŒãã«æ°ããããŒã¿ãæçš¿ããŠã¿ãŸãããïŒ
å®éããã®æ©èœã«ãããã¡ã¢ãªãå®å šã«è§£æŸãããšããã¢ã€ãã¢ãããããããŸãã ãæ°ä»ããããããŸããããããã§ã¯ "ã¡ã¢ãªã®è§£æŸ"ãšããæŠå¿µããã䜿çšããåŒçšç¬Šã§å²ãã§ããŸãã ãããè¡ãããçç±ã¯ããããã¯ã¯ã空ãããšèŠãªãããšãã§ããŸããããããã¯ãè¡šãã¡ã¢ãªã®äžéšãå®éã«ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«è¿ãããªãããã§ãã Pythonããã»ã¹ã¯ãã®ã¡ã¢ãªãä¿æããåŸã§ããã䜿çšããŠæ°ããããŒã¿ãä¿åããŸãã ã¡ã¢ãªã®çã®ãªãªãŒã¹ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«æ»ãã䜿çšã§ããããã«ãªãããšã§ãã
ã¢ãªãŒãã¯ãããã§èª¬æããã¹ããŒã ã§å¯äžã®ãšã³ãã£ãã£ã§ãããããã«ãã£ãŠè¡šãããã¡ã¢ãªã¯æ¬åœã«è§£æŸã§ããŸãã åžžèã§ã¯ãã¢ãªãŒãã䜿çšããäžèšã®ã¹ããŒã ã¯ãã»ãšãã©ç©ºã®ã¢ãªãŒããå®å šã«ç©ºã«ããããšãç®çãšããŠããŸãã ãã®ã¢ãããŒãã䜿çšãããšãå®å šã«ç©ºã®ã¢ãªãŒãã§è¡šãããã¡ã¢ãªã®äžéšãå®å šã«è§£æŸã§ãããããPythonãæ¶è²»ããã¡ã¢ãªã®éãåæžã§ããŸãã
ãŸãšã
ãã®è³æãèªãã§åŠãã ããšã¯æ¬¡ã®ãšããã§ãã
- ã¡ã¢ãªç®¡çãšã¯äœãããªãéèŠãªã®ãã
- Cããã°ã©ãã³ã°èšèªã§èšè¿°ãããPythonãCpythonã®ãªãã¡ã¬ã³ã¹å®è£ ã®é 眮æ¹æ³ã
- ã¡ã¢ãªç®¡çã®ããã«CPythonã§äœ¿çšãããããŒã¿æ§é ãšã¢ã«ãŽãªãºã ã
ã¡ã¢ãªç®¡çã¯ãã³ã³ãã¥ãŒã¿ãŒããã°ã©ã ã®äœæ¥ã®äžå¯æ¬ ãªéšåã§ãã Pythonã¯ãããã°ã©ããŒãæ°ä»ããªãã»ãšãã©ãã¹ãŠã®ã¡ã¢ãªç®¡çã¿ã¹ã¯ã解決ããŸãã Pythonã䜿çšãããšããã®èšèªã§èšè¿°ãã人ã¯èª°ã§ããã³ã³ãã¥ãŒã¿ãŒã®æäœã«é¢é£ããå€ãã®å°ããªè©³çŽ°ãç¡èŠã§ããŸãã ããã«ãããããã°ã©ãã¯ããé«ãã¬ãã«ã§äœæ¥ããããŒã¿ã®ä¿åå Žæãæ°ã«ããã«ç¬èªã®ã³ãŒããäœæã§ããŸãã
芪æãªãèªè ïŒ Pythonéçºã®çµéšãããå Žåã¯ãããã°ã©ã ã®ã¡ã¢ãªäœ¿çšéã«ã©ã®ããã«ã¢ãããŒããããæããŠãã ããã ããšãã°ãä¿åããããšããŠããŸããïŒ