Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Izhevsk PHP Meetup #3. Вместо keynote

Izhevsk PHP Meetup #3. Вместо keynote

Святослав Молодских, ресурс-менеджер, Центр Высоких Технологий
Это будет почти технический доклад с привкусом языка C и структур.

Izhevsk PHP Meetup

September 28, 2017
Tweet

More Decks by Izhevsk PHP Meetup

Other Decks in Technology

Transcript

  1. Кто я? • Святослав Молодских • Работаю 8 лет в

    одной компании, большую часть времени программистом • Перешёл на тёмную сторону
  2. zval // zend_types.h typedef struct _zval_struct zval; // zend.h struct

    _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount__gc; zend_uchar type;/* active type */ zend_uchar is_ref__gc; };
  3. zvalue_value // zend.h typedef union _zvalue_value { long lval; /*

    long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj; zend_ast *ast; } zvalue_value;
  4. Снова zval // zend.h struct _zval_struct { /* Variable information

    */ zvalue_value value; /* value */ zend_uint refcount__gc; zend_uchar type;/* active type */ zend_uchar is_ref__gc; };
  5. "foo": { "type": "string", "value": { "str": { "val": "foo",

    "len": 3 } }, "refcount": 1, "is_ref": 0 } $foo = 'foo';
  6. "foo", "bar": { "type": "string", "value": { "str": { "val":

    "foo", "len": 3 } }, "refcount": 2, "is_ref": 0 } $foo = ‘foo'; $bar = $foo;
  7. "bar": { "type": "string", "value": { "str": { "val": "foo",

    "len": 3 } }, "refcount": 1, "is_ref": 0 } $foo = 'foo'; $bar = $foo; unset($foo);
  8. $foo = 'foo'; $bar = $foo; $bar .= 'bar'; "foo":

    { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 1, "is_ref": 0 }, "bar": { "type": "string", "value": { "str": { "val": "foobar", "len": 6 } }, "refcount": 1, "is_ref": 0 }
  9. COW

  10. $foo = 'foo'; $bar = $foo; $baz = &$foo; "baz",

    "foo": { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 2, "is_ref": 1 }, "bar": { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 1, "is_ref": 0 }
  11. $foo = 'foo'; $bar = $foo; $baz = &$foo; $spam

    = $foo; "baz", "foo": { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 2, "is_ref": 1 }, "bar": { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 1, "is_ref": 0 }, "spam": { "type": "string", "value": { "str": { "val": "foo", "len": 3 } }, "refcount": 1, "is_ref": 0 }
  12. PHP 7 /* regular data types */ #define IS_UNDEF 0

    #define IS_NULL 1 #define IS_FALSE 2 #define IS_TRUE 3 #define IS_LONG 4 #define IS_DOUBLE 5 #define IS_STRING 6 #define IS_ARRAY 7 #define IS_OBJECT 8 #define IS_RESOURCE 9 #define IS_REFERENCE 10 /* constant expressions */ #define IS_CONSTANT 11 #define IS_CONSTANT_AST 12
  13. zval struct _zval_struct { zend_value value; /* value */ union

    { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2; };
  14. zend_value typedef union _zend_value { zend_long lval; /* long value

    */ double dval; /* double value */ 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; struct { uint32_t w1; uint32_t w2; } ww; } zend_value;
  15. zend_refcounted typedef struct _zend_refcounted zend_refcounted; struct _zend_refcounted { zend_refcounted_h gc;

    }; typedef struct _zend_refcounted_h { uint32_t refcount; /* reference counter 32-bit */ union { struct { ZEND_ENDIAN_LOHI_3( zend_uchar type, zend_uchar flags, /* used for strings & objects */ uint16_t gc_info) /* keeps GC root number (or 0) and color */ } v; uint32_t type_info; } u; } zend_refcounted_h;
  16. $foo $foo = []; zend_array_1(refcount=1, val=[]) $bar = $foo; $foo,

    $bar zend_array_1(refcount=2, val=[]) $baz = $bar; $foo, $bar, $baz zend_array_1(refcount=3, val=[]) $spam = &$baz; $foo, $bar zend_array_1(refcount=3, val=[]) $baz, $spam zend_reference_1(refcount=2) $spam[] = 1; $foo, $bar zend_array_1(refcount=2, val=[]) $baz, $spam zend_reference_1(refcount=2) zend_array_1(refcount=1, val=[1])