SlideShare ist ein Scribd-Unternehmen logo
1 von 41
PHP’s Guts 
Why you should know how PHP works
Architecture 
Yo I heard you like modules in your modules so we gave you three types!.
Engine 
 Lexer, Parser, Compiler, Executor
Core 
 Streams 
 Request Management 
 Variables 
 Network
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
Extensions 
 Talk to a C library 
 do stuff faster then PHP 
 make the engine funny
Lifecycle 
 MINIT 
 RINIT 
 GINIT 
GINIT 
MINIT 
RINIT 
GSHUTDOWN 
RSHUTDOWN SCRIPT 
MSHUTDOWN
Who Cares? 
 Pick the right SAPI 
 Fewer extensions = better 
 Static extensions = better 
 Lifecycle is important for sharing stuff 
 Newer PHP = better faster stronger
turn on the “go_fast” ini setting 
Thread, fork, async, very wow
Threading 
 Thread Safe != reentrant 
 Thread safe != parallel 
 Thread safe != async 
 Thread safe != concurrent 
 Thread safe == two threads running at the same time won’t stomp on the others data 
 yes really, that’s all it means
Reentrant 
 Let’s quit this, and run it again 
 and it will be like we never ran it
Async 
 I’m gonna work on this stuff 
 But I’m not going to block you if you have important stuff to do
Parallel … Concurrent 
 Concurrent – two things at the same time that need communication 
 Parallel – two things at the same time
TSRM 
 Thread safe resource manager 
 global data in extensions 
 making some C re-entrant 
 thread safety
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
Welcome to the Engine 
Lexers and Parsers and Opcodes OH MY!
Lexer 
 checks PHP’s spelling 
 turns into tokens 
 see token_get_all for what PHP sees
Parser 
 checks PHP’s grammar 
 E_PARSE means “bad phpish” 
 creates opcodes (or AST)
Compiler 
 Only with AST 
 Turns AST into Opcodes 
 Allows for fancier grammar
Opcodes 
 dump with http://derickrethans.nl/projects.html 
 machine readable language the runtime understands
Opcache (and AST) 
 cache opcodes – skip lexing and parsing 
 https://support.cloud.engineyard.com/entries/26902267-PHP-Performance-I-Everything- 
You-Need-to-Know-About-OpCode-Caches 
 https://wiki.php.net/rfc/abstract_syntax_tree
Engine (Virtual Machine) 
 reads opcode 
 does something 
 ??? 
 PROFIT
Why do I care? 
 Use an opcode cache 
 If you don’t, you’re crazy, stupid, or lazy 
 Upgrade to get cooler stuff
Variables 
PHP is a C types wrapper
Zvals 
typedef 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;
Numbers 
 Booleans are unsigned char 
 Integers are really signed long integers 
 Longs are platform dependent 
 Floats and doubles are doubles not floats
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)
Strings 
 Char * 
 Translated to what we see by an algorithm 
 ASCII, UTF8, binary – EVERYTHING has a codepage 
 wchar? screw you
Arrays 
 they’re not 
 hashtables 
 and doubly linked lists
Resources 
 stores random opaque C data 
 in a giant list of doom 
 sigh
Objects 
 handlers 
 property tables 
 magic storage
Why do I care? 
 Know the limitations of your data types 
 Remember that arrays aren’t arrays 
 Beware of many many resources 
 Beware of many many objects 
 64 bit can be broken in strange ways
C Moar 
Implementation WTFeries and other fun
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!
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
COW (not moo) 
 Copy On Write 
 1 zval, many variables 
 each variable increases refcount 
 destroy after refcount 
 Oh no, a change! copy
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
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
Side Track – Objects are not References 
$a = new stdClass; 
$b = $a; 
$a->foo = 'bar'; 
var_dump($b); 
$a = 'baz'; 
var_dump($b);
Places to Learn More 
 http://www.phpinternalsbook.com 
 http://php.net 
 http://lxr.php.net 
 http://wiki.php.net 
 http://nikic.github.io/ 
 http://blog.krakjoe.ninja/
About Me 
 http://emsmith.net 
 @auroraeosrose 
 That’s Aurora Eos Rose 
 auroraeosrose@gmail.com 
 freenode in #phpmentoring #phpwomen #phpinternals

Weitere ähnliche Inhalte

Was ist angesagt?

Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
julien pauli
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
julien pauli
 

Was ist angesagt? (20)

Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension review
 
System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 
Lecture8
Lecture8Lecture8
Lecture8
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
HHVM and Hack: A quick introduction
HHVM and Hack: A quick introductionHHVM and Hack: A quick introduction
HHVM and Hack: A quick introduction
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Submit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло МорозовSubmit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло Морозов
 

Andere mochten auch

Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a feature
Elizabeth Smith
 

Andere mochten auch (10)

Javascript DOM
Javascript DOMJavascript DOM
Javascript DOM
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
AutoIt for the rest of us - handout
AutoIt for the rest of us - handoutAutoIt for the rest of us - handout
AutoIt for the rest of us - handout
 
Php
PhpPhp
Php
 
Using AutoIt for Millennium Task Automation Handout
Using AutoIt for Millennium Task Automation HandoutUsing AutoIt for Millennium Task Automation Handout
Using AutoIt for Millennium Task Automation Handout
 
Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a feature
 
Lets Auto It
Lets Auto ItLets Auto It
Lets Auto It
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)
 
Website Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequestWebsite Auto scraping with Autoit and .Net HttpRequest
Website Auto scraping with Autoit and .Net HttpRequest
 
Modern sql
Modern sqlModern sql
Modern sql
 

Ähnlich wie Php’s guts

NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
rantav
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
Elizabeth Smith
 
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
Hadoop User Group
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
Elizabeth Smith
 

Ähnlich wie Php’s guts (20)

Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopers
 
Listen afup 2010
Listen afup 2010Listen afup 2010
Listen afup 2010
 
Avro
AvroAvro
Avro
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
Yahoo! Hadoop User Group - May Meetup - HBase and Pig: The Hadoop ecosystem a...
 
Php1
Php1Php1
Php1
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performances
 
Php training in chandigarh
Php  training in chandigarhPhp  training in chandigarh
Php training in chandigarh
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Basics of C
Basics of CBasics of C
Basics of C
 

Mehr von Elizabeth Smith

Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012
Elizabeth Smith
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
Elizabeth Smith
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
Elizabeth Smith
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
Elizabeth Smith
 

Mehr von Elizabeth Smith (19)

Welcome to the internet
Welcome to the internetWelcome to the internet
Welcome to the internet
 
Database theory and modeling
Database theory and modelingDatabase theory and modeling
Database theory and modeling
 
Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphp
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Lexing and parsing
Lexing and parsingLexing and parsing
Lexing and parsing
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Do the mentor thing
Do the mentor thingDo the mentor thing
Do the mentor thing
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 
Php go vrooom!
Php go vrooom!Php go vrooom!
Php go vrooom!
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Php’s guts

  • 1. PHP’s Guts Why you should know how PHP works
  • 2. Architecture Yo 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  GINIT GINIT MINIT RINIT GSHUTDOWN RSHUTDOWN SCRIPT MSHUTDOWN
  • 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 setting Thread, 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 won’t stomp on the others data  yes really, that’s all it means
  • 11. Reentrant  Let’s quit this, and run it again  and it will be like we never ran it
  • 12. Async  I’m gonna work on this stuff  But I’m 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 Engine Lexers and Parsers and Opcodes OH MY!
  • 17. Lexer  checks PHP’s spelling  turns into tokens  see token_get_all for what PHP sees
  • 18. Parser  checks PHP’s 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 http://derickrethans.nl/projects.html  machine readable language the runtime understands
  • 21. Opcache (and AST)  cache opcodes – skip lexing and parsing  https://support.cloud.engineyard.com/entries/26902267-PHP-Performance-I-Everything- You-Need-to-Know-About-OpCode-Caches  https://wiki.php.net/rfc/abstract_syntax_tree
  • 22. Engine (Virtual Machine)  reads opcode  does something  ???  PROFIT
  • 23. Why do I care?  Use an opcode cache  If you don’t, you’re crazy, stupid, or lazy  Upgrade to get cooler stuff
  • 24. Variables PHP is a C types wrapper
  • 25. Zvals typedef 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  they’re 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 aren’t arrays  Beware of many many resources  Beware of many many objects  64 bit can be broken in strange ways
  • 33. C Moar Implementation 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  http://www.phpinternalsbook.com  http://php.net  http://lxr.php.net  http://wiki.php.net  http://nikic.github.io/  http://blog.krakjoe.ninja/
  • 41. About Me  http://emsmith.net  @auroraeosrose  That’s Aurora Eos Rose  auroraeosrose@gmail.com  freenode in #phpmentoring #phpwomen #phpinternals

Hinweis der Redaktion

  1. story of how I got into internals in the first place and how each new discovery (extensions, sapis, engine, oh look now I can do it all) led down the alice rabbit hole but it also made me a better PHP programmer because I knew all the WTFs
  2. So PHP does the architecture of it’s system right – it’s as big as it needs to be, and no bigger – but all the important components are pluggable and extendable which makes it awesome glue take a side track about learning more about programming and how down or up the stack is usually more valuable in general than going across stacks
  3. well talk more about this later, but this is the part that actually looks at and analyzes your source code and makes it actually like – talk to your cpu and run yes, this isn’t really different from say c# or java – the difference is WHAT it compiles to C compiles to machine code your system can immediately use c# goes to msil which is run on their runtime java gotes to bytecode that runs on the java vm smarty compiles a template to a (horrible) php file
  4. so the core functionality of PHP (in main) is kind of a mishmash – but generally it’s IO the php manual lies though – if you look up “core” functionality what is actually IN core is not nearly so much instead what you’re seeing is extensions you can’t “turn off” – well you can if you’re nuts try it sometime, PHP is really boring without it’s “standard lib”
  5. SAPIs provide the glue for interfacing PHP into an application. They define the ways in which data is passed between an application and PHP this is really what sets PHP apart this can also make or break you if you choose poorly a lot of sapi choice is dictated by server choice, although most have gone to fastcgi at this point, which although it’s an old protocol it works well and is stable and “shared nothing” for example python invented it’s own interface (wsgi) which requires a separate server that talks something your server actually talks (fastcgi, scgi) instead of using a pluggable model there are those that whine that PHP doesn’t have this “middleware” – it’s actually easily doable though – you could do a dedicated sapi or just use the embedded sapi sapis take care of setting up interpreter context, dealing optionally with headers and input args (don’t need to do anything, it’s entirely optional, if you want yur PHP code itself needed work) we could use more sapis! people run away from writing these which is sad – I recruit – have a list of ones I’d love to mentor you into writing 
  6. almost everything is an extension there are two types, regular and “zend” extensions zend extensions can “hook” engine behavior using opcodes 99.9% of the functionality you use comes from this
  7. SO – threading makes this a little weird – because MINIT is not run in new threads so ginit is called right before rinit if ZTS is on (annoying)
  8. so why is it important to know this stuff you need to know what extensions are available why you would compile your own PHP with all static extensions sharing can be limited when requests aren’t shared
  9. This is going to annoy some people because they go on and on about how PHP is “thread safe” – but really it’s not it’s kind of almost able to be threaded when compiled right most of the time others blame things on libraries no – no there are some very bad things in core that totally prevent this 
  10. ah “thread safety”
  11. Parallelism is the act of taking a large job, splitting it up into smaller ones, and doing them at once. People often use "parallel" and "concurrent" interchangably, but there is a subtle difference. Concurrency is necessary for parallelism but not the other way around. If I alternate between cooking eggs and pancakes I'm doing both concurrently. If I'm cooking eggs while you are cooking pancakes, we are cooking concurrently and in parallel. Technically if I'm cooking eggs and you are mowing the lawn we are also working in parallel, but since no coordination is needed in that case there's nothing to talk about.
  12. talk about what each of these means
  13. Lexical Analysis Converts the source from a sequence of characters into a sequence of tokens
  14. Syntax Analysis Analyzes a sequence of tokens to determine their grammaticalstructure
  15. 5.6 and 7+
  16. Generate bytecode based on the information gathered byanalyzing the sourcecode
  17. abstract syntax tree – decouples compiler and parser steps – even though we compile to opcode, it’s still a compile before php7 we emit opcodes directrly from parsing (a bit eww) now we can do better, cooler stuff
  18. so zend is actually a “virtual machine” it interprets OPCODES and does stuff with them reads each opcode and does a specific action – like a giant state machine
  19. underlying it all PHP just has some basic types
  20. every zval stores some value and the type this value has A union defines multiple members of different types, but only one of them can ever be used at a time unions store all their members at the same memory location and just interpret the value located there differently depending on which member you access. The size of the union is the size of its largest member so why do we care that this is how PHP stores stuff? at the end of the day those are actual C types underneath we’re just “mapping” to with many of the conversion rules that go along with it
  21. char = smallest addressable unit of the machine IEEE 754 single-precision binary floating-point format = float IEEE 754 double-precision binary floating-point format = double this is more precision – but remember to use gmp for real math
  22. The disadvantage of the LP64 model is that storing a long into an int may overflow converting a pointer to a long will “work” in LP64 useful for – the lazy LLP64 is generally the “safer” route – you can’t convert a pointer to a long (WTF WOULD YOU?) and a long to an int won’t overflow (BC considerations) therefore logical choice for windows handling of strings >= 2^31 handling of 64 bit integers large file support handling of numeric 64 bit hash keys Fixed in PHP7
  23. null terminated array of chars Another way of accessing a contiguous chunk of memory, instead of with an array, is with a pointer. the character array containing the string must already exist (having been either statically- or dynamically-allocated) C is a programming language that was developed in an environment where the dominant character set was the 7-bit ASCII code. Hence since then the 8-bit byte is the most common unit of encoding. However when a software is developed for an international purpose, it has to be able to represent different characters. For example character encoding schemes to represent the Indian, Chinese, Japanese writing systems should be available. The inconvenience of handling such varied multibyte characters can be eliminated by using characters that are simply a uniform number of bytes. ANSI C provides a type that allows manipulation of variable width characters as uniform sized data objects called wide character story of Microsoft’s early adoption, utf8 on a napkin
  24. Arrays in C are just regions of memory that can be accessed by offset offsets must be continuous integers complex key becomes integer via hash function for hash collisions – PHP stores all the items with the same hash in a linked list
  25. so – advantage of resources is they’re smaller than objecst in php disadvantages are very numerous they’re slow – depending on what you’re doing much slower than objects they’re limited – you can literally run out of resources, and they get shove in a giant list in the executor no seriously this is why they suck sadpanda
  26. like resources, these are stored in executor globals like resources eventually some day you might run out but you can sure do a lot more with them store opaque data, deal with opaque data, etc
  27. The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer. The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns. Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation). things get crazy when you add dynamically loaded modules – they may have their OWN heap
  28. fewer calls to malloc – less cpu usage, less kernel madness less fragmentation LIBRARIES do not use it! you can turn it off, but 99.9% of the time it’s better with it
  29. The behavior is very straightforward: When a reference is added, increment the refcount, if a reference is removed, decrement it. If the refcount reaches 0, the zval is destroyed.
  30. All values in existing Zend Engine implementation were allocated on heap and they were subject for reference counting and garbage collection. Zend engine mostly operated by pointers to zvals phpng stores data in a totally different wayand even though I saw an interesting talk on it at zendcon I’m stillw rapping my head around the code basically it seperates scalar from non-scalar and uses flags to give it information (type, etc)
  31. Assigning values by references when you don't need to (in order to later modify the original value through a different label) is NOT a case of you outsmarting the silly engine and gaining speed and performance. It's the opposite, it's you TRYING to outsmart the engine and failing, because the engine is already doing a better job than you think. in other words, references are basically useful for – digging into internal nested arrays and input/output parameters – that’s about it
  32. objects do behave in a references but remember a variable that is assigned to an object just holds a pointer to the actual value of the object – which is elsewhere if you later assign that
  33. I should blog more, there’s a lot in my damn head