# Ternary operator performance improvements

- Thread: https://externals.io/message/56188
- Original archive: https://news-web.php.net/php.internals/56188
- Started by koubel@php.net on 2011-11-09T11:51:03+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56188 — koubel@php.net — 2011-11-09T11:51:03+00:00

URL: https://externals.io/message/56188#56188

> 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: ...

Same "copy always" behavior is made with string in ternary operator, and
it can be
also very annoying e.g:

$content =
file_get_contents('http://www.w3.org/TR/2011/WD-html-markup-20110525/spec.html');
$test = $content !== false ? $content : '';

Is this case also solved with your patch or huge strings are still problem
with a ternary "copy always" functionality? Maybe it's more rare case then
array.

P.S: Sorry I don't test it directly with beta2, I have no place to test.

M. Kubelik

---

## #56191 — Ivan Enderlin @ Hoa — 2011-11-09T13:52:11+00:00

URL: https://externals.io/message/56188#56191 · In reply to: #56188

Hi Miroslav & Arnaud,


> 
> > […]
>
> Same "copy always" behavior is made with string in ternary operator, and
> it can be
> also very annoying e.g:
>
> $content =
> file_get_contents('http://www.w3.org/TR/2011/WD-html-markup-20110525/spec.html'); 
>
> $test = $content !== false ? $content : '';
Is it the same with: $content = file_get_contents('…') ?: ''; Maybe we 
could have a heuristic for this shortcut operator no?

Best regards.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at LIFC/DISC (Vesontio) and INRIA (Cassis)
http://lifc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #56194 — Nikita Popov — 2011-11-09T15:24:33+00:00

URL: https://externals.io/message/56188#56194 · In reply to: #56188

Hi Miroslav!

Yes, the ternary patch fixes that problem too :)

Nikita

2011/11/9 Miroslav Kubelík <koubel@php.net>:
> 
> > […]
>
> Same "copy always" behavior is made with string in ternary operator, and
> it can be
> also very annoying e.g:
>
> $content =
> file_get_contents('http://www.w3.org/TR/2011/WD-html-markup-20110525/spec.html');
> $test = $content !== false ? $content : '';
>
> Is this case also solved with your patch or huge strings are still problem
> with a ternary "copy always" functionality? Maybe it's more rare case then
> array.
>
> P.S: Sorry I don't test it directly with beta2, I have no place to test.
>
> M. Kubelik

---

## #56197 — koubel@php.net — 2011-11-09T16:10:22+00:00

URL: https://externals.io/message/56188#56197 · In reply to: #56194

On Wed, 09 Nov 2011 16:24:33 +0100, Nikita Popov  
<nikita.ppv@googlemail.com> wrote:

> Hi Miroslav!
>
> Yes, the ternary patch fixes that problem too :)
>
> Nikita

Nice, thank all participants very much, Arnaud especially, very helpful  
patch.

Maybe string case can be mentioned in the Zend/micro_bench.php as array  
micro benchmars are:

https://github.com/php/php-src/commit/8bdb7a0a5c5466aec576c13f3af9c1a6cacdb35c#diff-1

Miroslav Kubelík
