Difference between revisions of "LSL Constants vs Globals"

From Second Life Wiki
Jump to navigation Jump to search
m
m (Replaced cat w nav)
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
There are some advantages in using global variables over the string and key constants, it's faster and uses less bytecode when it is longer then 3 characters and it used more then twice but it uses more bytecode when it is shorter then 3 characters or it used once. This is easily demonstrated by running the scripts in Figure 1.
There are some advantages in using global variables over the string and key constants when programming in [[LSO]]; it's faster and uses less bytecode when it is longer than 3 characters and is used more than twice, but it uses more bytecode when it is shorter than 3 characters or is used once. This is easily demonstrated by running the scripts in Figure 1.


===Pros===
However, this is only true for [[LSO]] compiled programs. If [[Mono]] is used, then the opposite is true and it uses less memory to use constants. Mono uses only one copy of a constant in memory. In the scripts in Figure 1, using constants saves the memory used to initialize the variable null_key. This is true if LL constants (like NULL_KEY) are used or if user supplied strings (such as "this is a string") are used.
 
===Pros when using LSO===
* Faster
* Faster
* Uses less bytecode when it is longer then 3 characters and it used more then twice.
* Uses less bytecode when it is longer than 3 characters and it used more than twice.
 
===Cons===
===Cons===
* Uses more bytecode when it is shorter then 3 characters or it used once.
* User can accidentally change the value.
* Uses more bytecode when it is shorter than 3 characters or it used once.




Line 26: Line 30:
default {
default {
     state_entry() {
     state_entry() {
         llOwnerSay(llGetFreeMemory());
         llOwnerSay((string) llGetFreeMemory());
     }
     }
}</lsl>
}</lsl>
Line 41: Line 45:
default {
default {
     state_entry() {
     state_entry() {
         llOwnerSay(llGetFreeMemory());
         llOwnerSay((string) llGetFreeMemory());
     }
     }
}</lsl>
}</lsl>
|}
|}
{{Resource Conservation Portal Nav|cat=memory}}
[[Category:LSL]]

Latest revision as of 18:20, 12 March 2014

There are some advantages in using global variables over the string and key constants when programming in LSO; it's faster and uses less bytecode when it is longer than 3 characters and is used more than twice, but it uses more bytecode when it is shorter than 3 characters or is used once. This is easily demonstrated by running the scripts in Figure 1.

However, this is only true for LSO compiled programs. If Mono is used, then the opposite is true and it uses less memory to use constants. Mono uses only one copy of a constant in memory. In the scripts in Figure 1, using constants saves the memory used to initialize the variable null_key. This is true if LL constants (like NULL_KEY) are used or if user supplied strings (such as "this is a string") are used.

Pros when using LSO

  • Faster
  • Uses less bytecode when it is longer than 3 characters and it used more than twice.

Cons

  • User can accidentally change the value.
  • Uses more bytecode when it is shorter than 3 characters or it used once.


Figure 1: Bytecode Cost
Constant Global

<lsl>

dead(){

   key a = NULL_KEY;
   key b = NULL_KEY;
   key c = NULL_KEY;
   key d = NULL_KEY;

}

default {

   state_entry() {
       llOwnerSay((string) llGetFreeMemory());
   }

}</lsl>

<lsl>key null_key = NULL_KEY;

dead(){

   key a = null_key;
   key b = null_key;
   key c = null_key;
   key d = null_key;

}

default {

   state_entry() {
       llOwnerSay((string) llGetFreeMemory());
   }

}</lsl>