class Foo { public $a = "foobarstring"; public $b; public $c = ['some', 'values']; } for ($i=0; $i<1000; $i++) { $m = memory_get_usage(); ${'var'.$i} = new Foo; echo memory_get_usage() - $m"\n"; }
for ($i=0; $i<1000; $i++) { $m = memory_get_usage(); ${'var'.$i} = [['some', 'values'], null, 'foobarstring']; echo memory_get_usage() - $m . "\n"; }
foobarstring
. That's just consumed already 1160 bytes of memory, which is 4.4 times more. $class = <<<'CL' class Foo { public $a = "foobarstring"; public $b; public $c = ['some', 'values']; } CL; echo memory_get_usage() . "\n"; eval($class); echo memory_get_usage() . "\n";
eval()
operator to declare and measure the memory used (using the PHP memory manager eval()
. In this case, no objects in this code are not created. The volume of involved memory (diff memory) is 2216 bytes. struct _zend_class_entry { char type; const char *name; zend_uint name_length; struct _zend_class_entry *parent; int refcount; zend_uint ce_flags; HashTable function_table; HashTable properties_info; zval **default_properties_table; zval **default_static_members_table; zval **static_members_table; HashTable constants_table; int default_properties_count; int default_static_members_count; union _zend_function *constructor; union _zend_function *destructor; union _zend_function *clone; union _zend_function *__get; union _zend_function *__set; union _zend_function *__unset; union _zend_function *__isset; union _zend_function *__call; union _zend_function *__callstatic; union _zend_function *__tostring; union _zend_function *serialize_func; union _zend_function *unserialize_func; zend_class_iterator_funcs iterator_funcs; /* handlers */ zend_object_value (*create_object)(zend_class_entry *class_type TSRMLS_DC); zend_object_iterator *(*get_iterator)(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC); int (*interface_gets_implemented)(zend_class_entry *iface, zend_class_entry *class_type TSRMLS_DC); /* a class implements this interface */ union _zend_function *(*get_static_method)(zend_class_entry *ce, char* method, int method_len TSRMLS_DC); /* serializer callbacks */ int (*serialize)(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC); int (*unserialize)(zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC); zend_class_entry **interfaces; zend_uint num_interfaces; zend_class_entry **traits; zend_uint num_traits; zend_trait_alias **trait_aliases; zend_trait_precedence **trait_precedences; union { struct { const char *filename; zend_uint line_start; zend_uint line_end; const char *doc_comment; zend_uint doc_comment_len; } user; struct { const struct _zend_function_entry *builtin_functions; struct _zend_module_entry *module; } internal; } info; };
zend_class_entry
, using more than half a kilobyte of dynamic memory for this purpose. Of course, this is not limited to this: as you noticed, the structure contains quite a few pointers, which also need to be placed in memory. That is, classes themselves consume much more memory than all objects created from them afterwards.OPArray
it OPArray
, which means the more memory it consumes. Add to this static variables that can be declared in the method. Next are the attributes, later they will also be placed in memory. The volume depends on their default values: the integers will take a little, but a large static array will eat a lot of memory.zend_class_entry
- about PHP comments. They are also known as annotations. These are string variables (in C language, char*
buffers) that must also be stored in memory. For C, which does not use Unicode, unlike PHP, the rule is very simple: one character = one byte. The more annotations you have in a class, the more memory will be used after parsing.zend_class_entry
field contains class annotations. Methods and attributes also have such a field. if (EG(full_tables_cleanup)) { zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function_full TSRMLS_CC); zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class_full TSRMLS_CC); } else { zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function TSRMLS_CC); zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class TSRMLS_CC); } static int clean_non_persistent_class(zend_class_entry **ce TSRMLS_DC) { return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE; }
zend_class_entry
- zend_class_entry
. And as you have seen, this structure is very cumbersome. Sometimes developers declare interfaces in code to be able to use their names in catch blocks. This allows you to catch only certain types of exceptions. For example: interface BarException { } class MyException extends Exception implements BarException { } try { $foo->bar(): } catch (BarException $e) { }
$class = <<<'CL' interface Bar { } CL; $m = memory_get_usage(); eval($class); echo memory_get_usage() - $m . "\n"; /* 912 bytes */
zend_class_entry
structure zend_class_entry
not go anywhere, just a number of fields, including the static_members_table
, will not be pointers in memory. Declaring a class, equivalent trait, or equivalent interface will require the same amount of memory, since they all use the same structure. /* Single class */ case ZEND_DECLARE_CLASS: if (do_bind_class(CG(active_op_array), opline, CG(class_table), 1 TSRMLS_CC) == NULL) { return; } table = CG(class_table); break;
do_bind_class()
. This function only registers a fully defined class in the class table for further use at runtime, and also checks for possible abstract methods: void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC) { zend_abstract_info ai; if ((ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) && !(ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) { memset(&ai, 0, sizeof(ai)); zend_hash_apply_with_argument(&ce->function_table, (apply_func_arg_t) zend_verify_abstract_class_function, &ai TSRMLS_CC); if (ai.cnt) { zend_error(E_ERROR, "Class %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")", ce->name, ai.cnt, ai.cnt > 1 ? "s" : "", DISPLAY_ABSTRACT_FN(0), DISPLAY_ABSTRACT_FN(1), DISPLAY_ABSTRACT_FN(2) ); } } }
ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC) { /* ... ... */ } else { if (ce->num_interfaces >= current_iface_num) { if (ce->type == ZEND_INTERNAL_CLASS) { ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num)); } else { ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num)); } } ce->interfaces[ce->num_interfaces++] = iface; zend_hash_merge_ex(&ce->constants_table, &iface->constants_table, (copy_ctor_func_t) zval_add_ref, sizeof(zval *), (merge_checker_func_t) do_inherit_constant_check, iface); zend_hash_merge_ex(&ce->function_table, &iface->function_table, (copy_ctor_func_t) do_inherit_method, sizeof(zend_function), (merge_checker_func_t) do_inherit_method_check, ce); do_implement_interface(ce, iface TSRMLS_CC); zend_do_inherit_interfaces(ce, iface TSRMLS_CC); } }
realloc()
for memory allocation, the second - erealloc()
. realloc()
allocates “permanent” memory, and erealloc()
“allocated on demand” memory.zval_add_ref
. It does not copy constants from one table to another, but shares their pointers, simply by adding the number of references.do_inherit_method
used: static void do_inherit_method(zend_function *function) { function_add_ref(function); } ZEND_API void function_add_ref(zend_function *function) { if (function->type == ZEND_USER_FUNCTION) { zend_op_array *op_array = &function->op_array; (*op_array->refcount)++; if (op_array->static_variables) { HashTable *static_variables = op_array->static_variables; zval *tmp_zval; ALLOC_HASHTABLE(op_array->static_variables); zend_hash_init(op_array->static_variables, zend_hash_num_elements(static_variables), NULL, ZVAL_PTR_DTOR, 0); zend_hash_copy(op_array->static_variables, static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_zval, sizeof(zval *)); } op_array->run_time_cache = NULL; } }
zval_add_ref
(here it is a method). Thus, a lot of computational resources are needed for the entire copying process, because many cycles and checks are involved here. But memory is used a little. Unfortunately, today the interface binding runs completely at runtime, and you will feel it with every request. Perhaps the developers will soon change it. /* good */ class A { } class B extends A { }
/* bad */ class B extends A { } class A { }
/* */ class B extends A { } class A { }
/* */ Fatal error: Class 'B' not found */ class C extends B { } class B extends A { } class A { }
zend_function_structure
. Only the names are different. Methods are compiled and added to the function_table
attribute in zend_class_entry
. Therefore, at run time, each method is presented; it is only a matter of translating a pointer to execution. typedef union _zend_function { zend_uchar type; struct { zend_uchar type; const char *function_name; zend_class_entry *scope; zend_uint fn_flags; union _zend_function *prototype; zend_uint num_args; zend_uint required_num_args; zend_arg_info *arg_info; } common; zend_op_array op_array; zend_internal_function internal_function; } zend_function;
__call()
called. It also checks the visibility - public / protected / private - depending on what the following actions are taken: static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC) { zend_function *fbc; zval *object = *object_ptr; zend_object *zobj = Z_OBJ_P(object); ulong hash_value; char *lc_method_name; ALLOCA_FLAG(use_heap) if (EXPECTED(key != NULL)) { lc_method_name = Z_STRVAL(key->constant); hash_value = key->hash_value; } else { lc_method_name = do_alloca(method_len+1, use_heap); zend_str_tolower_copy(lc_method_name, method_name, method_len); hash_value = zend_hash_func(lc_method_name, method_len+1); } /* If the method is not found */ if (UNEXPECTED(zend_hash_quick_find(&zobj->ce->function_table, lc_method_name, method_len+1, hash_value, (void **)&fbc) == FAILURE)) { if (UNEXPECTED(!key)) { free_alloca(lc_method_name, use_heap); } if (zobj->ce->__call) { /* if the class has got a __call() handler */ return zend_get_user_call_function(zobj->ce, method_name, method_len); /* call the __call() handler */ } else { return NULL; /* else return NULL, which will likely lead to a fatal error : method not found */ } } /* Check access level */ if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) { zend_function *updated_fbc; updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len, hash_value TSRMLS_CC); if (EXPECTED(updated_fbc != NULL)) { fbc = updated_fbc; } else { if (zobj->ce->__call) { fbc = zend_get_user_call_function(zobj->ce, method_name, method_len); } else { zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : ""); } } } else { /* ... ... */ }
if (EXPECTED(key != NULL)) { lc_method_name = Z_STRVAL(key->constant); hash_value = key->hash_value; } else { lc_method_name = do_alloca(method_len+1, use_heap); /* Create a zend_copy_str_tolower(dest, src, src_length); */ zend_str_tolower_copy(lc_method_name, method_name, method_len); hash_value = zend_hash_func(lc_method_name, method_len+1); }
zend_str_tolower_copy()
) before calling it. Not really everyone, but those where there is an if
. The key
variable prevents the execution of a function that converts to lower case (the part with the else
) - this is part of the PHP optimization implemented in version 5.4. If the method call is not dynamic, then the compiler has already calculated the key
, and less resources are spent during the execution. class Foo { public function BAR() { } } $a = new Foo; $b = 'bar'; $a->bar(); /* static call : good */ $a->$b(); /* dynamic call : bad */
BAR()
bar()
.key
“bar”, , . , “$b”, key
. , , ( zend_hash_func()
), .__call()
, . , , .zend_object
32 : typedef struct _zend_object { zend_class_entry *ce; HashTable *properties; zval **properties_table; HashTable *guards; /* protects from __get/__set ... recursion */ } zend_object;
zend_object_store
. Zend — , : ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC) { zend_object_value retval; *object = emalloc(sizeof(zend_object)); (*object)->ce = class_type; (*object)->properties = NULL; (*object)->properties_table = NULL; (*object)->guards = NULL; /* Add the object into the store */ retval.handle = zend_objects_store_put(*object, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC); retval.handlers = &std_object_handlers; return retval; }
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) { int i; if (class_type->default_properties_count) { object->properties_table = emalloc(sizeof(zval*) * class_type->default_properties_count); for (i = 0; i < class_type->default_properties_count; i++) { object->properties_table[i] = class_type->default_properties_table[i]; if (class_type->default_properties_table[i]) { #if ZTS ALLOC_ZVAL( object->properties_table[i]); MAKE_COPY_ZVAL(&class_type->default_properties_table[i], object->properties_table[i]); #else Z_ADDREF_P(object->properties_table[i]); #endif } } object->properties = NULL; } }
zval*
, . PHP refcount, Zend (ZTS, Zend thread safety), zval
. , ZTS ZTS PHP.properties_table
properties zend_object
?zend_property_info
. typedef struct _zend_property_info { zend_uint flags; const char *name; int name_length; ulong h; int offset; const char *doc_comment; int doc_comment_len; zend_class_entry *ce; } zend_property_info;
property_info
zend_class_entry
. : class Foo { public $a = 'foo'; protected $b; private $c; } struct _zend_class_entry { /* ... ... */ HashTable function_table; HashTable properties_info; /* here are the properties infos about $a, $b and $c */ zval **default_properties_table; /* and here, we'll find $a, $b and $c with their default values */ int default_properties_count; /* this will have the value of 3 : 3 properties */ /* ... ... */
Properties_infos
, . , object->properties
. scope (public/protected/private).__set()
. , object->property_table
. property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__set != NULL), key TSRMLS_CC); if (EXPECTED(property_info != NULL) && ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) && property_info->offset >= 0) ? (zobj->properties ? ((variable_ptr = (zval**)zobj->properties_table[property_info->offset]) != NULL) : (*(variable_ptr = &zobj->properties_table[property_info->offset]) != NULL)) : (EXPECTED(zobj->properties != NULL) && EXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &variable_ptr) == SUCCESS)))) { /* ... ... */ } else { zend_guard *guard = NULL; if (zobj->ce->__set && /* class has a __set() ? */ zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS && !guard->in_set) { Z_ADDREF_P(object); if (PZVAL_IS_REF(object)) { SEPARATE_ZVAL(&object); } guard->in_set = 1; /* prevent circular setting */ if (zend_std_call_setter(object, member, value TSRMLS_CC) != SUCCESS) { /* call __set() */ } guard->in_set = 0; zval_ptr_dtor(&object); /* ... ... */
function foo($var) { $var = 42; } $o = new MyClass; foo($o); var_dump($o); /* this is still an object, not the integer 42 */
zval
, , , , . . zval
, .object(MyClass)#1 (0) { } /* #1 is the object handle (number), it is unique */
Zend_object_store
. new
, unserialize()
, reflection API clone
. . typedef struct _zend_objects_store { zend_object_store_bucket *object_buckets; zend_uint top; zend_uint size; int free_list_head; } zend_objects_store; typedef struct _zend_object_store_bucket { zend_bool destructor_called; zend_bool valid; zend_uchar apply_count; union _store_bucket { struct _store_object { void *object; zend_objects_store_dtor_t dtor; zend_objects_free_object_storage_t free_storage; zend_objects_store_clone_t clone; const zend_object_handlers *handlers; zend_uint refcount; gc_root_buffer *buffered; } obj; struct { int next; } free_list; } bucket; } zend_object_store_bucket;
$this
, : , .. $this
, — , «» . .$this
. , $this
, . /* ... ... */ if (opline_is_fetch_this(last_op TSRMLS_CC)) { zend_error(E_COMPILE_ERROR, "Cannot re-assign $this"); } /* ... ... */ static zend_bool opline_is_fetch_this(const zend_op *opline TSRMLS_DC) { if ((opline->opcode == ZEND_FETCH_W) && (opline->op1_type == IS_CONST) && (Z_TYPE(CONSTANT(opline->op1.constant)) == IS_STRING) && ((opline->extended_value & ZEND_FETCH_STATIC_MEMBER) != ZEND_FETCH_STATIC_MEMBER) && (Z_HASH_P(&CONSTANT(opline->op1.constant)) == THIS_HASHVAL) && (Z_STRLEN(CONSTANT(opline->op1.constant)) == (sizeof("this")-1)) && !memcmp(Z_STRVAL(CONSTANT(opline->op1.constant)), "this", sizeof("this"))) { return 1; } else { return 0; } }
$this
? , OPCode INIT_METHOD_CALL
. , , $a->foo()
$a
. $a
. OPCode DO_FCALL
. ( ) $this
- — EG(This)
. if (fbc->type == ZEND_USER_FUNCTION || fbc->common.scope) { should_change_scope = 1; EX(current_this) = EG(This); EX(current_scope) = EG(scope); EX(current_called_scope) = EG(called_scope); EG(This) = EX(object); /* fetch the object prepared in previous INIT_METHOD opcode and affect it to EG(This) */ EG(scope) = (fbc->type == ZEND_USER_FUNCTION || !EX(object)) ? fbc->common.scope : NULL; EG(called_scope) = EX(call)->called_scope; }
$this
(, $this->a
= 8), OPCode ZEND_ASSIGN_OBJ
, , , $this
EG(This)
. static zend_always_inline zval **_get_obj_zval_ptr_ptr_unused(TSRMLS_D) { if (EXPECTED(EG(This) != NULL)) { return &EG(This); } else { zend_error_noreturn(E_ERROR, "Using $this when not in object context"); return NULL; } }
$this
(, $this->foo()
) ( $this->foo($this);
), $this
, . : if (op_array->this_var != -1 && EG(This)) { Z_ADDREF_P(EG(This)); if (!EG(active_symbol_table)) { EX_CV(op_array->this_var) = (zval **) EX_CV_NUM(execute_data, op_array->last_var + op_array->this_var); *EX_CV(op_array->this_var) = EG(This); } else { if (zend_hash_add(EG(active_symbol_table), "this", sizeof("this"), &EG(This), sizeof(zval *), (void **) EX_CV_NUM(execute_data, op_array->this_var))==FAILURE) { Z_DELREF_P(EG(This)); } } }
if (fbc->type == ZEND_USER_FUNCTION || fbc->common.scope) { /* ... ... */ EG(scope) = (fbc->type == ZEND_USER_FUNCTION || !EX(object)) ? fbc->common.scope : NULL; /* ... ... */ }
EG(scope)
zend_class_entry
. , . , : static zend_always_inline int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce TSRMLS_DC) { switch (property_info->flags & ZEND_ACC_PPP_MASK) { case ZEND_ACC_PUBLIC: return 1; case ZEND_ACC_PROTECTED: return zend_check_protected(property_info->ce, EG(scope)); case ZEND_ACC_PRIVATE: if ((ce==EG(scope) || property_info->ce == EG(scope)) && EG(scope)) { return 1; } else { return 0; } break; } return 0; }
class A { private $a; public function foo(A $obj) { $this->a = 'foo'; $obj->a = 'bar'; /* yes, this is possible */ } } $a = new A; $b = new A; $a->foo($b);
class Foo { public function __destruct() { echo "byebye foo"; } } $f = new Foo; thisfunctiondoesntexist(); /* fatal error, function not found, the Foo's destructor is NOT run */
void shutdown_destructors(TSRMLS_D) { zend_try { int symbols; do { symbols = zend_hash_num_elements(&EG(symbol_table)); zend_hash_reverse_apply(&EG(symbol_table), (apply_func_t) zval_call_destructor TSRMLS_CC); } while (symbols != zend_hash_num_elements(&EG(symbol_table))); zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC); } zend_catch { /* if we couldn't destruct cleanly, mark all objects as destructed anyway */ zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC); } zend_end_try(); } static int zval_call_destructor(zval **zv TSRMLS_DC) { if (Z_TYPE_PP(zv) == IS_OBJECT && Z_REFCOUNT_PP(zv) == 1) { return ZEND_HASH_APPLY_REMOVE; } else { return ZEND_HASH_APPLY_KEEP; } }
class Foo { public function __destruct() { var_dump("destroyed Foo"); } }
class Bar { public function __destruct() { var_dump("destroyed Bar"); } }
$a = new Foo; $b = new Bar; "destroyed Bar" "destroyed Foo"
$a = new Bar; $b = new Foo; "destroyed Foo" "destroyed Bar"
$a = new Bar; $b = new Foo; $c = $b; /* increment $b's object refcount */ "destroyed Bar" "destroyed Foo"
class Foo { public function __destruct() { var_dump("destroyed Foo"); die();} } /* notice the die() here */ class Bar { public function __destruct() { var_dump("destroyed Bar"); } } $a = new Foo; $a2 = $a; $b = new Bar; $b2 = $b; destroyed Foo
__destruct()
. PHP , . , .__get()
__set()
. - , , ( 8 , ulimit –s
) PHP. class Foo { public function __destruct() { new Foo; } /* you will crash */ }
Source: https://habr.com/ru/post/255237/
All Articles