Difference between revisions of "Talk:LSL Script Efficiency"

From Second Life Wiki
Jump to navigation Jump to search
(propose an article to show how best to call llGetFreeMemory to measure how small is that code)
 
(archive the talk that gave birth to the Code Racer article)
 
Line 1: Line 1:
= How Small Is That Code =
The old talk here archived at http://wiki.secondlife.com/w/index.php?title=Talk:LSL_Script_Efficiency&oldid=37041 gave birth to the [[Code Racer]] article.
 
'''Q: Want to know how small that code is?'''
 
'''A:''' The most accurate, least misleading technique I know is as follows.
 
First compile & run this brief & clear & conventional (though neither fast nor small) script:
 
<pre>
// http://wiki.secondlife.com/wiki/Talk:LSL_Script_Efficiency
 
integer unspent = 16384; // the well-known available size of byte code plus heap plus stack
integer wasted = 313; // the well-known byte size of this script before you add source code to it
 
// Count the bytes newly occupied by new byte code when you added source code to this script.
 
integer getSpentBytes(integer spendable)
{
    return spendable - llGetFreeMemory();
}
 
// Print the bytes spent when you added code to this script,
// whenever you Save or Reset this script.
 
default
{
    state_entry()
    {
        llOwnerSay((string) getSpentBytes(unspent - wasted));
    }
}
</pre>
 
The integer zero should be the result printed when you first try this.
 
See how that works?
 
This code quotes the well-known available size of byte code plus heap plus stack. This code quotes the well-known size of itself. This code calculates the difference between those two quotes, ''i.e.'', the count of bytes that should be free until you add more code.
 
Now you add code, and run the script again. For example, suppose you create a second copy of the getSpentBytes routine, giving its name some otherwise unused spelling like getSpentBites. Then you will see the second copy costs 47 bytes. A third copy costs the same, another 47 bytes for a total of 94 bytes. And so on.
 
Get it?
 
Now you can easily & instantly measure how small any code is.
 
Note: Take care to avoid falling into the easy error of printing llGetFreeMemory() before and after you delete the last function of the script. Adding the first function to the script costs an extra 4 bytes. Only people who understand LSO know why, and they haven't yet published that explanation anywhere near here.
 
= Yes Let's Talk of How Small Is That Code =
 
Our article here says "Efficiency is how much resource a particular script uses to accomplish a goal".
 
Yes exactly.
 
We should measure not only [[LSL Script Efficiency#How_Fast_Does_That_Code_Run]] but also how small is that code, as is conventional in computer science. When we rewrite code to produce equivalent results more efficiently, we trade brevity and clarity and conventionality and space and time off against one another. LSL can only measure space and time. We should show exactly how to measure both of those, in order to invite more people to make such measurements, to build a consensus science of how to write small and fast LSL code.
 
LSL gives us [[llGetFreeMemory]] to measure how small is that code, just as LSL gives us [[llGetTimestamp]] to measure how fast does that code run. We the LSL wiki authors have written the [[Code Racer]] and [[Efficiency Tester]] articles to show how to call llGetTimestamp to measure how fast does that code run. Now likewise we should write an article to show how to call llGetFreeMemory to measure how small is that code.
 
Possibly we should publish that article separately, possibly we should add that article inline into this [[LSL Script Efficiency]] article. Which choice is best, I don't yet know.

Latest revision as of 18:14, 19 October 2007