llGetListLength

From Second Life Wiki
Revision as of 11:35, 3 October 2012 by Kireji Haiku (talk | contribs) (some speed improvements)
Jump to navigation Jump to search

Summary

Function: integer llGetListLength( list src );

Returns an integer that is the number of elements in the list src.

• list src

Caveats

  • The LSL compiler will not optimize the condition, it will not hoisting this function out of the condition. Unless strictly necessary all work with strings, keys and lists should be manually hoisted and performed outside of the loop. See Best Practices for more details.
All Issues ~ Search JIRA for related Bugs

Examples

<lsl> //Basic usage default {

   state_entry()
   {
       list testList = ["one", "two", "three"];
       integer length = llGetListLength(testList);
       llOwnerSay("There are " + (string)length + " entries in the list.");
   }

}

</lsl>

Notes

Best Practices

When using list length to help you loop through a list, it is better to determine the length first, then start your loop:

<lsl> integer length = llGetListLength(mylist);

integer index;// default is 0 while (index < length) {

   // PUBLIC_CHANNEL has the integer value 0
   llSay(PUBLIC_CHANNEL, llList2String(mylist, index));
   ++index;

} </lsl>

The following example is to illustrate what not to do, it calculates the length in the "for" loop and is inefficient because the length gets recalculated at each pass through the loop. This should only ever be done if the list is in fact changing (in length) with each iteration of the loop[1].

<lsl> integer index;// default is 0 while (index < llGetListLength(mylist)) {

   // PUBLIC_CHANNEL has the integer value 0
   llSay(PUBLIC_CHANNEL, llList2String(mylist, index));
   ++index;

} </lsl>

LSO Optimizations

A faster and lighter (in bytecode) way to determine the length of a list is to do a not-equals compare with a null list. This works because the list not-equals compare returns the difference between the lengths, meaning that it returns the same result as llGetListLength(), minus the overhead in bytecode, and performance penalty of calling a non-native function. Note: This optimization is much less beneficial in Mono. Mono's llGetListLength function is almost twice as fast. <lsl> list in; integer len_in = llGetListLength(in); integer flen_in = (in != []); //flen_in and len_in will be the same

integer neg_len_in = -llGetListLength(in); integer fneg_len_in = ([] != in); //fneg_len_in and neg_len_in will be the same </lsl>

See Also

Functions

•  llListStatistics LIST_STAT_NUM_COUNT – Returns the number of integers and floats in the list
•  llStringLength Returns the number of characters in a string.

Deep Notes

All Issues

~ Search JIRA for related Issues
   Automatically optimise calls to llGetListLength(list) by replacing them with (list != [])

Source

'linden\indra\lscript\lscript_library\lscript_alloc.cpp' @ lsa_cmp_lists
'linden\indra\lscript\lscript_execute\lscript_execute.cpp' @ list_list_operation

Footnotes

  1. ^ Recalculating the length is slow because the VM duplicates the entire list (including the values) when it is pushed on the stack (so it can be popped off the stack when the length is calculated).

Signature

function integer llGetListLength( list src );