phpng-int

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
phpng-int [2014/05/05 19:17] dmitryphpng-int [2018/01/09 16:34] (current) – fixed typos kelunik
Line 1: Line 1:
 +====== PHPNG Implementation Details ======
 +
 +This page provides technical information about PHPNG internals. The general information about PHPNG might be found at [[phpng]], information for extension maintainers at [[phpng-upgrading]]. 
 +
 ===== Value Representation ===== ===== Value Representation =====
  
-All values in existing Zend Engine implementation were allocated on heap and they are subject for reference counting and garbage collection. Zend engine mostly operates by pointers to zvals (in many places even by zval**+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 (in many places even by pointers to pointers to zval) 
  
-The new implementation operates by zval structures their selves (not pointers). It stores new zval structures directly on stack, in HashTable buckets, and property slots. It dramatically reduces number of heap allocations/deallocations. It also avoids reference counting and garbage collection on primitive values (null, bool, long, double, interned string).+The new implementation operates by zval structures their selves (not pointers). It stores new zval structures directly on VM stack, in HashTable buckets, and property slots. It dramatically reduces number of heap allocations/deallocations. It also avoids reference counting and garbage collection on primitive values (null, bool, long, double, interned string, immutable arrays).
  
-The new implementation uses more VM stack space (instead of heap), because now it keeps there zval structures instead of pointers. Anyway, the overall memory usage is reduced. In some cases new approach assumes full zval copying instead of copy-on write implemented before, but it doesn't make performance penalties (it require two memory reads and two memory stores instead of single memory read/store + incrementing reference counter before that leads to the same 2 reads/stores).+The new implementation uses more VM stack space (instead of heap), because now it keeps there zval structures instead of pointers. Anyway, the overall memory usage is reduced. In some cases new approach assumes full zval copying instead of copy-on-write implemented before, but it doesn't make performance penalties (it require two memory reads and two memory stores instead of single memory read/store + reference counter increment, before thatleads to the same 2 reads/stores).
  
 ==== CELL Format (zval) ==== ==== CELL Format (zval) ====
Line 17: Line 21:
 </code> </code>
  
-The value cell represented as two 64-bit words. The first word contains actual value (it's defined as a union of possible value types), the second contains type tag and some flags. The type and flags may be accessed together as a single 32-bit world for efficiency. The “unused” space actually may be reused for different purposes when cell are embedded into other structures. (e.g. for hash collision list when the value embedded into HashTable)+The value cell represented as two 64-bit words. The first word contains actual value (it's defined as a union of possible value types), the second contains type tag and some flags. The type and flags may be accessed together as a single 32-bit word for efficiency. The “unused” space actually may be reused for different purposes when cell are embedded into other structures. (e.g. for hash collision list when the value embedded into HashTable)
  
-The re-factored engine defines the following data types most of that are well known from the exiting engine:+The re-factored engine defines the following data types. Most of them are well known from PHP-5 engine:
  
   * IS_UNDEF – we use a special type for undefined variables   * IS_UNDEF – we use a special type for undefined variables
Line 27: Line 31:
   * IS_LONG   * IS_LONG
   * IS_DOUBLE   * IS_DOUBLE
-  * IS_STRING – interned or dynamic string +  * IS_STRING – regular or interned string 
-  * IS_ARRAY+  * IS_ARRAY – regular or immutable array
   * IS_OBJECT   * IS_OBJECT
   * IS_RESOURCE   * IS_RESOURCE
-  * IS_REFERENCE – a separate type for references (t'll be explained later)+  * IS_REFERENCE – a separate type for references (it'll be explained later)
  
   * IS_CONSTANT – named constant   * IS_CONSTANT – named constant
-  * IS_CONSTANT_ARRAY – array with constants 
   * IS_CONSTANT_AST – constant expression   * IS_CONSTANT_AST – constant expression
  
Line 46: Line 49:
 Except for types itself, the engine defines few type flags to uniform handling of different data types with similar behavior. Except for types itself, the engine defines few type flags to uniform handling of different data types with similar behavior.
  
-  * IS_TYPE_CONSTANT – the type is a constant (IS_CONSTANT, IS_CONSTANT_ARRAY, IS_CONSTANT_AST) +  * IS_TYPE_CONSTANT – the type is a constant (IS_CONSTANT, IS_CONSTANT_AST) 
-  * IS_TYPE_REFCOUNTED – the type is a subject for reference counting (IS_STRING excluding interned srings, IS_ARRAY, IS_OBJECT, IS_RESOURCE, IS_REFERENCE). Values for all refcounted types are pointers to corresponding structures having common part (zend_refcounted). It's possible to get this structure using Z_COUNTD() macro or some data from that structure using Z_GC_TYPE(), Z_GC_FLAGS(), G_GC_INFO() and Z_GC_TYPE_INFO(). It's also possible to acess reference counter using Z_REFCOUNT(), Z_SET_REFCOUNT(), Z_ADDREF() and Z_DELREF().+  * IS_TYPE_REFCOUNTED – the type is a subject for reference counting (IS_STRING excluding interned srings, IS_ARRAY except for immutable arrays, IS_OBJECT, IS_RESOURCE, IS_REFERENCE). Values for all refcounted types are pointers to corresponding structures having common part (zend_refcounted). It's possible to get this structure using Z_COUNTD() macro or some data from that structure using Z_GC_TYPE(), Z_GC_FLAGS(), G_GC_INFO() and Z_GC_TYPE_INFO(). It's also possible to access reference counter using Z_REFCOUNT(), Z_SET_REFCOUNT(), Z_ADDREF() and Z_DELREF() macros.
   * IS_TYPE_COLLECTABLE – the type may be a root of unreferenced cycle and it's a subject for Garbage Collection (IS_ARRAY, IS_OBJECT).   * IS_TYPE_COLLECTABLE – the type may be a root of unreferenced cycle and it's a subject for Garbage Collection (IS_ARRAY, IS_OBJECT).
   * IS_TYPE_COPYABLE – the type has to be duplicated using zval_copy_ctor() on assignment or copy on write (IS_STRING excluding interned strings, IS_ARRAY)   * IS_TYPE_COPYABLE – the type has to be duplicated using zval_copy_ctor() on assignment or copy on write (IS_STRING excluding interned strings, IS_ARRAY)
 +  * IS_TYPE_IMMUTABLE - the type can't be changed directly, but may be copied on write. Used by immutable arrays to avoid unnecessary array duplication.
 +
 +Few constants flags are used as modifiers for IS_CONSTANT. Their meaning kept exactly the same as before.
  
-Few constants are flags used as modifiers for IS_CONSTANT. Their meaning kept exacly the same as before.+  * IS_CONSTANT_UNQUALIFIED 
 +  * IS_LEXICAL_VAR 
 +  * IS_LEXICAL_REF 
 +  * IS_CONSTANT_IN_NAMESPACE
  
-The type may be read using Z_TYPE() or Z_TYPE_P() macros, type flags using Z_TYPE_FLAGS() or Z_TYPE_FLAGS_P(), the combination of type and flags – Z_TYPE_INFO() or Z_TYPE_INFO_P().+The type of zval may be read using Z_TYPE() or Z_TYPE_P() macros, type flags using Z_TYPE_FLAGS() or Z_TYPE_FLAGS_P(), the combination of type and flags – Z_TYPE_INFO() or Z_TYPE_INFO_P(). **PHPNG doesn't work with pointers to pointers to zval and it doesn't provide macros with _PP() suffix anymore (like Z_TYPE_PP).**
  
 ==== IS_UNDEF ==== ==== IS_UNDEF ====
Line 110: Line 119:
 | DOUBLE VALUE (64-bit)                                                            | | DOUBLE VALUE (64-bit)                                                            |
 +--------------------------+-------------+-------------+-------------+-------------+ +--------------------------+-------------+-------------+-------------+-------------+
-| UNUSED (32-bit)          |           0 |           0 |           0 | IS_LONG     |+| UNUSED (32-bit)          |           0 |           0 |           0 | IS_DOUBLE   |
 +--------------------------+-------------+-------------+-------------+-------------+ +--------------------------+-------------+-------------+-------------+-------------+
  63                      32 31         24 23         16 15          8 7           0  63                      32 31         24 23         16 15          8 7           0
Line 197: Line 206:
 +----------------------------------------------------------------------------------+ +----------------------------------------------------------------------------------+
 | key - pointer to zend_string (NULL for numeric indexes) (64-bit or 32-bit)       | | key - pointer to zend_string (NULL for numeric indexes) (64-bit or 32-bit)       |
-+----------------------------------------------------------------------------------+ ++----------------------------------------------------------------------------------+  -+ 
-| VALUE (64-bit)                                                                    <- Embedded zval +| VALUE (64-bit)                                                                     |  
-+--------------------------+-------------+-------------+-------------+-------------+ ++--------------------------+-------------+-------------+-------------+-------------+   +- Embedded zval 
-| next- hash collision list| UNUSED      | const_flags | type_flags  | TYPE        | +| next- hash collision list| UNUSED      | const_flags | type_flags  | TYPE        |   
-+--------------------------+-------------+-------------+-------------+-------------+++--------------------------+-------------+-------------+-------------+-------------+  -+
  63                      32 31         24 23         16 15          8 7           0  63                      32 31         24 23         16 15          8 7           0
 </code> </code>
Line 309: Line 318:
 The check if the value is a reference might be done using Z_ISREF() macro, the reference value read using Z_REF() and Z_REFVAL() macros. They may be constructed using ZVAL_REF(), ZVAL_NEW_REF() and ZVAL_NEW_PERSISTENT_REF(). The check if the value is a reference might be done using Z_ISREF() macro, the reference value read using Z_REF() and Z_REFVAL() macros. They may be constructed using ZVAL_REF(), ZVAL_NEW_REF() and ZVAL_NEW_PERSISTENT_REF().
  
-==== IS_CONSTANT, IS_CONSTANT_ARRAY and IS_CONSTANT_AST ====+==== IS_CONSTANT and IS_CONSTANT_AST ====
  
-IS_CONSTANT actually points to zend_string structure, IS_CONSTANT_ARRAY to zend_array. They are handled a bit differently from regular strings and arrays.+IS_CONSTANT actually points to zend_string structure. They are handled a bit differently from regular strings and arrays.
  
 ==== IS_INDIRECT ==== ==== IS_INDIRECT ====
Line 325: Line 334:
 New implementation assumes, that we store zval structures (not pointers) in arrays and function stack frames. It must not be a problem for arrays because scalar values are going to be just duplicated, and compound values may point to shared reference-couned structures anyway. However it is a problem for  local variables (IS_CV), because they may be referenced through stack frame (by index) and through symbol table (by name). Both must point to the same structure. Values of IS_INDIRECT types are just weak pointers to real values. When we lazily create local symbol tables, we store IS_INDIRECT values in symbol tables and initialize them with the pointers to corresponding CV slots. It means that CV access by index became extremely efficient, as we don't need to perform double or even triple dereferences as before. New implementation assumes, that we store zval structures (not pointers) in arrays and function stack frames. It must not be a problem for arrays because scalar values are going to be just duplicated, and compound values may point to shared reference-couned structures anyway. However it is a problem for  local variables (IS_CV), because they may be referenced through stack frame (by index) and through symbol table (by name). Both must point to the same structure. Values of IS_INDIRECT types are just weak pointers to real values. When we lazily create local symbol tables, we store IS_INDIRECT values in symbol tables and initialize them with the pointers to corresponding CV slots. It means that CV access by index became extremely efficient, as we don't need to perform double or even triple dereferences as before.
  
-Global symbol tales are handled a bit differently. When we enter into some user code that uses global variables, we copy them from EG(symbol_table) into CV slots and initialize symtable values with IS_INDIRECT pointers. On exit we have to restore them back.+Global symbol tables are handled a bit differently. When we enter into some user code that uses global variables, we copy them from EG(symbol_table) into CV slots and initialize symtable values with IS_INDIRECT pointers. On exit we have to restore them back.
  
 The same concept is used for object properties access. In case dynamic properties table is required it's first initialized with IS_INDIRECT references to predefined object properties slots. The same concept is used for object properties access. In case dynamic properties table is required it's first initialized with IS_INDIRECT references to predefined object properties slots.
Line 358: Line 367:
 ===== VM Changes ===== ===== VM Changes =====
  
-With new zval implementation IS_TMP_VAR, IS_VAR and IS_CV operands are handled in very similar way. All three operands jut refer to certain slot of the current function stack frame. Such slots are allocated on segmented VM stack together with frame header (zend_execute_data). The first slots correspond to CV variables and the following to IS_TMP_VAR and IS_VAR. Except for local and temporary variables we also allocate space for syntactically nested function calls and actual parameters, that this function may push.+With new zval implementation IS_TMP_VAR, IS_VAR and IS_CV operands are handled in very similar way. All three operands just refer to certain slot of the current function stack frame. Such slots are allocated on segmented VM stack together with frame header (zend_execute_data). The first slots correspond to CV variables and the following to IS_TMP_VAR and IS_VAR. Except for local and temporary variables we also allocate space for syntactically nested function calls and actual parameters, that this function may push.
  
 ==== Function Stack Frame (zend_execute_data) ==== ==== Function Stack Frame (zend_execute_data) ====
Line 416: Line 425:
  
 [TO BE CONTINUED] [TO BE CONTINUED]
- 
phpng-int.1399317420.txt.gz · Last modified: 2017/09/22 13:28 (external edit)