php-src/Zend
Tim Düsterhus 2c5ed50d5c
zend_compile: Add support for %d to sprintf() optimization (#14561)
* zend_compile: Rename `string_placeholder_count` to `placeholder_count` in `zend_compile_func_sprintf()`

This is intended to make the diff of a follow-up commit smaller.

* zend_compile: Add support for `%d` to `sprintf()` optimization

This extends the existing `sprintf()` optimization by support for the `%d`
placeholder, which effectively equivalent to an `(int)` cast followed by a
`(string)` cast.

For a synthetic test using:

    <?php

    $a = 'foo';
    $b = 42;

    for ($i = 0; $i < 100_000_000; $i++) {
        sprintf("%s-%d", $a, $b);
    }

This optimization yields a 1.3× performance improvement:

    $ hyperfine 'sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php' \
          '/tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php'
    Benchmark 1: sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      3.296 s ±  0.094 s    [User: 3.287 s, System: 0.005 s]
      Range (min … max):    3.213 s …  3.527 s    10 runs

    Benchmark 2: /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      4.300 s ±  0.025 s    [User: 4.290 s, System: 0.007 s]
      Range (min … max):    4.266 s …  4.334 s    10 runs

    Summary
      sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php ran
        1.30 ± 0.04 times faster than /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php

* Fix sprintf_rope_optimization_003.phpt test expecation for 32-bit integers

* zend_compile: Indent switch-case labels in zend_compile_func_sprintf()

* Add GMP test to sprintf() rope optimization

* Add `%s` test case to sprintf() GMP test
2024-06-17 17:07:50 +02:00
..
asm rudimentary midipix port (#13896) 2024-04-18 08:19:44 +02:00
Optimizer ext/pgsql: add pg_jit server info. 2024-06-14 16:27:00 +01:00
tests Show enum cases in errors 2024-06-10 22:58:25 +02:00
bench.php
LICENSE
Makefile.frag Sync YFLAGS in scripts/dev/genfiles with PHP_PROG_BISON macro defaults (#12709) 2023-11-22 22:54:09 +01:00
micro_bench.php
README.md
zend_alloc_sizes.h
zend_alloc.c Detect heap freelist corruption (#14054) 2024-06-12 17:28:52 +02:00
zend_alloc.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_API.c Sync #if/ifdef/defined (#14508) 2024-06-09 14:23:41 +02:00
zend_API.h Zend: Add object_init_with_constructor() API (#14440) 2024-06-06 21:21:16 +01:00
zend_arena.h Use zend_error_noreturn for E_ERROR consistently 2023-09-14 11:44:55 +02:00
zend_ast.c Mark multple functions as static (#13864) 2024-05-22 13:11:46 +02:00
zend_ast.h ZEND_ELEMENT_COUNT usage reduction. (#13324) 2024-02-04 19:09:15 +00:00
zend_atomic.c Add zend_atomic_int (#14242) 2024-05-30 12:53:44 +02:00
zend_atomic.h Add zend_atomic_int (#14242) 2024-05-30 12:53:44 +02:00
zend_attributes_arginfo.h Do not generate frameless info items when func info generation is disabled 2024-02-18 11:39:00 +01:00
zend_attributes.c Leverage object_init_with_constructor() in zend_get_attribute_object() (#14532) 2024-06-10 15:12:22 +02:00
zend_attributes.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_attributes.stub.php Add support for typed class constants in stubs 2023-07-01 11:50:04 +02:00
zend_bitset.h adding const attribute to trailing/leading zeros helpers. (#13861) 2024-04-02 12:10:18 +01:00
zend_build.h
zend_builtin_functions_arginfo.h Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_builtin_functions.c Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_builtin_functions.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_builtin_functions.stub.php Implement stackless internal function calls 2024-02-06 17:42:28 +01:00
zend_call_stack.c refactor: zend_call_stack_get_linux_pthread 2024-06-10 10:40:40 -06:00
zend_call_stack.h Specify what is expected in zend_call_stack.base 2024-02-20 15:34:44 +01:00
zend_closures_arginfo.h Do not generate frameless info items when func info generation is disabled 2024-02-18 11:39:00 +01:00
zend_closures.c Include the source location in Closure names (#13550) 2024-04-12 18:21:13 +02:00
zend_closures.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_closures.stub.php
zend_compile.c zend_compile: Add support for %d to sprintf() optimization (#14561) 2024-06-17 17:07:50 +02:00
zend_compile.h Omit FETCH_THIS in closures 2024-05-16 12:28:11 +02:00
zend_config.w32.h Remove outdated Zend/zend_istdiostream.h file (#13765) 2024-03-21 10:37:14 +01:00
zend_constants_arginfo.h Do not generate frameless info items when func info generation is disabled 2024-02-18 11:39:00 +01:00
zend_constants.c Implement reflection constant 2024-04-17 22:53:09 +02:00
zend_constants.h Implement reflection constant 2024-04-17 22:53:09 +02:00
zend_constants.stub.php Improve detection of predefined constants 2023-09-07 10:00:55 +02:00
zend_cpuinfo.c
zend_cpuinfo.h Cacheline demote to improve performance (#11101) 2023-05-15 10:28:43 +03:00
zend_default_classes.c
zend_dtrace.c
zend_dtrace.d
zend_dtrace.h
zend_enum_arginfo.h Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_enum.c Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_enum.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_enum.stub.php
zend_errors.h
zend_exceptions_arginfo.h Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_exceptions.c Show enum cases in errors 2024-06-10 22:58:25 +02:00
zend_exceptions.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_exceptions.stub.php Add request_parse_body() function 2024-02-08 12:08:07 +01:00
zend_execute_API.c Sync #if/ifdef/defined (#14512) 2024-06-10 08:56:10 +02:00
zend_execute.c Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_execute.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_extensions.c Separate internal and user function extension handles 2024-05-21 11:56:38 +02:00
zend_extensions.h Separate internal and user function extension handles 2024-05-21 11:56:38 +02:00
zend_fibers_arginfo.h Do not generate frameless info items when func info generation is disabled 2024-02-18 11:39:00 +01:00
zend_fibers.c Merge branch 'PHP-8.3' 2024-04-08 21:04:41 +02:00
zend_fibers.h
zend_fibers.stub.php
zend_float.c
zend_float.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_frameless_function.c Implement stackless internal function calls 2024-02-06 17:42:28 +01:00
zend_frameless_function.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_gc.c Merge branch 'PHP-8.2' into PHP-8.3 2024-03-25 16:22:54 +01:00
zend_gc.h Remove WeakMap entries whose key is only reachable through the entry value (#10932) 2023-07-16 13:39:08 +02:00
zend_gdb.c Zend: change uses of sprintf into snprintf 2024-06-14 08:12:03 -07:00
zend_gdb.h
zend_generators_arginfo.h Make ReflectionGenerator::getFunction() legal after generator termination (#14167) 2024-05-21 08:54:51 +02:00
zend_generators.c Merge branch 'PHP-8.3' 2024-06-01 02:42:49 +02:00
zend_generators.h Make ReflectionGenerator::getFunction() legal after generator termination (#14167) 2024-05-21 08:54:51 +02:00
zend_generators.stub.php Make ReflectionGenerator::getFunction() legal after generator termination (#14167) 2024-05-21 08:54:51 +02:00
zend_globals_macros.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_globals.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_hash.c Fix iterator position resetting 2024-01-21 23:24:00 +01:00
zend_hash.h Replace ZEND_ASSUME() by ZEND_ASSERT() in zend_hash_*_ptr setters (#14466) 2024-06-05 11:08:11 +02:00
zend_highlight.c Align highlight_string|file with HTML standard and modern browsers 2023-08-12 15:08:28 +01:00
zend_highlight.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_hrtime.c Use ZEND_API in zend_hrtime (#13288) 2024-03-17 03:09:08 +01:00
zend_hrtime.h Use ZEND_API in zend_hrtime (#13288) 2024-03-17 03:09:08 +01:00
zend_inheritance.c Merge branch 'PHP-8.3' 2024-06-05 23:59:49 +03:00
zend_inheritance.h RFC: Add #[Override] attribute (#9836) 2023-06-29 20:23:53 +02:00
zend_ini_parser.y Mark multple functions as static (#13864) 2024-05-22 13:11:46 +02:00
zend_ini_scanner.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_ini_scanner.l small fixes 2023-07-18 08:31:02 +01:00
zend_ini.c ext/standard: Minor improvements to phpinfo() output (#13371) 2024-02-11 11:05:07 +01:00
zend_ini.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_interfaces_arginfo.h Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_interfaces.c general signatures discrepencies fixes (#13122) 2024-01-10 22:19:23 +00:00
zend_interfaces.h
zend_interfaces.stub.php
zend_iterators.c
zend_iterators.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_language_parser.y [RFC] Implement dereferencable for new exprs with constructor args 2024-05-28 00:23:12 +02:00
zend_language_scanner.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_language_scanner.l Omit FETCH_THIS in closures 2024-05-16 12:28:11 +02:00
zend_list.c Merge branch 'PHP-8.1' into PHP-8.2 2023-10-29 21:47:23 +01:00
zend_list.h
zend_llist.c
zend_llist.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_long.h
zend_map_ptr.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_max_execution_timer.c Merge branch 'PHP-8.3' 2024-04-16 14:20:23 +02:00
zend_max_execution_timer.h
zend_mmap.h
zend_modules.h Merge branch 'PHP-8.3' 2024-02-20 21:26:39 +01:00
zend_multibyte.c general signatures discrepencies fixes (#13122) 2024-01-10 22:19:23 +00:00
zend_multibyte.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_multiply.h
zend_object_handlers.c Merge branch 'PHP-8.3' 2024-06-06 15:51:47 +01:00
zend_object_handlers.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_objects_API.c
zend_objects_API.h Zend: Remove dependency on zend.h for certain headers (#12166) 2023-09-11 12:27:21 +01:00
zend_objects.c Merge branch 'PHP-8.2' into PHP-8.3 2024-03-08 18:27:10 +01:00
zend_objects.h
zend_observer.c Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_observer.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_opcode.c Implement "support doc comments for internal classes and functions" (#13266) 2024-02-25 08:41:31 +01:00
zend_operators.c Sync #if/ifdef/defined (#14520) 2024-06-11 22:47:05 +02:00
zend_operators.h refactor: change zend_is_true to return bool (#14301) 2024-05-24 15:16:36 -06:00
zend_portability.h Fix warnings function declaration isn't a prototype (#14577) 2024-06-15 21:27:56 +02:00
zend_ptr_stack.c
zend_ptr_stack.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_range_check.h
zend_signal.c Mark multple functions as static (#13864) 2024-05-22 13:11:46 +02:00
zend_signal.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_smart_str_public.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_smart_str.c Show enum cases in errors 2024-06-10 22:58:25 +02:00
zend_smart_str.h Show enum cases in errors 2024-06-10 22:58:25 +02:00
zend_smart_string_public.h
zend_smart_string.h
zend_sort.c
zend_sort.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_stack.c
zend_stack.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_stream.c
zend_stream.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_string.c Merge branch 'PHP-8.3' 2024-01-09 20:06:15 +03:00
zend_string.h Merge branch 'PHP-8.3' 2024-05-06 08:49:52 +03:00
zend_strtod_int.h Remove zend_strtod mutex (#13974) 2024-04-23 11:52:38 +02:00
zend_strtod.c Remove zend_strtod mutex (#13974) 2024-04-23 11:52:38 +02:00
zend_strtod.h Remove zend_strtod mutex (#13974) 2024-04-23 11:52:38 +02:00
zend_system_id.c Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_system_id.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_type_info.h Backport fix for HASH/PACKED array inference through MAY_BE_ARRAY_EMPTY flag (#12591) 2023-11-03 10:54:57 +03:00
zend_types.h ZEND_ELEMENT_COUNT usage reduction. (#13324) 2024-02-04 19:09:15 +00:00
zend_variables.c
zend_variables.h Zend: Remove dependency on zend.h for certain headers (#12166) 2023-09-11 12:27:21 +01:00
zend_verify_type_inference.h [skip ci] Use _exit for type inference verification 2024-01-23 18:15:30 +01:00
zend_virtual_cwd.c Sync #if/ifdef/defined (#14520) 2024-06-11 22:47:05 +02:00
zend_virtual_cwd.h Sync #if/ifdef/defined (#14520) 2024-06-11 22:47:05 +02:00
zend_vm_def.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_vm_execute.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_vm_execute.skl Fixed GH-12596: Segmentation fault on AArch64 release build with opcache.jit=1112 (#12813) 2023-12-05 12:04:00 +03:00
zend_vm_gen.php Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_vm_handlers.h Optimize observers (#13649) 2024-06-15 14:42:27 +02:00
zend_vm_opcodes.c Implement stackless internal function calls 2024-02-06 17:42:28 +01:00
zend_vm_opcodes.h Implement stackless internal function calls 2024-02-06 17:42:28 +01:00
zend_vm_trace_handlers.h
zend_vm_trace_lines.h
zend_vm_trace_map.h
zend_vm.h Fix header errors when parsed standalone (#14272) 2024-05-20 22:30:38 +02:00
zend_weakrefs_arginfo.h Do not generate frameless info items when func info generation is disabled 2024-02-18 11:39:00 +01:00
zend_weakrefs.c refactor: change zend_is_true to return bool (#14301) 2024-05-24 15:16:36 -06:00
zend_weakrefs.h Remove WeakMap entries whose key is only reachable through the entry value (#10932) 2023-07-16 13:39:08 +02:00
zend_weakrefs.stub.php
zend.c Detect heap freelist corruption (#14054) 2024-06-12 17:28:52 +02:00
zend.h Add zend_random_bytes(), zend_random_bytes_insecure() functions (#14054) 2024-06-12 17:27:01 +02:00
Zend.m4 Wrap Zend stack limit check in AC_CACHE_CHECK (#14437) 2024-06-02 20:13:00 +02:00

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:

sapi/cli/php -r 'leak();'

Zend MM disabled:

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:

ZEND_VM_HANDLER(<OPCODE-NUMBER>, <OPCODE>, <OP1_TYPES>, <OP2_TYPES>)
{
    <HANDLER'S CODE>
}

<OPCODE-NUMBER> is a opcode number (0, 1, ...) <OPCODE> is an opcode name (ZEN_NOP, ZEND_ADD, :) <OP1_TYPES> and <OP2_TYPES> 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. <HANDLER'S CODE> 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:

EXECUTE_DATA
    execute_data
ZEND_VM_DISPATCH_TO_HANDLER(<OP>)
    return <OP>_helper(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
ZEND_VM_DISPATCH_TO_HELPER(<NAME>)
    return <NAME>(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
ZEND_VM_DISPATCH_TO_HELPER_EX(<NAME>,<PARAM>,<VAL>)
    return <NAME>(<VAL>, ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
ZEND_VM_CONTINUE()
    return 0
ZEND_VM_NEXT_OPCODE()
    NEXT_OPCODE()
ZEND_VM_SET_OPCODE(<TARGET>
    SET_OPCODE(<TARGET>
ZEND_VM_INC_OPCODE()
    INC_OPCOD()
ZEND_VM_RETURN_FROM_EXECUTE_LOOP()
    RETURN_FROM_EXECUTE_LOOP()
ZEND_VM_C_LABEL(<LABEL>):
    <LABEL>:
ZEND_VM_C_GOTO(<LABEL>)
    goto <LABEL>
OP<X>_TYPE
    opline->op<X>.op_type
GET_OP<X>_ZVAL_PTR(<TYPE>)
    get_zval_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
GET_OP<X>_ZVAL_PTR_PTR(<TYPE>)
    get_zval_ptr_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
GET_OP<X>_OBJ_ZVAL_PTR(<TYPE>)
    get_obj_zval_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
GET_OP<X>_OBJ_ZVAL_PTR_PTR(<TYPE>)
    get_obj_zval_ptr_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
IS_OP<X>_TMP_FREE()
    IS_TMP_FREE(free_op<X>)
FREE_OP<X>()
    FREE_OP(free_op<X>)
FREE_OP<X>_IF_VAR()
    FREE_VAR(free_op<X>)
FREE_OP<X>_VAR_PTR()
    FREE_VAR_PTR(free_op<X>)

Executor's helpers can be defined without parameters or with one parameter. This is done with the following constructs:

ZEND_VM_HELPER(<HELPER-NAME>, <OP1_TYPES>, <OP2_TYPES>)
{
    <HELPER'S CODE>
}

ZEND_VM_HELPER_EX(<HELPER-NAME>, <OP1_TYPES>, <OP2_TYPES>, <PARAM_SPEC>)
{
    <HELPER'S CODE>
}

The executors code is generated by the PHP script zend_vm_gen.php. It uses zend_vm_def.h and zend_vm_execute.skl as input and produces zend_vm_opcodes.h and zend_vm_execute.h. The first file is a list of opcode definitions. It is included from zend_compile.h. The second one is an executor code itself. It is included from zend_execute.c.

zend_vm_gen.php can produce different kind of executors. You can select a different opcode threading model using --with-vm-kind=CALL|SWITCH|GOTO|HYBRID. You can disable opcode specialization using --without-specializer. At last you can debug the executor using the original zend_vm_def.h or the generated zend_vm_execute.h file. Debugging with the original file requires the --with-lines option. By default, Zend Engine uses the following command to generate the executor:

# Default VM kind is HYBRID
php zend_vm_gen.php --with-vm-kind=HYBRID