LSL Hacks

From Second Life Wiki
Jump to navigation Jump to search

Hacks

A page dedicated LSL Hacks, those things that make your code so much better but at the same time so much worse.


llMessageLinked key parameter as string

VM: LSO Discover: Unknown

Instead of passing a valid key as the forth parameter of the llMessageLinked function, a string value can be used. When the link_message event is triggered, the key can be typecast to a string (implicitly or explicitly) without any value degradation.

<lsl>llMessageLinked(LINK_THIS, 10, "Hello", "World!")</lsl>

Pros

  • Allows for a second string to be sent to the receiving scripts
  • Faster then splitting a single string into two.

Cons

  • May no longer be a valid key value.

llGetListLength(myList) and (myList != [])

VM: LSO Discover: Strife Onizuka

Pros

  • Faster
  • Uses less bytecode

Cons

  • Less readable
  • Will likely be removed in LSL3[1]

++c and c++

VM: LSO Discover: Not Applicable

In LSO LSL (as opposed to Mono LSL) ++c is faster than c++ because of how the bytecode is generated[2]. There are very few applications where ++c can't be used instead.

Pros

  • Faster code.
  • Saves 6 bytes and 2 instructions.

Cons

  • None

(c = -~c) same as ++c

VM: LSO Discover: Strife Onizuka

For the same instruction cost of (++c), (c = -~c) can be substituted giving a 4 byte saves.

Pros

  • Saves 4 bytes of bytecode.

Cons

  • Will fail on hardware that doesn't support two's compliment (unlikely LL will ever use such a platform).
  • Harder to understand.
  • ?

Testing key validity

VM: LSO & Mono Discover: Strife Onizuka

By passing the key value to a conditional, if it is valid and not a NULL_KEY then it will execute the true branch.

<lsl>integer isKey(key in) {

   if(in) return 2;
   return (in == NULL_KEY);

}</lsl>

Pros

  • Fast
  • Easy
  • Painless

Cons

  • Can give false positives if the variable type is not a key
  • can only be tested by itself

if(~c) and if(c != -1)

VM: LSO Discover: Unknown

Instead of using if(c != -1) you can use if(~c). This applies to all conditionals.

Pros

  • Faster (60%)
  • Uses less bytecode (6 bytes, 1 instruction)

Cons

  • Harder to understand
  • Only for Integers

myList = (myList = []) + and myStr = (myStr = "") + Hack

VM: LSO Discover: Strife Onizuka

This hack works equally well for both strings and lists.

Instead of using myList = myList + addition you can use myList = (myList = []) + myList + addition which will in certain situations reduce memory fragmentation. Memory fragmentation can result in what appears to be a memory leak. This works because LSL execution is Right-To-Left, it frees the value stored at the variable's memory location after copying it to the stack but before storing the return back to the location; the result can be better memory compacting.

Pros

  • Possibility of reduced memory fragmentation

Cons

  • More costly in bytecode and slower
  • Doesn't work in LSLEditor (LSLEditor uses Left-To-Right order of execution).
  • No real benefit when used in Mono
  • Possibility of slightly increasing memory fragmentation

Notes

  • Be sure to test it both ways around before using this.
  • This can also be used with any function.
    • llDeleteSubList((myList = []) + myList, x, y)

32bit hexadecimal notation instead of negative integer notation for constants (ex: 0xFFFFFFFF instead of -1)

VM: LSO (MONO unknown) Discover: Void Singer

instead of using negative integer constants in code (does not apply to global declarations) use the 32bit hexadecimal notation. Works because the compiler does not optimize out the negation sign for integer literals (LSO confirmed, MONO unknown, but not harmful)

Pros

  • Faster (one less operation)
  • Less Byte Code (2 bytes saved on the operation)

Cons

  • Harder to read

VM Detection

VM: Both Discoverer: Void Singer

<lsl>(llToLower( "Ü" ) == "Ü"); //-- yields TRUE for LSO, and FALSE for MONO. (llToLower( "Ü" ) != "Ü"); //-- yields TRUE for MONO, and FALSE for LSO.</lsl>

  • No Pros or Cons

VM Detection

VM: Both Discoverer: Strife Onizuka

<lsl>(llGetListEntryType( (list)((key)"") ) & 1); //-- yields TRUE for LSO, and FALSE for MONO. (llGetListEntryType( (list)((key)"") ) >> 2); //-- yields TRUE for MONO, and FALSE for LSO.</lsl>

  • No Pros or Cons

(c & 0x80000000) instead of (c < 0)

VM: LSO Discover: Unknown

Instead of checking if an integer is less than 0 using the comparison operator, the bitwise AND operator can be used to check for the sign-bit. If the sign bit (0x80000000) is true, then the number is negative, and thus less than 0.

Pros

  • Saves one byte

Cons

  • Harder to understand
  • Only for Integers

(c & power_of_two_minus_one) instead of (c % power_of_two)

VM: LSO Discover: Unknown

Instead of using the modulus operator with a power of two (such as 2^2 or 4), the bitwise AND operator can be used with the value of the power of two minus one (such as 3 instead of 4).

<lsl>if(var & 3);</lsl> will work the same as <lsl>if(var % 4);</lsl>

Pros

  • Saves one byte

Cons

  • Harder to understand

Footnotes

  1. ^ Plans for LSL3 are still being worked out, nothing has been finalized, no release date has been set, LSL3 may in-fact never happen.
  2. ^ The LSO LSL compiler does not produce optimized code.