Php’s guts

  • Published on

  • View

  • Download

Embed Size (px)


Talk given at True North PHP 2014


  • 1. PHPs GutsWhy you should know how PHP works

2. ArchitectureYo I heard you like modules in your modules so we gave you three types!. 3. Engine Lexer, Parser, Compiler, Executor 4. Core Streams Request Management Variables Network 5. SAPIS Server API CLI, CGI, mod_php, phpdbg, embed input args output, flushing, file descriptors, interruptions, system user info input filtering and optionally headers, post data, http specific stuff 6. Extensions Talk to a C library do stuff faster then PHP make the engine funny 7. Lifecycle MINIT RINIT GINITGINITMINITRINITGSHUTDOWNRSHUTDOWN SCRIPTMSHUTDOWN 8. Who Cares? Pick the right SAPI Fewer extensions = better Static extensions = better Lifecycle is important for sharing stuff Newer PHP = better faster stronger 9. turn on the go_fast ini settingThread, fork, async, very wow 10. Threading Thread Safe != reentrant Thread safe != parallel Thread safe != async Thread safe != concurrent Thread safe == two threads running at the same time wont stomp on the others data yes really, thats all it means 11. Reentrant Lets quit this, and run it again and it will be like we never ran it 12. Async Im gonna work on this stuff But Im not going to block you if you have important stuff to do 13. Parallel Concurrent Concurrent two things at the same time that need communication Parallel two things at the same time 14. TSRM Thread safe resource manager global data in extensions making some C re-entrant thread safety 15. Why do I care? react-php (parallel) pecl event (async) pthreads (concurrent) pcntl (fork and pray) proc_open/popen (subprocessing) queues and jobs and workers native tls rfc 16. Welcome to the EngineLexers and Parsers and Opcodes OH MY! 17. Lexer checks PHPs spelling turns into tokens see token_get_all for what PHP sees 18. Parser checks PHPs grammar E_PARSE means bad phpish creates opcodes (or AST) 19. Compiler Only with AST Turns AST into Opcodes Allows for fancier grammar 20. Opcodes dump with machine readable language the runtime understands 21. Opcache (and AST) cache opcodes skip lexing and parsing 22. Engine (Virtual Machine) reads opcode does something ??? PROFIT 23. Why do I care? Use an opcode cache If you dont, youre crazy, stupid, or lazy Upgrade to get cooler stuff 24. VariablesPHP is a C types wrapper 25. Zvalstypedef union _zvalue_value {long lval;double dval;struct {char *val;int len;} str;HashTable *ht;zend_object_value obj;} zvalue_value;typedef union _zend_value {zend_long lval;double dval;zend_refcounted *counted;zend_string *str;zend_array *arr;zend_object *obj;zend_resource *res;zend_reference *ref;zend_ast_ref *ast;zval *zv;void *ptr;zend_class_entry *ce;zend_function *func;} zend_value; 26. Numbers Booleans are unsigned char Integers are really signed long integers Longs are platform dependent Floats and doubles are doubles not floats 27. 64 Bit Madness LLP64 short = 16 int = 32 long = 32 long long = 64 pointer = 64(windows) LP64 short = 16 integer = 32 long = 64 long long = 64 pointer = 64(unices) 28. Strings Char * Translated to what we see by an algorithm ASCII, UTF8, binary EVERYTHING has a codepage wchar? screw you 29. Arrays theyre not hashtables and doubly linked lists 30. Resources stores random opaque C data in a giant list of doom sigh 31. Objects handlers property tables magic storage 32. Why do I care? Know the limitations of your data types Remember that arrays arent arrays Beware of many many resources Beware of many many objects 64 bit can be broken in strange ways 33. C MoarImplementation WTFeries and other fun 34. Stack? Heap? Stack = scratch space for thread of execution can overflow! slightly faster size determined at thread start Heap = space for dynamic allocation managed by program can fragment leaky! 35. Zend Memory Manager Internal Heap Allocator frees yo memory (leak management) preallocates blocks in set sizes that PHP uses caches allocations to avoid fragmentation allows monitoring of memory usage 36. COW (not moo) Copy On Write 1 zval, many variables each variable increases refcount destroy after refcount Oh no, a change! copy 37. Refcounts, GC, and PHPNG Sometimes you have a refcount but no var to reference it This is a circular reference, this sucks (ask doctrine) GC checks for this periodically and cleans up PHPNG 38. References are not Pointers PHP is smarter than you are access the same variable content by different names using symbol table aliases variable name != variable content 39. Side Track Objects are not References$a = new stdClass;$b = $a;$a->foo = 'bar';var_dump($b);$a = 'baz';var_dump($b); 40. Places to Learn More 41. About Me @auroraeosrose Thats Aurora Eos Rose freenode in #phpmentoring #phpwomen #phpinternals