Well, here's my opinion based on whatI remember about programming languages design and implementation and not Perl in particular... The return in foo is useless so the only optimization I see here is to simply ignore it. It does not use memory and does nothing different from simply exiting the subroutine. In other cases, a similar return without parameters might actually make a difference because it might prevent other code in the subroutine to be executed and simply pops the stack and returns the control to the parent block of code that called the subroutine. It is a simple "traffic control" mechanism.

For the second subroutine, you use memory when you return. I would argue that you create a result value (in this case a boolean, although Perl doesn't use booleans explicitly). Whatever that value is, it takes up memory space to store the result of your comparison. You may think that by defining the values above you save memory but you don't. You return a copy of that value. Even assuming return uses references (which I'd say is not the case) you'd still be using memory to store the reference to the value defined up top. Your bar return does more than flow of control, it actually passes a value up to the parent.

For this reason, I think the claim that Perl does not use additional resources to represent that value internally is incorrect. It is a bool after all (Perl chooses not to call it so) but it is a value and that takes up space. If you optimize it to save that space it pretty much means you destroy what yo want to return. It seems you'd be looking for a meaningful value but are willing to spend zero to store it.

Again, my 2c without knowing intimate details about how Perl handles the situation, but based on this line of thinking, I suspect there's little room for other scenarios. Anybody has any other idea?


In reply to Re: Not returning scalars(SV) to optimize and save memory? by RaduH
in thread Not returning scalars(SV) to optimize and save memory? by Withigo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.