# Zend Engine ## Zend memory manager ### General The goal of the new memory manager (available since PHP 5.2) is to reduce memory allocation overhead and speedup memory management. ### Debugging Normal: ```bash sapi/cli/php -r 'leak();' ``` Zend MM disabled: ```bash USE_ZEND_ALLOC=0 valgrind --leak-check=full sapi/cli/php -r 'leak();' ``` ### Shared extensions Since PHP 5.3.11 it is possible to prevent shared extensions from unloading so that valgrind can correctly track the memory leaks in shared extensions. For this there is the `ZEND_DONT_UNLOAD_MODULES` environment variable. If set, then `DL_UNLOAD()` is skipped during the shutdown of shared extensions. ## ZEND_VM `ZEND_VM` architecture allows specializing opcode handlers according to `op_type` fields and using different execution methods (call threading, switch threading and direct threading). As a result ZE2 got more than 20% speedup on raw PHP code execution (with specialized executor and direct threading execution method). As in most PHP applications raw execution speed isn't the limiting factor but system calls and database calls are, your mileage with this patch will vary. Most parts of the old zend_execute.c go into `zend_vm_def.h`. Here you can find opcode handlers and helpers. The typical opcode handler template looks like this: ```c ZEND_VM_HANDLER(, , , ) { } ``` `` is a opcode number (0, 1, ...) `` is an opcode name (ZEN_NOP, ZEND_ADD, :) `` and `` are masks for allowed operand op_types. Specializer will generate code only for defined combination of types. You can use any combination of the following op_types UNUSED, CONST, VAR, TMP and CV also you can use ANY mask to disable specialization according operand's op_type. `` is a handler's code itself. For most handlers it stills the same as in old `zend_execute.c`, but now it uses macros to access opcode operands and some internal executor data. You can see the conformity of new macros to old code in the following list: ```c EXECUTE_DATA execute_data ZEND_VM_DISPATCH_TO_HANDLER() return _helper(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU) ZEND_VM_DISPATCH_TO_HELPER() return (ZEND_OPCODE_HANDLER_ARGS_PASSTHRU) ZEND_VM_DISPATCH_TO_HELPER_EX(,,) return (, ZEND_OPCODE_HANDLER_ARGS_PASSTHRU) ZEND_VM_CONTINUE() return 0 ZEND_VM_NEXT_OPCODE() NEXT_OPCODE() ZEND_VM_SET_OPCODE( SET_OPCODE( ZEND_VM_INC_OPCODE() INC_OPCOD() ZEND_VM_RETURN_FROM_EXECUTE_LOOP() RETURN_FROM_EXECUTE_LOOP() ZEND_VM_C_LABEL(