Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Not returning scalars(SV) to optimize and save memory?

by RaduH (Scribe)
on Dec 06, 2007 at 20:19 UTC ( #655492=note: print w/replies, xml ) Need Help??

in reply to Not returning scalars(SV) to optimize and save memory?

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?

  • Comment on Re: Not returning scalars(SV) to optimize and save memory?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://655492]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2018-07-20 13:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (431 votes). Check out past polls.