LSL Script Memory

From Second Life Wiki
Revision as of 14:58, 30 March 2008 by Uzume Grigorovich (talk | contribs) (→‎Needed Updates: lsl code tagging)
Jump to navigation Jump to search

Script Memory

All scripts in LSL start out with 16 kilobytes of memory, and that memory is used up rapidly. Below is a list of code and the memory usage.

All of the following data was collected through llGetFreeMemory(). As the LSL compiler is not a simple program, the data below may not be 100% accurate, in fact, much of it is wrong and can use corrections.

There are still many bits that can be improved upon, and many more that can be taken into more detail. If anyone has any free time and doesn't know what to do, play around with the memory usage for different functions. A list of needed updates can be seen at the end of the page.

Variables

Declaring variables as global

Variable Memory Usage

integer  10
float    10
string   18 + 1 per character
key      18 + 1 per character
vector   18
rotation 22
list     21 + list memory usage

List Memory Usage

integer  15
float    15
string   12 + 1 per character
key      12 + 1 per character
vector   23
rotation 27
list     Lists Can Not Contain Lists

Declaring variables inside default state

Variable Memory Usage

integer  15
float    15
string   12 + 1 per character
key      12 + 1 per character
vector   31
rotation 39
list     15 + list memory usage

List Memory Usage

integer  7
float    7
string   4 + 1 per character
key      4 + 1 per character
vector   23
rotation 30
list     Lists Can Not Contain Lists

Simply stating values

Putting a Value onto the Stack

Integers 1 + 4 bytes for the value
Float    1 + 4 bytes for the value
String   1 + bytes for characters + 1 byte for null
Key      1 + bytes for characters + 1 byte for null
Vector   1 + 3 * float cost
Rotation 1 + 4 * float cost
List     1 + 4 for list length + list memory usage

To remove a value from the stack costs 1 byte.

List Memory Usage

Integers 7
Float    7
String   4 + 1 per character
Key      4 + 1 per character
Vector   23
Rotation 30
list     Lists Can Not Contain Lists

Constants

All integer constants use 6 bytes of memory.

Other Constants

ZERO_VECTOR   16
ZERO_ROTATION 21
NULL_KEY      39

Extras

6 bytes to reference variables

Examples

<lsl> integer i; //10 bytes

default {

state_entry() {
 list l = ["Testing", "This"] //15 (list) + 15 (string) + 8 (string)
}

} </lsl>

Functions

Declaring functions

Functions require 16 bytes to be created, with 3 bytes per paramater, plus the return type.

Return Types

integer  4
float    4
string   4
key      4
vector   20
rotation 27
list     4

Declaring variables in functions

Variable Memory Usage

integer  11
float    11
string   8 + 1 per character
key      8 + 1 per character
vector   19
rotation 23
list     11 + list memory usage

List Memory Usage

integer  7
float    7
string   4 + 1 per character
key      4 + 1 per character
vector   23
rotation 30
list     Lists Can Not Contain Lists

Calling functions

21 bytes to call a function with no return
21 bytes to call a function with any return + type of return
Subtract number of parameters entered

Return Types

integer 2 float 2 string 10 + 1 per character returned key 10 + 1 per character returned vector 2 rotation 2 list 18 + list memory usage

Examples

<lsl> list f() { //16 (function) + 4 (return)

   list ret = [0]; //11 (list) + 7 (integer)
   return ret; 6 (list)

} </lsl> <lsl> string f() {

   return "";

}

default {

   state_entry() {
       f();   //21 (call to f) + 10 (returns string)
   }

} </lsl>

Operators

List of Operators

+  1
-  1
*  1
/  1
%  1
&  0
|  0
^  0
!  0
>> 0
<< 0
~  1
== 1
<= 1
<  1
>= 1
>  1
!= 1

Assignment

Assigning values to variables takes as many bytes as used minus one.

Examples

<lsl> string s; //12 bytes s; //6 bytes ""; //3 bytes (see below) s = ""; //8 (6 (string) + 3 (null string) - 1) bytes </lsl> However... <lsl> string s = ""; //12 bytes </lsl> <lsl> integer i; //15 bytes i = i + 1 // 6 bytes (integer) + 6 bytes (integer) + 6 bytes (1) + 1 byte (addition) - 1 byte (assignment) </lsl>

Statements

if    6
while 11
for   11
do    6
jump  5
@     0
state 5

Examples

<lsl> if (5 < 10) { // 6 (if) + 6 (integer) + 6 (integer) + 1 (compare) //Do something here } </lsl> <lsl> while (1 < 2 & 3 < 4) { // 11 (while) + 4*6 (4 integers) + 2 (2 compares) //Do something here } </lsl>

Typecasting

integer  10
float    10
string   10
key      10
vector   10
rotation 10
list     25

States

14 bytes for any event in a state + 1 for each parameter 17 bytes to create a new state

Needed Updates

Several updates are needed for this page, the most needed are listed below:

  • Verification of all data
  • Clarification of explanations
  • Investigations into how the functions work
    • Calling of functions
    • How return types effect the return
  • In the following code, the call to llGetFreeMemory() returns a different value. How is it affected? Why?

<lsl> default {

    state_entry() {
         llOwnerSay((string)llGetFreeMemory());
         list l = ["", "", "", ""];
         llOwnerSay((string)llGetFreeMemory());
    }

} </lsl>