LSL Script Memory

From Second Life Wiki
Revision as of 09:26, 5 April 2017 by Sei Lisa (talk | contribs) (Add findings about interned strings)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Script Memory

All scripts in LSL pre-MONO start out with 16 kilobytes of memory. Mono scripts start with 64 kilobytes, this may be reduced with llSetMemoryLimit(). Additionally, Mono scripts only use the amount of real memory that they currently need, not the amount allocated to them (64kB, or whatever was set by llSetMemoryLimit(). Mono scripts also share the memory needed for their bytecode with all other instances in the same region of the same script copied after compilation. Below is a list of code and the memory usage.

Much of the following data was collected through llGetFreeMemory(). The revised figures for Mono lists were obtained via llGetUsedMemory(). 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 could use corrections.

There are still many bits that could be improved upon, and many more that could use more detail. If anyone has any free time, play around with the memory usage for different functions. A list of needed updates is at the end of the page.

Variables

See the end of this page.

Declaring variables as global

Variable Memory Usage (Pre Mono)

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 (MONO) (Revised findings as of Jan 2013)

integer  16
float    16
string   18 + 2 per character
interned string  4 (must be inserted as a reference to a pre-existing string)
key      102 
key as string  90
vector   24
rotation 28
list     Lists Can Not Contain Lists

For other results relating to Mono see https://wiki.secondlife.com/wiki/User:Omei_Qunhua

List Memory Usage (Pre MONO)

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


String entries have a constant 8 for pre-compile text.

myList = ["testing", "1", "asdhdgkjajlgsdfdsw"]
   (8, 8, 8)
myList = [llGetSubString("testing", 0, -1), (string)1, llToUpper("asdhdgkjajlgsdfdsw")]
   (22 + 14, 22 + 2, 22 + 36)

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

integer i; //10 bytes

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

Functions

Declaring functions

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

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.

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

list f() {  //16 (function) + 4 (return)
    list ret = [0]; //11 (list) + 7 (integer)
    return ret; 6 (list)
}
string f() {
    return "";
}

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

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

string s; //12 bytes
s;        //6 bytes
"";       //3 bytes (see below)
s = "";   //8 (6 (string) + 3 (null string) - 1) bytes

However...

string s = ""; //12 bytes
integer i; //15 bytes
i = i + 1 // 6 bytes (integer) + 6 bytes (integer) + 6 bytes (1) + 1 byte (addition) - 1 byte (assignment)

Statements

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

Examples

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

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?
default {
     state_entry() {
          llOwnerSay((string)llGetFreeMemory());
          list l = ["", "", "", ""];
          llOwnerSay((string)llGetFreeMemory());
     }
}