# Small question about performance

- Thread: https://externals.io/message/58957
- Original archive: https://news-web.php.net/php.internals/58957
- Started by Klaus Silveira on 2012-03-15T14:21:19+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #58957 — Klaus Silveira — 2012-03-15T14:21:19+00:00

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

Hello internals,

I've been involved in a discussion at the PHP Standards Group and we
recently had the following statement:

*Say you had a loop, and inside that loop you wanted to modify a param
> **update the key:**
> **foreach($a as $key => $val) {
> **   $a[$key] = someLong(functionCalls(hereThat($spanOver85Chars)));
> **}**
> **If this exceeded the line width, you would have to split things like
> **this over a few lines, storing the val temporarily in a zval's until
> **you reached your end computation. Therefore allocating more memory
> **iteratively. *


I'm curious about this. Can anyone confirm it or a benchmark should be
made?

---

## #58958 — Richard Lynch — 2012-03-15T14:42:29+00:00

URL: https://externals.io/message/58957#58958 · In reply to: #58957

> Hello internals,
>
> I've been involved in a discussion at the PHP Standards Group and we
> recently had the following statement:
>
> *Say you had a loop, and inside that loop you wanted to modify a param
> > […]
>
>
> I'm curious about this. Can anyone confirm it or a benchmark should be
> made?

I don't see why your standard can't allow for:
      $a[$key] =
        someLong(
          functionCalls(
            hereThat(
              $spanOver85Chars
        ) ) );

Some folks insist that the closing parens be on separate lines. 
They're isomorphic to me, so I just put them on one line.

So long as the count matches and they "line up" in reverse order,
indented properly, it's all good.

But then, I cut my teeth on Lisp, where a zillion parens to close off
almost anything is the norm. :-)

PS
If you want a benchmark for how much 3 or 4 temporary zvals takes,
knock yourself out.

Also consider trying this in a second benchmark:

//don't create / destroy zvals in the loop, hopefully
$someLong = '';
$functionCalls = '';
$hereThat = '';
foreach ($a as $key => $value){
  ...
}

PPS
I'm assuming $a is not within the Coding Standard :-)


-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE

---

## #58959 — Nikita Popov — 2012-03-15T14:57:41+00:00

URL: https://externals.io/message/58957#58959 · In reply to: #58957

> Hello internals,
>
> I've been involved in a discussion at the PHP Standards Group and we
> recently had the following statement:
>
> *Say you had a loop, and inside that loop you wanted to modify a param
> > […]
If I am understanding the text correctly it is saying that
    $f1 = f1();
    $f2 = f2($f1);
    $f3 = f3($f2);
is using more memory than
    $f3 = f3(f2(f1()));

For me this doesn't make any sense. In the latter case PHP will also
create temporary variables to store the return values. There should be
no difference in memory consumption.

Nikita

---

## #58962 — Patrick ALLAERT — 2012-03-15T16:22:28+00:00

URL: https://externals.io/message/58957#58962 · In reply to: #58959

2012/3/15 Nikita Popov <nikita.ppv@googlemail.com>:
> If I am understanding the text correctly it is saying that
>    $f1 = f1();
>    $f2 = f2($f1);
>    $f3 = f3($f2);
> is using more memory than
>    $f3 = f3(f2(f1()));
>
> For me this doesn't make any sense. In the latter case PHP will also
> create temporary variables to store the return values. There should be
> no difference in memory consumption.

It does make sense to me.

In the first case, when calling f3(), $f1 is still referenced.
In the second case, when calling f3(), the result of f2() is
referenced, but there is no more active reference to the result of
f1().

Regarding the original problem:
foreach($a as $key => $val) {
    $a[$key] = someLong(functionCalls(hereThat($spanOver85Chars)));
}

Sounds easier to split over lines without temporary zvals:
foreach($a as $key => $val) {
    $a[$key] = someLong(
        functionCalls(
            hereThat(
                $spanOver85Chars
            )
        )
    );
}

---

## #58963 — Michael Stowe — 2012-03-15T16:33:34+00:00

URL: https://externals.io/message/58957#58963 · In reply to: #58962

Just to elaborate on what Patrick said, in the first case the variables are
temporary, where in the second they persist even after you finish your
loop.  So even after the foreach is finished, the $f1, $f2, and $f3
variables are still storing data- even though it is no longer needed.

In order to free up the memory allocated to these variables you'd have to
unset them after they're used in the loop, or after the loop has finished.

- Mike




On Thu, Mar 15, 2012 at 11:22 AM, Patrick ALLAERT <patrickallaert@php.net>wrote:

> 2012/3/15 Nikita Popov <nikita.ppv@googlemail.com>:
> > […]
>
> It does make sense to me.
>
> In the first case, when calling f3(), $f1 is still referenced.
> In the second case, when calling f3(), the result of f2() is
> referenced, but there is no more active reference to the result of
> f1().
>
> Regarding the original problem:
> foreach($a as $key => $val) {
>    $a[$key] = someLong(functionCalls(hereThat($spanOver85Chars)));
> }
>
> Sounds easier to split over lines without temporary zvals:
> foreach($a as $key => $val) {
>     $a[$key] = someLong(
>        functionCalls(
>            hereThat(
>                $spanOver85Chars
>            )
>        )
>    );
> }
>
> --
> 
> 
>
>


-- 
-----------------------

"My command is this: Love each other as I
have loved you."                         John 15:12

---

## #58964 — Stas Malyshev — 2012-03-15T16:49:45+00:00

URL: https://externals.io/message/58957#58964 · In reply to: #58959

Hi!

> If I am understanding the text correctly it is saying that
>      $f1 = f1();
>      $f2 = f2($f1);
>      $f3 = f3($f2);
> is using more memory than
>      $f3 = f3(f2(f1()));

Short answer: it doesn't matter, use either as you wish.

Long answer: Technically, the former also uses hash buckets to bind 
values to the symbol table, so it can use more memory (and if it so 
happens that using these variables makes symbol table go over hashtable 
threshold, hashtable internal storage size will be increased), so it 
will consume marginally more memory. But the difference is completely 
negligible.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #58965 — Nikita Popov — 2012-03-15T16:54:14+00:00

URL: https://externals.io/message/58957#58965 · In reply to: #58962

> 2012/3/15 Nikita Popov <nikita.ppv@googlemail.com>:
> > […]
>
> It does make sense to me.
>
> In the first case, when calling f3(), $f1 is still referenced.
> In the second case, when calling f3(), the result of f2() is
> referenced, but there is no more active reference to the result of
> f1().
I don't really know when PHP frees temporary variables, but my guess
was that they are freed when the scope is left.

If that is not true, then forget whatever I said.

But if it is true, then there is no inherent difference between the
two version. The only difference is that explicit $variables would
need an entry in the active symbol table, which is pretty much
negligible.

---

## #58966 — Paul Dragoonis — 2012-03-15T17:39:00+00:00

URL: https://externals.io/message/58957#58966 · In reply to: #58965

> 
> > […]
> I don't really know when PHP frees temporary variables, but my guess
> was that they are freed when the scope is left.

Each variable has a refcount, then that hits 0 it can be freed up.

>
> If that is not true, then forget whatever I said.
>
> But if it is true, then there is no inherent difference between the
> two version. The only difference is that explicit $variables would
> need an entry in the active symbol table, which is pretty much
> negligible.

---

## #58967 — Paul Dragoonis — 2012-03-15T17:41:24+00:00

URL: https://externals.io/message/58957#58967 · In reply to: #58966

> 
> > […]
>
> Each variable has a refcount, then that hits 0 it can be freed up.

To add to that. A zval will have a refcount, so if you do $a =
someFunc(); then $a will have a refcount.

If you do something like $a = someFunc(anotherFunc(moreFunc())), the
return values of anotherFunc() and moreFunc() will be temp stored, but
they will _not_ have a refcount because they never got assigned into a
zval like $a.

Hope that made sense.

>
> > […]

---

## #58969 — Reindl Harald — 2012-03-15T18:58:25+00:00

URL: https://externals.io/message/58957#58969 · In reply to: #58967

Am 15.03.2012 18:41, schrieb Paul Dragoonis:
> > […]
> 
> To add to that. A zval will have a refcount, so if you do $a =
> someFunc(); then $a will have a refcount.
> 
> If you do something like $a = someFunc(anotherFunc(moreFunc())), the
> return values of anotherFunc() and moreFunc() will be temp stored, but
> they will _not_ have a refcount because they never got assigned into a
> zval like $a.

to make sure i understand this really

function myfunc()
{
 $b = internal_function_with_hughe_return_value();
 return false;
}

$b is freed after the function has finished
if not i should write a lot of unset() what
is not possible if you have return $b

---

## #58970 — Michael Stowe — 2012-03-15T19:10:04+00:00

URL: https://externals.io/message/58957#58970 · In reply to: #58969

The $b on this example would be freed as it is in the function's scope, and
not the global scope.  The exception to this would be a static variable
within a function, which would persist for future use within the function.

Class properties on the other hand will persist until the object is
destructed, or until they are unset.

So for your example,
function test() {
$b = 'apple';
return $b;
}

You would not need to unset $b as it resides temporarily within the
function, and does not persist outside of the function.

- Mike




On Thu, Mar 15, 2012 at 1:58 PM, Reindl Harald <h.reindl@thelounge.net>wrote:

>
>
> Am 15.03.2012 18:41, schrieb Paul Dragoonis:
> > […]
>
> to make sure i understand this really
>
> function myfunc()
> {
>  $b = internal_function_with_hughe_return_value();
>  return false;
> }
>
> $b is freed after the function has finished
> if not i should write a lot of unset() what
> is not possible if you have return $b
>
>
>
>
>


-- 
-----------------------

"My command is this: Love each other as I
have loved you."                         John 15:12

---

## #58971 — Reindl Harald — 2012-03-15T19:14:20+00:00

URL: https://externals.io/message/58957#58971 · In reply to: #58970

thanks

exactly what i assumed, but better to be sure instead
wasting somewhere ressources without need :-)

Am 15.03.2012 20:10, schrieb Michael Stowe:
> The $b on this example would be freed as it is in the function's scope, and
> not the global scope.  The exception to this would be a static variable
> within a function, which would persist for future use within the function.
> 
> Class properties on the other hand will persist until the object is
> destructed, or until they are unset.
> 
> So for your example,
> function test() {
> $b = 'apple';
> return $b;
> }
> 
> You would not need to unset $b as it resides temporarily within the
> function, and does not persist outside of the function.
