Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

types:list [2017-09-20 10:13 SLT]
sei Add Limits
types:list [2021-02-01 09:00 SLT] (current)
sei Info on passing lists by reference
Line 1: Line 1:
 $nav $nav
-===== List type =====+===== Type: list =====
  
 A list is a sequentially numbered collection of elements of other types. It's the closest thing LSL has to what other languages call an array. Lists can have any number of elements, from zero to as many as the available memory can hold. You can think of a list as a row of a spreadsheet in which each cell has an element that can be one of the LSL types, but instead of having the columns labelled "​A",​ "​B",​ and so on, they are numbered starting from zero. We refer to that numbering as the index or position of the element. A list is a sequentially numbered collection of elements of other types. It's the closest thing LSL has to what other languages call an array. Lists can have any number of elements, from zero to as many as the available memory can hold. You can think of a list as a row of a spreadsheet in which each cell has an element that can be one of the LSL types, but instead of having the columns labelled "​A",​ "​B",​ and so on, they are numbered starting from zero. We refer to that numbering as the index or position of the element.
Line 47: Line 47:
  
 Internally, if ''​L''​ is a list and ''​e''​ is of any type other than a list, ''​L + e''​ is more efficient than either ''​L + [e]''​ or ''​L + (list)e''​. Internally, if ''​L''​ is a list and ''​e''​ is of any type other than a list, ''​L + e''​ is more efficient than either ''​L + [e]''​ or ''​L + (list)e''​.
 +
 +That applies to code memory usage. However, when the element already exists in a list, it's more efficient, from the point of view of data memory usage, to append it using $lfn[llList2List]. See [[#Data memory usage]] below.
 </​WRAP>​ </​WRAP>​
  
Line 91: Line 93:
 </​file>​ </​file>​
  
-==== Memory ​====+==== Data memory usage ====
  
 This section is intended for advanced scripters. This section is intended for advanced scripters.
Line 100: Line 102:
 | $ty[integer] | 16 | | $ty[integer] | 16 |
 | $ty[float] | 16 | | $ty[float] | 16 |
-| $ty[string] | 18 + size of the string in UTF-16 (*) (†)  |+| $ty[string] | 18 + size of the string in UTF-16 (*)  |
 | $ty[key] | 30 + size of the string in UTF-16 (*)  | | $ty[key] | 30 + size of the string in UTF-16 (*)  |
 | $ty[vector] | 24 | | $ty[vector] | 24 |
Line 107: Line 109:
 (*) That's typically 2 bytes per character, but there are some special characters that take 4 bytes each, e.g. <​html><​code>&#​x1d538;</​code></​html>​ (*) That's typically 2 bytes per character, but there are some special characters that take 4 bytes each, e.g. <​html><​code>&#​x1d538;</​code></​html>​
  
-(†) If the string is reused ​(//​[[https://​en.wikipedia.org/​wiki/​String_interning|interned]]//​ is the technical term), the memory taken is just bytes. Strings ​are reused if they come from a string literal used in a variable or in some other list, or from $lfn[llList2String],​ $lfn[llList2List],​ $lfn[llList2ListStrided],​ $lfn[llListSort],​ etc. as long as the original still exists. However, strings that are the result of some operation, ​e.g. addition of strings ​or $lfn[llToLower],​ $lfn[llGetSubString],​ etc. are not reused ​even if they match previously ​existing string (but they are still reusable)Key entries ​are not reused, but if cast to string, that string can be reused.+List elements can be reused. ​A reused element takes only 4 bytes regardless of the typeAll elements can be reused by not extracting ​the elements from the list, but adding them to the list in list form. For example: 
 + 
 +<code lsl2> 
 +list A = (list)1; 
 +A += llList2List(A0, 0); 
 +</​code>​ 
 + 
 +This list takes only 20 bytes (16 for the integer and for a reference to the same integer). 
 + 
 +Strings ​in particular can also be reused if they already exist in a list, as long as they are not the result of some operation ($lfn[llList2String] is not considered an operation, ​but addition of strings ​and other functions such as $lfn[llToLower],​ $lfn[llGetSubString],​ etc. are). If they are the result of an operation, they are not reused, but they are reusable. For example: 
 + 
 +<code lsl2> 
 +// This uses 20 bytes, per the table above: 
 +list A = (list)"​X";​ 
 +// This only adds 4 bytes to the list, because constant 
 +// strings are reused ​if they have been used 
 +// to assign them to some list element that 
 +// still contains the unmodified string, in this 
 +// case the first element of the list: 
 +A += "​X";​ 
 +// This adds 20 bytes to the list because the 
 +// string that results from calculation is not 
 +// reused, even if it matches an existing 
 +// string
 +A = A + ("​X"​ + ""​);​  
 +// However, it is reusable, so this element 
 +// will take 4 bytes only: 
 +A = A + llList2String(A,​ -1); 
 +string B = "​Y";​ 
 +// This adds 20 bytes to the list: 
 +A += B; 
 +// This adds 4 bytes to the list: 
 +A += B; 
 +</​code>​ 
 + 
 +Basically, the rule isn't whether the strings are equal, ​but whether ​they are handled verbatim without applying operations to them. The same trick fails with other types, including keys; adding e.g. a key variable to a list multiple times, results in one new instance of the key being counted for every addition. Same happens for integers, for example. But extracting them as lists still works. 
 + 
 +Lists are passed by reference ​to functionsmeaning ​that when passing a list to a function, a new copy is not created, but instead the same list is used.
  
-==== Limits ​====+=== Limits ===
  
 List size is only limited by available memory. However, list initializers (''​[''​ ... ''​]''​ literals used in global variables) and list constructors (''​[''​ ... ''​]''​ literals used in events or functions) have some limitations in $Mono: List size is only limited by available memory. However, list initializers (''​[''​ ... ''​]''​ literals used in global variables) and list constructors (''​[''​ ... ''​]''​ literals used in events or functions) have some limitations in $Mono:
  
   * List initializers are limited to 4096 elements.   * List initializers are limited to 4096 elements.
-  * List constructors are limited by an internal stack limit on expressions. The stack limit is 1024, but some of it is used internally and while doing operations, ​and you can't use a list constructor with more than 1022 elements and assign it to a variable. +  * List constructors are limited by an internal stack limit on expressions. The stack limit is 1024, but part of it is used internally and while doing operations, ​so you can't use a list constructor with more than 1022 elements and assign it to a variable. 
-    * This same stack limit affects any kind of expression, not just list constructors. It would be too hard to describe how stack is used by expressions,​ but you can work around the limit by breaking up the expression ​into several assignments.+    * This same stack limit affects any kind of expression, not just list constructors. It would be too hard to describe how the stack is used by expressions,​ but if you really have an expression that complex, ​you can work around the limit by breaking ​it up into several assignments.