in reply to Re^10: Why does each() always re-evaluate its argument? ("for_list" )
in thread Why does each() always re-evaluate its argument?
It's fine to reset the iterator even if they don't use it, so that's not proof. But, in fact you're right; in Perl_hv_pushkv there is
EXTEND_MORTAL(nkeys); EXTEND(SP, ext); while ((entry = hv_iternext(hv))) { if (flags & 1) { SV *keysv = newSVhek(HeKEY_hek(entry)); SvTEMP_on(keysv); PL_tmps_stack[++PL_tmps_ix] = keysv; PUSHs(keysv); } if (flags & 2) PUSHs(HeVAL(entry)); }
I'm rather surprised they didn't optimize that to just iterate the hashtable directly. I'm sure there are some special cases handled by hv_iternext, but they could have just tested for that first.
Extending the stack by one element or by 200,000 has the same fixed performance cost.
How do you figure? It has to allocate the current size + 1.6MB of ram and initialize all of it. (and as the code above shows, it also has to grow the temps stack by 800KB and initialize that) Overwriting a pair of scalars in a loop would extend by 2 and probably fall within the size of the existing stack(s) anyway.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^12: Why does each() always re-evaluate its argument? ("for_list" )
by ikegami (Patriarch) on Dec 11, 2023 at 03:02 UTC | |
by NERDVANA (Priest) on Dec 11, 2023 at 07:26 UTC | |
by ikegami (Patriarch) on Dec 11, 2023 at 14:08 UTC | |
by NERDVANA (Priest) on Dec 11, 2023 at 22:20 UTC | |
by ikegami (Patriarch) on Dec 12, 2023 at 05:16 UTC | |
|
Re^12: Why does each() always re-evaluate its argument? ("for_list" )
by LanX (Saint) on Dec 09, 2023 at 14:53 UTC |