TLãDR
PHP 8ã®Just In Timeã³ã³ãã€ã©ãŒã¯ãOpcacheæ¡åŒµæ©èœã®äžéšãšããŠå®è£ ãããŠãããå®è¡æã«ãªãã¬ãŒãã£ã³ã°ã³ãŒããããã»ããµãŒåœä»€ã«ã³ã³ãã€ã«ããããã«èšèšãããŠããŸãã
ã€ãŸããJITã䜿çšãããšãäžéšã®ãªãã¬ãŒãã£ã³ã°ã³ãŒããZend VMã§è§£éããå¿ èŠããªããªãããã®ãããªåœä»€ã¯ããã»ããµã¬ãã«ã®åœä»€ãšããŠçŽæ¥å®è¡ãããŸãã
PHP 8ã®JIT
PHP 8ã®æãã³ã¡ã³ãã®å€ãæ©èœã®1ã€ã¯ãJust In TimeïŒJITïŒã³ã³ãã€ã©ãŒã§ããããã¯å€ãã®ããã°ãã³ãã¥ããã£ã§è³ã«ããŸã-ããã«ã€ããŠã¯å€ãã®è©±é¡ããããŸããããããŸã§ã®ãšãããJITãã©ã®ããã«æ©èœãããã«ã€ããŠã®è©³çŽ°ã¯ããŸãããããŸããã
æçšãªæ å ±ãèŠã€ããããã«äœåºŠãè©Šè¡é¯èª€ãç¹°ãè¿ããçµæãPHPã®ãœãŒã¹ã³ãŒãã調ã¹ãããšã«ããŸãããç§ã®Cã«é¢ããå°ããªç¥èãšããããŸã§ã«åéã§ããæ£åšãããã¹ãŠã®æ å ±ãçµã¿åãããŠããã®èšäºãæºåããPHP JITãããããç解ããã®ã«åœ¹ç«ã€ããšãé¡ã£ãŠããŸãã
ç©äºãã·ã³ãã«ã«ä¿ã€ããã«ïŒJITãæåŸ ã©ããã«æ©èœããå Žåãã³ãŒãã¯Zend VMãä»ããŠå®è¡ãããã®ã§ã¯ãªããäžé£ã®ããã»ããµã¬ãã«ã®åœä»€ãšããŠçŽæ¥å®è¡ãããŸãã
ãããå šäœã®ã¢ã€ãã¢ã§ãã
ãããããããããããç解ããã«ã¯ãPHPãå éšã§ã©ã®ããã«æ©èœããããèããå¿ èŠããããŸããããã»ã©é£ããããšã§ã¯ãããŸããããããã€ãã®çŽ¹ä»ãå¿ èŠã§ãã
ç§ã¯ãã§ã«ãphpãã©ã®ããã«æ©èœãããã«ã€ããŠã®æŠèŠããŸãšããèšäºãæžããŠããŸãããã®èšäºãè€éã«ãªããããŠãããšæãããå Žåã¯ããã®åä»»è ãèªãã§æ»ã£ãŠããŠãã ãããããã«ãããåŠçãå°ãç°¡åã«ãªããŸãã
PHPã³ãŒãã¯ã©ã®ããã«å®è¡ãããŸããïŒ
ç§ãã¡ã¯çãphpãã€ã³ã¿ãŒããªã¿ãŒåèšèªã§ããããšãç¥ã£ãŠããŸããããããããã¯å®éã«ã¯ã©ãããæå³ã§ããïŒ
ã¹ããããã§ãããWebã¢ããªã±ãŒã·ã§ã³å šäœã§ãããPHPã³ãŒããå®è¡ãããšãã¯ãã€ã§ããphpã€ã³ã¿ãŒããªã¿ãŒãçµç±ããå¿ èŠããããŸããæãäžè¬çã«äœ¿çšãããã®ã¯ãPHP FPMãšCLIã€ã³ã¿ãŒããªã¿ãŒã§ãã圌ãã®ä»äºã¯éåžžã«ç°¡åã§ããphpã³ãŒããååŸããŠè§£éããçµæãè¿ãã ãã§ãã
ããã¯ããã¹ãŠã®ã€ã³ã¿ããªã¿èšèªã«å ±éã®ç¶æ³ã§ããäžéšã®æé ã¯ç°ãªãå ŽåããããŸãããäžè¬çãªèãæ¹ã¯åãã§ããPHPã§ã¯ã次ã®ããã«æ©èœããŸãã
- PHPã³ãŒããèªã¿åãããããŒã¯ã³ãšåŒã°ããããŒã¯ãŒãã®ã»ããã«å€æãããŸãããã®ããã»ã¹ã«ãããã€ã³ã¿ããªã¿ã¯ããã°ã©ã ã®ã©ã®éšåã«åã³ãŒããèšè¿°ãããŠããããç解ã§ããŸãããã®æåã®ã¹ãããã¯ãLexingãŸãã¯TokenizingãšåŒã°ããŸãã
- , PHP . (Abstract Syntax Tree â AST) , (parsing). AST , , . , «echo 1 + 1» « 1 + 1» , , « , â 1 + 1».
- AST, , . -, , (Intermediate Representation IR), PHP (Opcode). AST .
- ãªãã³ãŒããäœæãããã®ã§ãæãèå³æ·±ãã®ã¯ã³ãŒãã®å®è£ ã§ããPHPã«ã¯Zend VMãšåŒã°ãããšã³ãžã³ãããããªãã³ãŒãã®ãªã¹ããååŸããŠå®è¡ã§ããŸãããã¹ãŠã®ãªãã³ãŒããå®è¡ãããåŸãããã°ã©ã ã¯çµäºããŸãã
å°ãããããããããããã«ã
å³ãäœæããŸãããPHP解éããã»ã¹ã®ç°¡ç¥å³ã§ãã
ããªããèŠãããšãã§ããããã«ããªãç°¡åã§ããããããããã«ãããã«ããã¯ããããŸããphpã³ãŒããé »ç¹ã«å€æŽãããªãå Žåãå®è¡ãããã³ã«ã³ãŒããåå¥è§£æããã³è§£æãããã€ã³ãã¯äœã§ããïŒ
çµå±ã®ãšããããªãã³ãŒãã ãã«èå³ããããŸãããïŒæ£ããïŒãããOpcacheæ¡åŒµãååšããçç±ã§ãã
Opcacheæ¡åŒµ
Opcacheæ¡åŒµæ©èœã¯PHPã«ä»å±ããŠãããéåžžãç¡å¹ã«ããç¹å¥ãªçç±ã¯ãããŸããã PHPã䜿çšããŠããå Žåã¯ãOpcacheãæå¹ã«ããå¿ èŠããããŸãã
ããã¯ããªã³ã©ã€ã³ã®å ±æãªãã³ãŒããã£ãã·ã¥ã¬ã€ã€ãŒãè¿œå ããããšã§ãããã®ä»äºã¯ãæè¿çæããããªãã³ãŒããASTãããã§ããããŠãã£ãã·ã¥ããåŸã®å®è¡ã§åå¥è§£æãšè§£æã®ãã§ãŒãºãç°¡åã«ã¹ãããã§ããããã«ããããšã§ãã
以äžã¯ãOpcacheæ¡åŒµãèæ ®ããåãããã»ã¹ã®å³ã§ããOpcacheã䜿çšãã
PHP解éãããŒããã¡ã€ã«ããã§ã«è§£æãããŠããå Žåãphpã¯ãã¡ã€ã«ãå解æããã®ã§ã¯ãªãããã£ãã·ã¥ããããªãã³ãŒããæœåºããŸãã
åå¥è§£æã解æãããã³ã³ã³ãã€ã«ã®ã¹ããããã©ãã»ã©çŸããã¹ããããããããé äºããã ãã§ãã
泚æïŒããã§ãPHP 7.4ããªããŒãæ©èœã圹ã«ç«ã¡ãŸããããã«ãããPHP FPMã«ã³ãŒãããŒã¹ã解æããããããªãã³ãŒãã«å€æããå®éã«äœããè¡ãåã§ããã£ãã·ã¥ããããã«æ瀺ã§ããŸãã
ããã§JITãã©ãã«è²Œãä»ããããšãã§ããã®ãçåã«æããããããããŸããããå°ãªããšãç§ã¯ããé¡ã£ãŠããŸãããããç§ããã®èšäºãæžããŠããçç±ã§ã...
Just In Timeã³ã³ãã€ã©ãŒã¯äœãããŸããïŒ
PHP Internals Newsã®PHPãšJITããããã£ã¹ãã®ãšããœãŒã㧠Zivã®èª¬æãèããåŸãJITãå®éã«äœãããã¹ããã«ã€ããŠããã€ãã®ã¢ã€ãã¢ãåŸãããšãã§ããŸãã...
Opcacheãopcodeã®é«éãã§ãããèš±å¯ããZend VMãJITã«çŽæ¥ãžã£ã³ãã§ããå ŽåZend VMããªããŠãæ©èœããããã«èšèšãããŠããŸãã
Zend VMã¯ããªãã¬ãŒãã£ã³ã°ã³ãŒããšããã»ããµèªäœã®éã®ã¬ã€ã€ãŒãšããŠæ©èœããCããã°ã©ã ã§ããJITã¯ã³ã³ãã€ã«ãããã³ãŒããå®è¡æã«çæãããããphpã¯Zend VMãã¹ãããããŠãçŽæ¥ããã»ããµã«ãžã£ã³ãã§ããŸããçè«çã«ã¯ãããã©ãŒãã³ã¹ã®ç¹ã§ããããå©çãåŸãã¹ãã§ãã
ãã·ã³ã³ãŒããã³ã³ãã€ã«ããã«ã¯ãã¢ãŒããã¯ãã£ã®ã¿ã€ãããšã«éåžžã«å ·äœçãªå®è£ ãäœæããå¿ èŠããããããæåã¯å¥åŠã«èãããŸãããããããå®éã«ã¯ããªãçŸå®çã§ãã
PHPã®JITå®è£ ã¯ãDynASMïŒåçã¢ã»ã³ãã©ãŒïŒã©ã€ãã©ãªãŒã䜿çšããŸããããã¯ãç¹å®ã®åœ¢åŒã®CPUåœä»€ã®ã»ããããããŸããŸãªã¿ã€ãã®CPUã®ã¢ã»ã³ããªã³ãŒãã«ãããããŸãããããã£ãŠãJust In Timeã³ã³ãã€ã©ã¯ãDynASMã䜿çšããŠãªãã¬ãŒãã£ã³ã°ã³ãŒããã¢ãŒããã¯ãã£åºæã®ãã·ã³ã³ãŒãã«å€æããŸãã
ãŸã 誰ããç§ãæ©ãŸããŠããŸããã...
ããªããŒããå®è¡åã«PHPã³ãŒããæäœå¯èœã«è§£æã§ããDynASMãæäœå¯èœã³ãŒãããã·ã³ã³ãŒãã«ã³ã³ãã€ã«ã§ããå ŽåïŒãžã£ã¹ãã€ã³ã¿ã€ã ã³ã³ãã€ã«ïŒãAhead of Timeã³ã³ãã€ã«ã䜿çšããŠPHPãé©åãªå Žæã«ã³ã³ãã€ã«ããªãã®ã¯ãªãã§ããïŒ
ç§ãããããã£ã¹ããšããœãŒãããåŸãèãã®1ã€ã¯ãPHPã®åã匱ããšããããšã§ãããã€ãŸããZend VMãç¹å®ã®ãªãã³ãŒããå®è¡ããããšãããŸã§ãPHPã¯å€æ°ã®åãç¥ããªãããšããããããŸããããã¯ãå€æ°ã®ããŸããŸãªåè¡šçŸãžã®å€ãã®ãã€ã³ã¿ãæã€zend_valueå ±çšäœåã
調ã¹ãããšã§ç解ã§ããŸãã Zendã®VMãzend_valueããå€ãååŸããããšãããã³ã«ãããã¯ã®ãããªãã¯ãã®äœ¿çšZSTR_VALãå€ã®é£çµããæååãã€ã³ã¿ã«ã¢ã¯ã»ã¹ããããšããŠããŸãã
ããšãã°ããã®Zend VMãã³ãã©ãŒã¯ã以äžïŒ<=ïŒåŒãåŠçããå¿ èŠããããŸãããªãã©ã³ãã®ã¿ã€ããæšæž¬ããããã«ãããŸããŸãªã³ãŒããã¹ã«åå²ããæ¹æ³ã確èªããŠãã ããã
ãã®åæšè«ããžãã¯ããã·ã³ã³ãŒãã§è€è£œããããšã¯çŸå®çã§ã¯ãªããç©äºãããã«é ãããå¯èœæ§ããããŸãã
åãè©äŸ¡ãããåŸã®æåŸã®ã³ã³ãã€ã«ãããã·ã³ã³ãŒããžã®ã³ã³ãã€ã«ã¯CPUãéäžçã«äœ¿çšããã¿ã¹ã¯ã§ãããããé©åãªãªãã·ã§ã³ã§ã¯ãããŸããããããã£ãŠãå®è¡æã«ãã¹ãŠãã³ã³ãã€ã«ããããšã¯æªãèãã§ãã
Just In Timeã³ã³ãã€ã©ã¯ã©ã®ããã«åäœããŸããïŒ
ååãªéã®ããªã³ã³ãã€ã«ãçæããããã«åãæšå®ããããšã¯ã§ããªãããšãããã£ããå®è¡æã®ã³ã³ãã€ã«ã«ã¯ã³ã¹ãããããããšãããã£ãŠããŸãã JITã¯PHPã«ã©ã®ããã«åœ¹ç«ã¡ãŸããïŒ
ãã®æ¹çšåŒã®ãã©ã³ã¹ãåãããã«ãPHP JITã¯ã䟡å€ããããšèããããã€ãã®ãªãã³ãŒãã®ã¿ãã³ã³ãã€ã«ããããšããŸãããããè¡ãããã«ãZendä»®æ³ãã·ã³ã«ãã£ãŠå®è¡ããããªãã³ãŒãã®ãããã¡ã€ã«ãäœæããã©ã®ã³ãŒããã³ã³ãã€ã«ã«æå³ããããããã§ãã¯ããŸãã ïŒæ§æã«ãã£ãŠç°ãªããŸãïŒã
ç¹å®ã®opcodeãã³ã³ãã€ã«ããããšãZend VMã«å§ä»»ãã代ããã«ãã³ã³ãã€ã«ãããã³ãŒãã«å®è¡ãå§ä»»ããŸãã次ã®å³ã®ããã«ãªããŸã
ãJITã䜿çšããPHP解éãããŒããããããã§ã«ã³ã³ãã€ã«ãããŠããå Žåããªãã³ãŒãã¯Zend VMãä»ããŠå®è¡ãããŸããã
ãããã£ãŠãOpcacheæ¡åŒµæ©èœã«ã¯ãç¹å®ã®ãªãã¬ãŒãã£ã³ã°ã³ãŒããã³ã³ãã€ã«ããå¿ èŠããããã©ããã決å®ããããã€ãã®åœä»€ããããŸãããã®å Žåãã³ã³ãã€ã©ãŒã¯DynASMã䜿çšããŠããããã·ã³ã³ãŒãã«å€æãããã®æ°ããçæããããã·ã³ã³ãŒããå®è¡ããŸãã
èå³æ·±ãããšã«ãçŸåšã®å®è£ ã§ã¯ã³ã³ãã€ã«æžã¿ã³ãŒãïŒæ§æå¯èœïŒã«ã¡ã¬ãã€ãã®å¶éããããããã³ãŒãå®è¡ã¯JITãšè§£éæžã¿ã³ãŒããã·ãŒã ã¬ã¹ã«åãæ¿ããããšãã§ããã¯ãã§ãã
ã¡ãªã¿ã«ãBenoit Jacquemontã«ããphpã®JITã«é¢ãããã®è¬æŒã¯ããããç解ããã®ã«éåžžã«åœ¹ç«ã¡ãŸããã
ã©ã®ãããªå Žåã«ã³ã³ãã€ã«ãè¡ããããã¯ãŸã ããããŸãããããŸã ããã«ã€ããŠã¯ç¥ããããããŸããã
ã ãããããªãã®çç£æ§ã®åäžã¯ãããã巚倧ã§ã¯ãªãã§ããã
ã»ãšãã©ã®phpã¢ããªã±ãŒã·ã§ã³ã¯Just In Timeã³ã³ãã€ã©ãŒã䜿çšããŠãããã©ãŒãã³ã¹ã®ã¡ãªãããããŸããªããšèª°ããèšã£ãŠããçç±ãä»ããã£ãšã¯ã£ããããŠããããšãé¡ã£ãŠããŸãããããŠãã¢ããªã±ãŒã·ã§ã³ã®ããŸããŸãªJITæ§æã®ãããã¡ã€ãªã³ã°ãšå®éšã«é¢ããZivã®æšå¥šäºé ãæåã®æ¹æ³ã§ããçç±ã
PHP FPMã䜿çšããŠããå Žåãã³ã³ãã€ã«ããããªãã³ãŒãã¯éåžžè€æ°ã®ãªã¯ãšã¹ãã«åæ£ãããŸãããããã¯ãŸã ã²ãŒã ãã§ã³ãžã£ãŒã§ã¯ãããŸããã
ããã¯ãJITãCPUã®æäœãæé©åããããã§ãããçŸåšãã»ãšãã©ã®phpã¢ããªã±ãŒã·ã§ã³ã¯ãä»ã®äœãããI / Oã«äŸåããŠããŸãããšã«ãããã£ã¹ã¯ããããã¯ãŒã¯ã«ã¢ã¯ã»ã¹ããå¿ èŠãããå ŽåãåŠçæäœãã³ã³ãã€ã«ãããŠãããã©ããã¯é¢ä¿ãããŸãããã¿ã€ãã³ã°ã¯éåžžã«äŒŒãŠããŸãã
ããã...
ç»ååŠçãæ©æ¢°åŠç¿ãªã©ãI / O以å€ã®ããšãå®è¡ããŠããŸãã I / O以å€ã®ãã®ã¯ããžã£ã¹ãã€ã³ã¿ã€ã ã³ã³ãã€ã©ã®æ©æµãåããŸããããããCã§ã¯ãªãPHPã§èšè¿°ããããã€ãã£ãPHPé¢æ°ãæžãããšã«åŸåããŠãããšäººã ãèšãçç±ã§ããããŸãããã®ãããªé¢æ°ãã³ã³ãã€ã«ããŠãããªãŒããŒãããã¯åçã«å€ãããŸããã
PHPããã°ã©ããŒãšããŠã®èå³æ·±ãæé...
ãã®èšäºãã圹ã«ç«ãŠã°å¹žãã§ããPHP8ã®JITã«ã€ããŠç解ãæ·±ããŠããã ããã°å¹žãã§ããããã§ããããããªãã®ä»²éã®éçºè ãšå ±æããããšãå¿ããªãã§ãã ãããããã¯ç¢ºãã«ããªãã®äŒè©±ã«å°ãã®äŸ¡å€ãè¿œå ããŸãïŒ
-- @nawarian
PHP: