Jump to content

Scripting Memory usage by Functions - Anyone else noticed this?


Love Zhaoying
 Share

You are about to reply to a thread that has been inactive for 2581 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Continuing work on an epic script, I've been "refactoring" some code lately and checking the results of memory usage before and after.

Here, by "refactoring" I mean this:  I find a few lines of code that I am using repeatedly, and turn that code into a function.

To my surprise, it can use MORE script memory to do this. (I am using llGetUsedMemory() to check memory usages, as for this scenario I mostly care about "code" memory usage, not stack/heap usage.)

For a simple example:  If I find several places in my code that I am getting the first character from a string as a "Prefix" using llGetSubString(varname, 0,0) - then I make a function called "GetPrefix" that just returns the first character of the string.  I know this is not a very good example, as it only represents a single line of code.

What I found is this:  If I create a function, it always uses an additional 512 bytes minimum of script memory!  Depending on the number of lines in the function, it actually SAVES memory to NOT use a function and to repeat the same code in-line instead.

On the other hand, depending on the number of lines of code, and how many places it is called, the 512 bytes always used by the function is made up for by having the function call.  If the function has say, 10 lines of code then it would "wash" and the savings is about the same either say (again, depending on the code and number of times it is repeated).

So far, doing this I have managed to save 2K total of memory in one script just by NOT writing user functions in some cases.  That's not a lot, but when memory is tight it FEELS like a lot!

The next part of this experiment is that  I will combine two "medium-sized" functions which perform similar operations into 1 "larger" function which switches to decide exactly what operations to perform - and see if I still save that 512 bytes (from the extra function call) plus the savings from not repeating code in the two similar functions.  My ultimate goal is to save about 16k by these "tweaks".  It is all very counter-intuitive.

Has anyone else noticed this? (Disclaimer, of course I only use Mono settings. Also, I am a professional programmer.)

Link to comment
Share on other sites

This same issue pops up in virtually every programming environment. There is always some overhead for a function call, and you must balance that against the savings of sequestering some block of code in one. LSL's overhead is particularly egregious, but even in assembly language I've found that repeating a small block of code can be more efficient (in code size, speed, or both) than function calls. When speed is of the essence, even loops can kill.

Most programmers don't need to worry about such tradeoffs, as compute resources aren't as constrained these days. And the best of today's compilers can determine whether a function should be called or in-lined, and whether loops should be unrolled.

LSL is an odd thing. The 512 byte block size may be the result of some need to move scripts around in memory while they're executing. That's easier to do if you can contain sections of code in blocks with well defined entry and exit points, and with a size that's friendly to the memory management and file systems.

Edited by Madelaine McMasters
Link to comment
Share on other sites

32 minutes ago, Love Zhaoying said:

Has anyone else noticed this?

Yes, in fact.  It's reasonably well documented in http://wiki.secondlife.com/wiki/LSL_Script_Memory#Functions  .  That section seems to have been updated most recently by Omei Qunhua, if I interpret the page history correctly.  Notice, in particular, her comment that " Tests in January 2013 show that user functions in Mono no longer (if ever) automatically take up a block of memory (512 bytes) each. As with all Mono code, an extra 512-block of memory is used when needed, but not automatically per user function. In addition the perceived memory usage can vary depending, it is thought, on the action of periodic garbage collection. Inserting an llSleep() for instance, maybe allowing garbage collection to jump in, before reading memory usage, can show a reduction in space used. "

You may want to review that entire section of the LSL Portal, as well as http://wiki.secondlife.com/wiki/LSL_Script_Efficiency  and http://wiki.secondlife.com/wiki/LSL_Hacks

I am not a professional programmer myself and am rarely concerned about the perils of tight memory.  Efficiency, on the other hand, is always a worthy goal, so I've often turned to those sections for insights about how to write better code in LSL.

  • Like 2
Link to comment
Share on other sites

1 hour ago, Rolig Loon said:

Yes, in fact.  It's reasonably well documented in http://wiki.secondlife.com/wiki/LSL_Script_Memory#Functions  .  That section seems to have been updated most recently by Omei Qunhua, if I interpret the page history correctly.  Notice, in particular, her comment that " Tests in January 2013 show that user functions in Mono no longer (if ever) automatically take up a block of memory (512 bytes) each. As with all Mono code, an extra 512-block of memory is used when needed, but not automatically per user function. In addition the perceived memory usage can vary depending, it is thought, on the action of periodic garbage collection. Inserting an llSleep() for instance, maybe allowing garbage collection to jump in, before reading memory usage, can show a reduction in space used. "

You may want to review that entire section of the LSL Portal, as well as http://wiki.secondlife.com/wiki/LSL_Script_Efficiency  and http://wiki.secondlife.com/wiki/LSL_Hacks

I am not a professional programmer myself and am rarely concerned about the perils of tight memory.  Efficiency, on the other hand, is always a worthy goal, so I've often turned to those sections for insights about how to write better code in LSL.

Thanks, I also noticed it does not ALWAYS take minimum 512 bytes to add a function, just ALMOST always.

Link to comment
Share on other sites

1 hour ago, Rolig Loon said:

Yes, in fact.  It's reasonably well documented in http://wiki.secondlife.com/wiki/LSL_Script_Memory#Functions  .

 

15 minutes ago, Love Zhaoying said:
14 minutes ago, Love Zhaoying said:

Some of the information appears very old and refers mostly to LSL (LSL_Hacks especially).  Thanks anyway, the first link was most useful.

 

Link to comment
Share on other sites

Just now, Love Zhaoying said:

Some of the information appears very old and refers mostly to LSL (LSL_Hacks especially).

I agree.  There are a lot of old scripts bouncing around SL, though, so you can run into some of those hacks occasionally. That page has saved me a bit of head scratching in the past.  ;)

Link to comment
Share on other sites

That's why I like old freebie scripts so much; I look at the code, see weird voodoo stuff going on, and then I look it all up and rewrite the script in a cleaner, updated version. It's a really fun way to learn more about scripting (by breaking it all and having to fix it, lol), while at the same time giving yourself nice "modern" versions of useful scripts (once they're fixed).

I've read before about the extra block of script memory thing, but with the majority of what I do myself it's never seemed to be an issue. I try to only make separate functions to call if it saves lots of repetition in the script itself, though. I figure if it makes the script a tenth of the previous length, it's worth it, because it saves a lot of time in making edits and ideally makes the whole thing go faster.

Iunno if it really makes much difference, but it seems to make sense to me that a much shorter script is kinda balanced by the potential allocation of a bit more memory... Hmm...

  • Like 1
Link to comment
Share on other sites

This worked out so far, I am very close to saving 4K just by combining functions (until I add more new functionality, of course).  

Who would have thought that having functions to "simplify" your code eats so much memory?  Not me, and the information was not easily obtainable - except the links you gave, which deny it is as big a problem under Mono - WRONG in my experience so far!

Thanks again!

Link to comment
Share on other sites

On 6/3/2017 at 7:23 AM, Nova Convair said:

I have never put too much efforts into optimizing scripts. Readability and my comfort is more important.

I agree, too bad I needed more memory or I would not have made any changes at all. Code readability really suffered once I combined ("unrolled") all the functions.

Link to comment
Share on other sites

I'm certainly not anywhere near either end of the spectrum myself; I think my personal feelings on good coding practices fall somewhere between "does it work?" and "can I make sense of it?" xD

I used to be into Perl, and regular expressions ate my brain... I'd get 50 lines of code down to a single line of regex, and it'd work wonderfully, and later I'd want to change it and couldn't even reverse-engineer my own work, it was so foreign to human thinking... So yeah, I think a happy medium is the most reasonable approach. If it works well, and you can go back and change it easily, hey, it's good. ^-^

  • Like 1
Link to comment
Share on other sites

And you're still functional. I'm not quite that hardy, but I applaud your lasting this long! xD

Actually, I've thought before how nice it would be to have some regular expressions of some sort in LSL; it might make some things way easier.

And you know, thas also why I pooh-pooh anyone's attempts at pooh-poohing LSL for not having what some of the other scripting languages out there have. If SL people decide they need LSL to do something, and LSL can't do it, then they find a way. I think the challenge of overcoming limitations in the language through creative thinking is just as much a part of why I do this stuff as, say, wanting to make a toy that farts on command. Possibly the better and nobler part of it, too, considering the whole farting toy aspect thing...

Link to comment
Share on other sites

14 hours ago, Berksey said:

If SL people decide they need LSL to do something, and LSL can't do it, then they find a way. I think the challenge of overcoming limitations in the language through creative thinking is just as much a part of why I do this stuff as, say, wanting to make a toy that farts on command. Possibly the better and nobler part of it, too, considering the whole farting toy aspect thing...

Yep! Even if it means several years of stopping and starting over, complete rewrites, splitting into multiple scripts with async communications, etc.  LSL is a very iterative experience. If you can't figure it out the first time or it seems to complicated, next time you will probably succeed.

14 hours ago, Berksey said:

And you're still functional. I'm not quite that hardy, but I applaud your lasting this long! xD

I pretend to be functional on a daily basis.  This is giving me a much-needed brain exercise to distract from a dysfunctional reality!

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2581 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...