Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82902 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64884 invoked from network); 16 Feb 2015 23:30:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Feb 2015 23:30:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain lerdorf.com designates 209.85.192.182 as permitted sender) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.192.182 mail-pd0-f182.google.com Received: from [209.85.192.182] ([209.85.192.182:37739] helo=mail-pd0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/84-36518-29D72E45 for ; Mon, 16 Feb 2015 18:30:27 -0500 Received: by pdbfl12 with SMTP id fl12so39456341pdb.4 for ; Mon, 16 Feb 2015 15:30:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type; bh=gq986qQG25V5NJtfu3yun4KcI0m2KQn08W/Hk8IOkh8=; b=ABxBoHdWUWYZpvygDzIWKaf2DxdzecIBXK3UUjea0tPks0s1Cp6PsmpHO2eN8v033Z gT0a/rPwps7ggtOhtKJutZlrduT7NZ0lI+0GMAPonuSKM5evwJ9DvV9fD5BFtT238WoY Y1zUFxcQnif5kzKPQzEhhLCkEuQAjUTwXYqJfUNSicu+RFINj2J1Zg77/VDXlA1lGqNM MNVuF6FQu/MtHqK80IFqHQAX4lw4UG9oYapeb+45q1upi3D+/o5aLEMhSf5qL71/yKVf UTd4MoQ49gd035zKF7W1W7uKuG+r4jOkqMzWnHJr3iVSdYrB9Q0o0SpzlEaYO6+GWBdT O/mw== X-Gm-Message-State: ALoCoQkprGA7az4U2lSpg8ZpLoguZrSbv5PVCmyjDs4S2KFV6dXziD332dQU+fAXfsUcUxsgRRPg X-Received: by 10.68.57.228 with SMTP id l4mr12837869pbq.43.1424129423942; Mon, 16 Feb 2015 15:30:23 -0800 (PST) Received: from [192.168.200.14] (c-50-131-44-225.hsd1.ca.comcast.net. [50.131.44.225]) by mx.google.com with ESMTPSA id ek12sm8651188pdb.92.2015.02.16.15.30.22 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Feb 2015 15:30:22 -0800 (PST) Message-ID: <54E27D8D.5020901@lerdorf.com> Date: Mon, 16 Feb 2015 15:30:21 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: Bob Weinand , PHP Internals References: In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="qXPXkPJ051Qra3tF11pVmCbiQbKboPukm" Subject: Re: [PHP-DEV] Switch jumptable optimization From: rasmus@lerdorf.com (Rasmus Lerdorf) --qXPXkPJ051Qra3tF11pVmCbiQbKboPukm Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On 02/16/2015 03:04 PM, Bob Weinand wrote: > I'd like to show you my recent work on a jumptable optimization for swi= tches. >=20 > https://github.com/php/php-src/pull/1048 >=20 > It is a fully transparent optimization of switches, by putting a new ZE= ND_SWITCH opcode at the top of the switch in case we can build a jumptabl= e, which is the case if the cases are only scalars (no doubles) or evalua= te to them at compile-time. >=20 > Switches tend to be big sometimes with a lot of static (literals or con= stants 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 sometime= s big switches), where it makes it a few percent faster. (For more concre= te numbers see https://github.com/php/php-src/pull/1048#issuecomment-7303= 2647) >=20 > As the optimization is only done if there are 3 or more cases, the look= up is always faster than the single comparisons. So there's no negative p= erformance impact. >=20 > It already works, except with opcache where the CFG optimization pass s= till lacks support. Dmitry or Laruence volunteered to add support for opc= ache. >=20 > I'd like to know if there are any technical objections to the PR. If no= t, they will add opcache support and merge it in. 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): =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_consta= nts.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 And jump.php is: