Skip to content
JasXSL edited this page Nov 29, 2017 · 9 revisions

Here are some good code practices to prevent stack heap collisions in LSL. You may notice that a lot of these goes against programming 101, but this is LSL and we'll have to make do with less than 64k.

Use macros for functions that don't require a subscope

If your function doesn't require bracket {} such as for, if, or while. Use a macro instead. Functions always consume at least 512 bytes in LSL, which adds up pretty quickly. If your function doesn't need an extra scope, it would be wise to rewrite it as a macro. Here's a function that changes the object's name and says something:

// As function (memory intensive)
sayAs( string name, string text ){
  llSetObjectName(name);
  llSay(0, text);
}

// As macro (cheap, note the \ allowing you to use multiple lines in macros)
#define sayAs( name, text ) \
  llSetObjectName(name); \
  llSay(0, text)

Don't make functions if they're only used once

As I explained before, functions use a lot of extra memory. If you define a function and only use it at a single location of your script, it's much better to write it inline.

Use a #ROOT script to gate nearly all events.

Events are also memory intensive. This is why XOBJ uses a #ROOT file to forward all events.

Use bitwise operations

ELI5. Bitwise operations lets you use each bit in a byte (32 bits in LSL). The most basic example for this is integer BFL; which you'll see in a lot of JasX scripts. This allows you to use a single integer to contain 32 booleans. LSL integers under mono consume 16 bytes, so by using this instead of integers you can essentially store 512 bytes using only 16 bytes. This can be used to store bigger numbers as well. The GoThongs resource integer put into the description of a HUD is a combination of 4x 7-bit integers, allowing 4x resource percentage values to be passed in a single integer. It has the added benefit of being really fast.

Use preprocessor constants

You might be familiar with creating constants as global variables. For an example: key USER = "cf2625ff-b1e9-4478-8e6b-b954abde056b"; This is wasteful. If USER should never change, use #define USER "cf2625ff-b1e9-4478-8e6b-b954abde056b" instead.

Use Mono

There's no reason to use LSL2.

Use llSetMemoryLimit()

If your script doesn't need to receive an unknown amount of data like lists that grow or shrink based on external input, you should use llSetMemoryLimit(). You can probably toy around with the values, but I usually use memLim(1.5) which is part of the libJasPre preprocessor library included in XOBJ. This sets the max memory of the script to 1.5x the starting memory use.