Skip to content

Commit 7b880e3

Browse files
committed
Only use array templates with opcache
With opache, arrays are marked as immutable. Without opcache, nested arrays need refcounting, and thus require a loop over its elements.
1 parent e07fdd4 commit 7b880e3

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9792,7 +9792,7 @@ static bool zend_try_ct_eval_array(zval *result, zend_ast *ast, bool allow_templ
97929792
zend_ast *last_elem_ast = NULL;
97939793
uint32_t i;
97949794
bool is_constant = 1;
9795-
bool use_template = true;
9795+
bool use_template = CG(compiler_options) & ZEND_COMPILE_WITH_ARRAY_TEMPLATES;
97969796

97979797
if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
97989798
zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");

Zend/zend_compile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,11 @@ END_EXTERN_C()
12851285
/* ignore observer notifications, e.g. to manually notify afterwards in a post-processing step after compilation */
12861286
#define ZEND_COMPILE_IGNORE_OBSERVER (1<<18)
12871287

1288+
/* Allow compiling with ZEND_ARRAY_DUP and ZEND_ARRAY_SET_PLACEHOLDER opcodes.
1289+
* These are generally faster with opcache, because the array constant is
1290+
* immutable, which allows for fast cloning through memcpy. */
1291+
#define ZEND_COMPILE_WITH_ARRAY_TEMPLATES (1<<19)
1292+
12881293
/* The default value for CG(compiler_options) */
12891294
#define ZEND_COMPILE_DEFAULT ZEND_COMPILE_HANDLE_OP_ARRAY
12901295

ext/opcache/ZendAccelerator.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,7 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl
18151815
CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION;
18161816
CG(compiler_options) |= ZEND_COMPILE_IGNORE_OTHER_FILES;
18171817
CG(compiler_options) |= ZEND_COMPILE_IGNORE_OBSERVER;
1818+
CG(compiler_options) |= ZEND_COMPILE_WITH_ARRAY_TEMPLATES;
18181819
#ifdef ZEND_WIN32
18191820
/* On Windows, don't compile with internal classes. Shm may be attached from different
18201821
* processes with internal classes living in different addresses. */
@@ -4429,6 +4430,7 @@ static zend_result accel_preload(const char *config, bool in_child)
44294430
CG(compiler_options) |= ZEND_COMPILE_DELAYED_BINDING;
44304431
CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION;
44314432
CG(compiler_options) |= ZEND_COMPILE_IGNORE_OTHER_FILES;
4433+
CG(compiler_options) |= ZEND_COMPILE_WITH_ARRAY_TEMPLATES;
44324434
CG(skip_shebang) = true;
44334435

44344436
zend_try {

0 commit comments

Comments
 (0)