Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:55805 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64269 invoked from network); 14 Oct 2011 18:09:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Oct 2011 18:09:04 -0000 Authentication-Results: pb1.pair.com smtp.mail=arnaud.lb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=arnaud.lb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: arnaud.lb@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-wy0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:40622] helo=mail-wy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A2/00-63869-EBA789E4 for ; Fri, 14 Oct 2011 14:09:03 -0400 Received: by wyf23 with SMTP id 23so773080wyf.29 for ; Fri, 14 Oct 2011 11:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:user-agent:cc:mime-version:content-type :message-id; bh=rLMIE+sqsEJ/ILgzBztOe5bmUsXEe4N0daqwZHYxQS8=; b=V5zDm2sbFOkz1T5sohTsgzdNCwI1f/jedEJxbVbTF8IMuDAr1dbha24K9b0dPLoUAr Fmz3TVMlfyeN2bCePHO0fX4sE5flXuI3F+r3iFgTIvmyV2JW0xvefdAORcSxQIHUKWJb Yw+S71kECpfjArrVF7vzuFvGTBsSqDyGd0BY8= Received: by 10.216.221.212 with SMTP id r62mr3069242wep.73.1318615739344; Fri, 14 Oct 2011 11:08:59 -0700 (PDT) Received: from noch2.localnet (130-244-168-88.selfip.net. [88.168.244.130]) by mx.google.com with ESMTPS id g20sm15554118wbp.13.2011.10.14.11.08.57 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 14 Oct 2011 11:08:58 -0700 (PDT) To: PHP Internals Date: Fri, 14 Oct 2011 20:08:56 +0200 User-Agent: KMail/1.13.7 (Linux/3.0.0-1-amd64; KDE/4.6.5; x86_64; ; ) Cc: Andi Gutmans , Zeev Suraski , Stas Malyshev , Dmitry Stogov , Nikita Popov MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_4qHmOjKz3Rm+kKN" Message-ID: <201110142008.56344.arnaud.lb@gmail.com> Subject: Ternary operator performance improvements From: arnaud.lb@gmail.com (Arnaud Le Blanc) --Boundary-00=_4qHmOjKz3Rm+kKN Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi, I've already posted this patch and it has since been reviewed and improved. I'm re-posting it for discussion before eventually commiting it. The ternary operator always copies its second or third operand, which is very slow compared to an if/else when the operand is an array for example: $a = range(0,9); // this takes 0.3 seconds here: for ($i = 0; $i < 5000000; ++$i) { if (true) { $b = $a; } else { $b = $a; } } // this takes 3.8 seconds: for ($i = 0; $i < 5000000; ++$i) { $b = true ? $a : $a; } I've tried to reduce the performance hit by avoiding the copy when possible (patch attached). Benchmark: Without patch: (the numbers are the time taken to run the code a certain amount of times) $int = 0; $ary = array(1,2,3,4,5,6,7,8,9); true ? 1 : 0 0.124 true ? 1+0 : 0 0.109 true ? $ary : 0 2.020 ! true ? $int : 0 0.103 true ? ${'ary'} : 0 2.290 ! true ?: 0 0.091 1+0 ?: 0 0.086 $ary ?: 0 2.151 ! ${'var'} ?: 0 2.317 ! With patch: true ? 1 : 0 0.124 true ? 1+0 : 0 0.195 true ? $ary : 0 0.103 true ? $int : 0 0.089 true ? ${'ary'} : 0 0.103 true ?: 0 0.086 1+0 ?: 0 0.159 $cv ?: 0 0.090 ${'var'} ?: 0 0.089 The array copying overhead is eliminated. There is however a slowdown in some of the cases, but overall there is no completely unexpected performance hit as it is the case currently. What do you think ? Is there any objection ? Best regards, --Boundary-00=_4qHmOjKz3Rm+kKN Content-Type: text/x-patch; charset="UTF-8"; name="ternary-improvements.diff.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ternary-improvements.diff.txt" diff --git a/Zend/micro_bench.php b/Zend/micro_bench.php index 87a1b15..7052588 100644 --- a/Zend/micro_bench.php +++ b/Zend/micro_bench.php @@ -202,6 +202,35 @@ function read_str_offset($n) { } } +function issetor($n) { + $val = array(0,1,2,3,4,5,6,7,8,9); + for ($i = 0; $i < $n; ++$i) { + $x = $val ?: null; + } +} + +function issetor2($n) { + $f = false; $j = 0; + for ($i = 0; $i < $n; ++$i) { + $x = $f ?: $j + 1; + } +} + +function ternary($n) { + $val = array(0,1,2,3,4,5,6,7,8,9); + $f = false; + for ($i = 0; $i < $n; ++$i) { + $x = $f ? null : $val; + } +} + +function ternary2($n) { + $f = false; $j = 0; + for ($i = 0; $i < $n; ++$i) { + $x = $f ? $f : $j + 1; + } +} + /*****/ function empty_loop($n) { @@ -318,4 +347,12 @@ read_hash(N); $t = end_test($t, '$x = $hash[\'v\']', $overhead); read_str_offset(N); $t = end_test($t, '$x = $str[0]', $overhead); +issetor(N); +$t = end_test($t, '$x = $a ?: null', $overhead); +issetor2(N); +$t = end_test($t, '$x = $f ?: tmp', $overhead); +ternary(N); +$t = end_test($t, '$x = $f ? $f : $a', $overhead); +ternary2(N); +$t = end_test($t, '$x = $f ? $f : tmp', $overhead); total($t0, "Total"); diff --git a/Zend/tests/ternary.phpt b/Zend/tests/ternary.phpt new file mode 100644 index 0000000..d1a6d45 --- /dev/null +++ b/Zend/tests/ternary.phpt @@ -0,0 +1,429 @@ +--TEST-- +?: and ternary operators +--FILE-- + + int(1) +} +-- 46 -- +array(1) { + [0]=> + int(1) +} +-- 47 -- +array(1) { + [0]=> + int(1) +} +-- 48 -- +array(1) { + [0]=> + int(1) +} +-- 49 -- +string(3) "bar" +-- 50 -- +string(2) "cd" +-- 51 -- +string(8) "val_of_b" +-- 52 -- +string(8) "val_of_b" +-- 53 -- +array(1) { + [0]=> + int(1) +} +-- 54 -- +array(1) { + [0]=> + int(1) +} +-- 55 -- +array(1) { + [0]=> + int(1) +} +-- 56 -- +array(1) { + [0]=> + int(1) +} diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index f8fe4ef..221d4f9 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -1469,7 +1469,8 @@ void zend_do_free(znode *op1 TSRMLS_DC) /* {{{ */ && opline->result.var == op1->u.op.var) { if (opline->opcode == ZEND_FETCH_R || opline->opcode == ZEND_FETCH_DIM_R || - opline->opcode == ZEND_FETCH_OBJ_R) { + opline->opcode == ZEND_FETCH_OBJ_R || + opline->opcode == ZEND_QM_ASSIGN_VAR) { /* It's very rare and useless case. It's better to use additional FREE opcode and simplify the FETCH handlers their selves */ @@ -6308,8 +6309,13 @@ void zend_do_jmp_set(const znode *value, znode *jmp_token, znode *colon_token TS int op_number = get_next_op_number(CG(active_op_array)); zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); - opline->opcode = ZEND_JMP_SET; - opline->result_type = IS_TMP_VAR; + if (value->op_type == IS_VAR || value->op_type == IS_CV) { + opline->opcode = ZEND_JMP_SET_VAR; + opline->result_type = IS_VAR; + } else { + opline->opcode = ZEND_JMP_SET; + opline->result_type = IS_TMP_VAR; + } opline->result.var = get_temporary_variable(CG(active_op_array)); SET_NODE(opline->op1, value); SET_UNUSED(opline->op2); @@ -6326,9 +6332,20 @@ void zend_do_jmp_set_else(znode *result, const znode *false_value, const znode * { zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); - opline->opcode = ZEND_QM_ASSIGN; - opline->extended_value = 0; SET_NODE(opline->result, colon_token); + if (colon_token->op_type == IS_TMP_VAR) { + if (false_value->op_type == IS_VAR || false_value->op_type == IS_CV) { + CG(active_op_array)->opcodes[jmp_token->u.op.opline_num].opcode = ZEND_JMP_SET_VAR; + CG(active_op_array)->opcodes[jmp_token->u.op.opline_num].result_type = IS_VAR; + opline->opcode = ZEND_QM_ASSIGN_VAR; + opline->result_type = IS_VAR; + } else { + opline->opcode = ZEND_QM_ASSIGN; + } + } else { + opline->opcode = ZEND_QM_ASSIGN_VAR; + } + opline->extended_value = 0; SET_NODE(opline->op1, false_value); SET_UNUSED(opline->op2); @@ -6363,8 +6380,13 @@ void zend_do_qm_true(const znode *true_value, znode *qm_token, znode *colon_toke CG(active_op_array)->opcodes[qm_token->u.op.opline_num].op2.opline_num = get_next_op_number(CG(active_op_array))+1; /* jmp over the ZEND_JMP */ - opline->opcode = ZEND_QM_ASSIGN; - opline->result_type = IS_TMP_VAR; + if (true_value->op_type == IS_VAR || true_value->op_type == IS_CV) { + opline->opcode = ZEND_QM_ASSIGN_VAR; + opline->result_type = IS_VAR; + } else { + opline->opcode = ZEND_QM_ASSIGN; + opline->result_type = IS_TMP_VAR; + } opline->result.var = get_temporary_variable(CG(active_op_array)); SET_NODE(opline->op1, true_value); SET_UNUSED(opline->op2); @@ -6383,8 +6405,19 @@ void zend_do_qm_false(znode *result, const znode *false_value, const znode *qm_t { zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); - opline->opcode = ZEND_QM_ASSIGN; SET_NODE(opline->result, qm_token); + if (qm_token->op_type == IS_TMP_VAR) { + if (false_value->op_type == IS_VAR || false_value->op_type == IS_CV) { + CG(active_op_array)->opcodes[colon_token->u.op.opline_num - 1].opcode = ZEND_QM_ASSIGN_VAR; + CG(active_op_array)->opcodes[colon_token->u.op.opline_num - 1].result_type = IS_VAR; + opline->opcode = ZEND_QM_ASSIGN_VAR; + opline->result_type = IS_VAR; + } else { + opline->opcode = ZEND_QM_ASSIGN; + } + } else { + opline->opcode = ZEND_QM_ASSIGN_VAR; + } SET_NODE(opline->op1, false_value); SET_UNUSED(opline->op2); diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 5ae15f4..888058d 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1299,6 +1299,7 @@ void execute_new_code(TSRMLS_D) /* {{{ */ case ZEND_JMPZ_EX: case ZEND_JMPNZ_EX: case ZEND_JMP_SET: + case ZEND_JMP_SET_VAR: opline->op2.jmp_addr = &CG(active_op_array)->opcodes[opline->op2.opline_num]; break; } diff --git a/Zend/zend_opcode.c b/Zend/zend_opcode.c index 9d4f3e7..65b9aa5 100644 --- a/Zend/zend_opcode.c +++ b/Zend/zend_opcode.c @@ -529,6 +529,7 @@ ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC) case ZEND_JMPZ_EX: case ZEND_JMPNZ_EX: case ZEND_JMP_SET: + case ZEND_JMP_SET_VAR: opline->op2.jmp_addr = &op_array->opcodes[opline->op2.opline_num]; break; } diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 4e944a1..238864e 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -4661,8 +4661,45 @@ ZEND_VM_HANDLER(152, ZEND_JMP_SET, CONST|TMP|VAR|CV, ANY) if (i_zend_is_true(value)) { ZVAL_COPY_VALUE(&EX_T(opline->result.var).tmp_var, value); - zendi_zval_copy_ctor(EX_T(opline->result.var).tmp_var); - FREE_OP1(); + if (!IS_OP1_TMP_FREE()) { + zendi_zval_copy_ctor(EX_T(opline->result.var).tmp_var); + } + FREE_OP1_IF_VAR(); +#if DEBUG_ZEND>=2 + printf("Conditional jmp to %d\n", opline->op2.opline_num); +#endif + ZEND_VM_JMP(opline->op2.jmp_addr); + } + + FREE_OP1(); + CHECK_EXCEPTION(); + ZEND_VM_NEXT_OPCODE(); +} + +ZEND_VM_HANDLER(158, ZEND_JMP_SET_VAR, CONST|TMP|VAR|CV, ANY) +{ + USE_OPLINE + zend_free_op free_op1; + zval *value, *ret; + + SAVE_OPLINE(); + value = GET_OP1_ZVAL_PTR(BP_VAR_R); + + if (i_zend_is_true(value)) { + if (OP1_TYPE == IS_VAR || OP1_TYPE == IS_CV) { + Z_ADDREF_P(value); + EX_T(opline->result.var).var.ptr = value; + EX_T(opline->result.var).var.ptr_ptr = NULL; + } else { + ALLOC_ZVAL(ret); + INIT_PZVAL_COPY(ret, value); + EX_T(opline->result.var).var.ptr = ret; + EX_T(opline->result.var).var.ptr_ptr = NULL; + if (!IS_OP1_TMP_FREE()) { + zval_copy_ctor(EX_T(opline->result.var).var.ptr); + } + } + FREE_OP1_IF_VAR(); #if DEBUG_ZEND>=2 printf("Conditional jmp to %d\n", opline->op2.opline_num); #endif @@ -4692,6 +4729,34 @@ ZEND_VM_HANDLER(22, ZEND_QM_ASSIGN, CONST|TMP|VAR|CV, ANY) ZEND_VM_NEXT_OPCODE(); } +ZEND_VM_HANDLER(157, ZEND_QM_ASSIGN_VAR, CONST|TMP|VAR|CV, ANY) +{ + USE_OPLINE + zend_free_op free_op1; + zval *value, *ret; + + SAVE_OPLINE(); + value = GET_OP1_ZVAL_PTR(BP_VAR_R); + + if (OP1_TYPE == IS_VAR || OP1_TYPE == IS_CV) { + Z_ADDREF_P(value); + EX_T(opline->result.var).var.ptr = value; + EX_T(opline->result.var).var.ptr_ptr = NULL; + } else { + ALLOC_ZVAL(ret); + INIT_PZVAL_COPY(ret, value); + EX_T(opline->result.var).var.ptr = ret; + EX_T(opline->result.var).var.ptr_ptr = NULL; + if (!IS_OP1_TMP_FREE()) { + zval_copy_ctor(EX_T(opline->result.var).var.ptr); + } + } + + FREE_OP1_IF_VAR(); + CHECK_EXCEPTION(); + ZEND_VM_NEXT_OPCODE(); +} + ZEND_VM_HANDLER(101, ZEND_EXT_STMT, ANY, ANY) { SAVE_OPLINE(); diff --git a/Zend/zend_vm_opcodes.h b/Zend/zend_vm_opcodes.h index ed80ddc..9b76b0e 100644 --- a/Zend/zend_vm_opcodes.h +++ b/Zend/zend_vm_opcodes.h @@ -157,3 +157,5 @@ #define ZEND_ADD_TRAIT 154 #define ZEND_BIND_TRAITS 155 #define ZEND_SEPARATE 156 +#define ZEND_QM_ASSIGN_VAR 157 +#define ZEND_JMP_SET_VAR 158 --Boundary-00=_4qHmOjKz3Rm+kKN--