Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82904 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68599 invoked from network); 16 Feb 2015 23:48:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Feb 2015 23:48:03 -0000 Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.90 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.90 blu004-omc2s15.hotmail.com Received: from [65.55.111.90] ([65.55.111.90:63102] helo=BLU004-OMC2S15.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D8/35-36518-3B182E45 for ; Mon, 16 Feb 2015 18:48:03 -0500 Received: from BLU436-SMTP204 ([65.55.111.72]) by BLU004-OMC2S15.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Mon, 16 Feb 2015 15:48:01 -0800 X-TMN: [h7UlTj8p/+JWgphixJa9PjRay1uei+4d] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_E3FD31BD-EE9E-491C-9E8D-B6C9E5B7C05D" MIME-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: <54E27D8D.5020901@lerdorf.com> Date: Tue, 17 Feb 2015 00:47:54 +0100 CC: PHP Internals References: <54E27D8D.5020901@lerdorf.com> To: Rasmus Lerdorf X-Mailer: Apple Mail (2.1990.1) X-OriginalArrivalTime: 16 Feb 2015 23:47:58.0532 (UTC) FILETIME=[FE1C7840:01D04A42] Subject: Re: [PHP-DEV] Switch jumptable optimization From: bobwei9@hotmail.com (Bob Weinand) --Apple-Mail=_E3FD31BD-EE9E-491C-9E8D-B6C9E5B7C05D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="windows-1252" > Am 17.02.2015 um 00:30 schrieb Rasmus Lerdorf : >=20 > On 02/16/2015 03:04 PM, Bob Weinand wrote: >> I'd like to show you my recent work on a jumptable optimization for = switches. >>=20 >> https://github.com/php/php-src/pull/1048 = >>=20 >> It is a fully transparent optimization of switches, by putting a new = ZEND_SWITCH opcode at the top of the switch in case we can build a = jumptable, which is the case if the cases are only scalars (no doubles) = or evaluate to them at compile-time. >>=20 >> Switches tend to be big sometimes with a lot of static (literals or = constants usually) cases, which was a comparison for each case, and now = just a simple hashtable lookup is needed, which greatly improves = performance. In case where the result of the switch can be determined at = compile-time, it even replaces the comparisons with a simple ZEND_JMP = opcode. >>=20 >> In synthetic benchmarks the results are mind blowing, but in = practice, benefits mostly are stateful userland parsers (often called = with sometimes big switches), where it makes it a few percent faster. = (For more concrete numbers see = https://github.com/php/php-src/pull/1048#issuecomment-73032647) >>=20 >> As the optimization is only done if there are 3 or more cases, the = lookup is always faster than the single comparisons. So there's no = negative performance impact. >>=20 >> It already works, except with opcache where the CFG optimization pass = still lacks support. Dmitry or Laruence volunteered to add support for = opcache. >>=20 >> I'd like to know if there are any technical objections to the PR. If = not, they will add opcache support and merge it in. >=20 > It looks ok to me if Xinchen or Dmitry gets the opcache support = working > for it and the feature is stabilized in time. I gave it a try and it = is > segfaulting for me (no opcache): >=20 > =3D=3D5612=3D=3D Command: sapi/cli/php -n jump.php > =3D=3D5612=3D=3D > =3D=3D5612=3D=3D Invalid read of size 4 > =3D=3D5612=3D=3D at 0xA64637: _zend_is_inconsistent = (zend_hash.c:44) > =3D=3D5612=3D=3D by 0xA65749: _zend_hash_add_or_update_i = (zend_hash.c:427) > =3D=3D5612=3D=3D by 0xA65AF9: _zend_hash_add (zend_hash.c:489) > =3D=3D5612=3D=3D by 0xA37E28: zend_hash_add_ptr (zend_hash.h:458) > =3D=3D5612=3D=3D by 0xA39B0A: zend_hash_add_constant = (zend_constants.c:486) > =3D=3D5612=3D=3D by 0xA39D01: zend_register_constant = (zend_constants.c:523) > =3D=3D5612=3D=3D by 0xA389E9: zend_register_long_constant = (zend_constants.c:194) > =3D=3D5612=3D=3D by 0xA3855C: zend_register_standard_constants > (zend_constants.c:116) > =3D=3D5612=3D=3D by 0xA52D6E: zend_startup (zend.c:671) > =3D=3D5612=3D=3D by 0x9C7FAA: php_module_startup (main.c:2099) > =3D=3D5612=3D=3D by 0xAFA460: php_cli_startup (php_cli.c:421) > =3D=3D5612=3D=3D by 0xAFC4D6: main (php_cli.c:1335) > =3D=3D5612=3D=3D Address 0x8 is not stack'd, malloc'd or (recently) = free'd >=20 > And jump.php is: >=20 > $a =3D 1; > switch($a) { > case 0: echo 0; break; > case 1: echo 1; break; > case 2: echo 1; break; > case 3: echo 1; break; > case 4: echo 1; break; > case 5: echo 1; break; > case 6: echo 1; break; > case 7: echo 1; break; > case 8: echo 1; break; > case 9: echo 1; break; > } >=20 > -Rasmus Hey, this doesn't really look related to my patch. Did you do something wrong = when cloning my branch? Or forget a make clean or similar? Locally it works for me and echoes 1 as expected. Bob= --Apple-Mail=_E3FD31BD-EE9E-491C-9E8D-B6C9E5B7C05D--