Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86836 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 49283 invoked from network); 24 Jun 2015 07:32:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Jun 2015 07:32:30 -0000 Authentication-Results: pb1.pair.com header.from=laruence@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=xinchen.h@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.216.46 as permitted sender) X-PHP-List-Original-Sender: xinchen.h@zend.com X-Host-Fingerprint: 209.85.216.46 mail-vn0-f46.google.com Received: from [209.85.216.46] ([209.85.216.46:43157] helo=mail-vn0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 97/44-11255-B0D5A855 for ; Wed, 24 Jun 2015 03:32:29 -0400 Received: by vnbf62 with SMTP id f62so5078861vnb.10 for ; Wed, 24 Jun 2015 00:32:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=fqsg65Rb3tH1SPvxNDvjhYZx3AMYHxL8QS/Pcq6WKHo=; b=hIbNMlFs2YZp/JiPIElip0Btib4xq9Vivms1y52CC4VSXvOymViEkEmctTqGMK+N2z AnOD28jRbTG/LmY6VccPyiovPf/nrFMsVUc/ZuIDPf8uJeOrpyJg1SmVAVUcr884P+ZP gqetFkB3s6+oPEijWhqInm8q/+uqh+rnnvZrlj7O6vuxj4/7chKWKsiO74kVT2AO/KL9 U7z2W269S3Dyf3/Z8P2ivDG/cjIqr9Ugaa6aliPdcOT1wfLbX/Err9ndEq3FiAX2Cho9 WfI0L6BQ6MhPfxEPl6N5jic+5livqdFSUq4Pps+NNr2mV8dOFi0FqJ4CZhen50YP+JbC ebKg== X-Gm-Message-State: ALoCoQkysg8PipBWn2Yl/ciTSfqU+y7C26SVa9SAFnunMcinqeA+K0yv1IwHHwIIW+MCBaRt2WEDwgr6BpDET6HKJ4DHdFe6flkrqWueixppRJUHY2O86U2csv4A+FsgysiFGByFqK8897IOhunoX1Odk1lTfRmIlwQdP+mzzTwMMTFEHT4UsPE= X-Received: by 10.52.231.97 with SMTP id tf1mr36360295vdc.21.1435131145312; Wed, 24 Jun 2015 00:32:25 -0700 (PDT) Received: from mail-vn0-f45.google.com (mail-vn0-f45.google.com. [209.85.216.45]) by mx.google.com with ESMTPSA id c7sm4757155vdw.16.2015.06.24.00.32.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jun 2015 00:32:24 -0700 (PDT) Received: by vnbg7 with SMTP id g7so5093821vnb.11 for ; Wed, 24 Jun 2015 00:32:23 -0700 (PDT) X-Received: by 10.52.231.97 with SMTP id tf1mr36360231vdc.21.1435131143935; Wed, 24 Jun 2015 00:32:23 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.12.1 with HTTP; Wed, 24 Jun 2015 00:32:04 -0700 (PDT) In-Reply-To: References: Date: Wed, 24 Jun 2015 15:32:04 +0800 Message-ID: To: Yasuo Ohgaki Cc: Juan Basso , PHP Internals , Anthony Ferrara Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Optimizing php_html_entities() From: laruence@php.net (Xinchen Hui) Hey: On Wed, Jun 24, 2015 at 3:13 PM, Yasuo Ohgaki wrote: > Hi all, > > On Wed, Jun 24, 2015 at 12:20 PM, Yasuo Ohgaki wrote: >> >> On Wed, Jun 24, 2015 at 12:05 PM, Juan Basso wrote: >>> >>> Did you test the performance impact on strings? Since you changed how it >>> works the impact can be positive and maybe worth to make the method more >>> broad. >> >> >> It's a bit slower (~3%) for 'abc' as input string. If I remove redundant >> type >> check(convert_to_string), it's better though. ~3% seems too large for 2 >> additional if statements - parameter type check. I'll look into. >> >> [yohgaki@dev github-php-src]$ cat b.php >> > const LOOP=100000000; >> >> $start = microtime(true); >> for ($i = 0; $i < LOOP; $i++) { >> } >> $loop_time = microtime(true) - $start; >> >> >> $start = microtime(true); >> for ($i = 0; $i < LOOP; $i++) { >> htmlspecialchars('abc'); >> } >> echo 'Time: '.(microtime(true) - $start - $loop_time)."\n"; >> >> [yohgaki@dev github-php-src]$ ./php.old b.php >> Time: 4.4925589561462 >> [yohgaki@dev github-php-src]$ ./php.old b.php >> Time: 4.4821939468384 >> [yohgaki@dev github-php-src]$ ./php.new b.php >> Time: 4.5892491340637 >> [yohgaki@dev github-php-src]$ ./php.new b.php >> Time: 4.6097931861877 > > > Followings are better approximation. New code is a bit slower even if it's > in > benchmark error range. ~1% - ~3% slower for strings while it's much faster > (~70% or more. This case is more than 100%) for integers depending on > integer size. Since integer/float is much faster, only a few presence of > int/float > negates negative impact. > > As you can see, loop only benchmark varies a lot. > I also counted total number of CPU instructions by perf. It varies too. > However, new code is a bit slower for sure even if there is only 2 "if" > addition > for strings. I was expecting use of raw zval negates this... > > I would like to apply similar change to most string escape/conversion > functions. Sorry for saying this, but I don't see any reason to do so. the benchmark make nothing to me, since it's actually not a recommend(right) usage at all. as I said, the fastest way to htmlspecicalchars an integer is not calling htmlspecicalchars against number. and I can always get some "benchmark" to show my "optimization" works, like if (EXPECT()); I can always make a "benchmark" which make the condition fails, and get some "improvement".. if you are insist to do so, please write an RFC, I will vote NO for it. sorry & thanks > (Please note that subject is "escape and conversion" function only) > Current patch allows "resource" type, but it should raise type error. > > Comments are appreciated. > Sorry for flood of mails. > > Regards, > > [yohgaki@dev github-php-src]$ ./php.old b.php > Loop time > Loop: 0.08224892616272 > Loop: 0.08589506149292 > Loop: 0.087536096572876 > Loop: 0.082170963287354 > Loop: 0.082328081130981 > Loop: 0.081686019897461 > Loop: 0.087927103042603 > Loop: 0.081233024597168 > Loop: 0.082290887832642 > Loop: 0.081492900848389 > Loop: 0.081452131271362 > Loop: 0.081291913986206 > Loop Avg: 0.08220899105072 > Escaping string abcdefgh > Time: 0.6288548707962 > Time: 0.61239206790924 > Time: 0.6169821023941 > Time: 0.61680710315704 > Time: 0.60974395275116 > Time: 0.61229383945465 > Time: 0.61705410480499 > Time: 0.61985099315643 > Time: 0.61954915523529 > Time: 0.62286603450775 > Time: 0.62001097202301 > Time: 0.6216961145401 > Time Avg: 0.6166380405426 > Escaping integer 12345678 > Time: 0.98103010654449 > Time: 0.97324693202972 > Time: 0.96535289287567 > Time: 0.962033867836 > Time: 0.95926511287689 > Time: 0.9667671918869 > Time: 0.96851003170013 > Time: 0.96103513240814 > Time: 0.96122300624847 > Time: 0.95892608165741 > Time: 0.96553599834442 > Time: 0.96350586414337 > Time Avg: 0.96321551799774 > > [yohgaki@dev github-php-src]$ ./php.new b.php > Loop time > Loop: 0.082480907440186 > Loop: 0.081701993942261 > Loop: 0.081080913543701 > Loop: 0.080965042114258 > Loop: 0.081230163574219 > Loop: 0.08064603805542 > Loop: 0.081595897674561 > Loop: 0.081001043319702 > Loop: 0.080890893936157 > Loop: 0.084527969360352 > Loop: 0.082308053970337 > Loop: 0.081248998641968 > Loop Avg: 0.081266903877258 > Escaping string abcdefgh > Time: 0.62283012866974 > Time: 0.62788126468658 > Time: 0.63206312656403 > Time: 0.62420103549957 > Time: 0.62370917797089 > Time: 0.62745521068573 > Time: 0.626731133461 > Time: 0.68132016658783 > Time: 0.62430212497711 > Time: 0.63127825260162 > Time: 0.63222811222076 > Time: 0.62956402301788 > Time Avg: 0.62700154781342 > Escaping integer 12345678 > Time: 0.41526029109955 > Time: 0.41403410434723 > Time: 0.41262814998627 > Time: 0.41230318546295 > Time: 0.41555306911469 > Time: 0.40698001384735 > Time: 0.41639611721039 > Time: 0.41320106983185 > Time: 0.41369721889496 > Time: 0.42157099246979 > Time: 0.42368505001068 > Time: 0.42184422016144 > Time Avg: 0.4141624212265 > > [yohgaki@dev github-php-src]$ cat b.php > const LOOP=10000000; > const ITER=12; > > echo "Loop time\n"; > $t = []; > for ($n = 0; $n < ITER; $n++) { > $start = microtime(true); > for ($i = 0; $i < LOOP; $i++) { > } > $loop_time = microtime(true) - $start; > echo 'Loop: '.$loop_time."\n"; > $t[] = $loop_time; > } > // Remove max for better approximation > unset($t[array_search(max($t), $t)]); > unset($t[array_search(max($t), $t)]); > $loop_time = array_sum($t)/(ITER-2); > echo "Loop Avg: ${loop_time}\n"; > > $s = 'abcdefgh'; > echo "Escaping string ${s}\n"; > $t = []; > for ($n = 0; $n < ITER; $n++) { > $start = microtime(true); > for ($i = 0; $i < LOOP; $i++) { > htmlspecialchars($s); > } > $time = (microtime(true) - $start - $loop_time); > echo "Time: ${time}\n"; > $t[] = $time; > } > // Remove max for better approximation > unset($t[array_search(max($t), $t)]); > unset($t[array_search(max($t), $t)]); > $time = array_sum($t)/(ITER-2); > echo "Time Avg: ${time}\n"; > > $s = 12345678; > echo "Escaping integer ${s}\n"; > $t = []; > for ($n = 0; $n < ITER; $n++) { > $start = microtime(true); > for ($i = 0; $i < LOOP; $i++) { > htmlspecialchars($s); > } > $time = (microtime(true) - $start - $loop_time); > echo "Time: ${time}\n"; > $t[] = $time; > } > // Remove max for better approximation > unset($t[array_search(max($t), $t)]); > unset($t[array_search(max($t), $t)]); > $time = array_sum($t)/(ITER-2); > echo "Time Avg: ${time}\n"; > > > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net -- Xinchen Hui @Laruence http://www.laruence.com/