Are listeners actually expensive, or is this a myth?

Creating scripts
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Are listeners actually expensive, or is this a myth?

Post by John Mela »

I'm considering putting a listener into an object, which would listen to a private (ie large, obscure number) channel for handshaking. There could be 20 or 30 of these objects in a region, but not hundreds.

In testing, I found that 64 objects like that incur no noticeable CPU usage (according to the World graph and Top Scripts) when idle. Obviously, when a message is sent out on that channel there will be processing in the listen() events of the objects, but I'm more concerned about the cost of these scripts when no messages are being sent or received, which would be 99.9% of the time.

Now, listeners have long had a bad reputation in the SL and OpenSim community, with people saying they cause lag, are in short supply, etc. Yet nobody I've ever encountered has been able to quantify these or even verify that there genuinely is a problem in OpenSim code (it's been confirmed by LL that it definitely is a myth in SL, but obviously that's no help to us).

I suspect that a myth may have grown up because of people coding listeners on channel 0, or even on small non-zero channels, which could cause listen() events to fire frequently. And I'm not sure why people say there is a limit on the number of listeners per region.

Can anyone shed any factual light on this?

Thanks!
These users thanked the author John Mela for the post (total 4):
Ilan TochnerShandon LoringZed deTremontAda Radius
User avatar
Shandon Loring
Posts: 1341
Joined: Sat Oct 26, 2013 3:25 am
Has thanked: 961 times
Been thanked: 1581 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by Shandon Loring »

Watching anxiously for comments..

I've always used a number of Listeners for numerous things.
Partly because multi-line scripting workarounds are beyond me, and seem unnecessary.
But then I use llSleep(x) too for the same reason! :lol:
But I'm no super-scripter by any means. So..

Watching anxiously for comments..
These users thanked the author Shandon Loring for the post:
Ada Radius
User avatar
Oren Hurvitz
Posts: 361
Joined: Sun Dec 23, 2012 8:42 am
Has thanked: 19 times
Been thanked: 499 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by Oren Hurvitz »

Listeners incur almost no cost while they're idle (i.e., while messages aren't sent to them). They do incur some cost when receiving a message, and when adding a new listener. This cost is proportional to the number of listeners on the same channel. For example, if you send a message to a channel that has 30 listeners, it will take x30 CPU as when sending a message to a channel that has only one listener. This is true even if you use filters to limit the listeners, because OpenSim still has to take a look at all of the potential listeners to find the ones you actually want. But using different channels does help, because OpenSim doesn't even look at listeners on other channels than the one that the message was sent to.

The usage cost appears to be pretty low, at least if not too many messages are being sent. Sure, OpenSim has to go over all of the potential listeners, but it can probably do this in a few ms. It only becomes a problem if you have a large number of listeners (hundreds?), or if you send many messages per second.

Caveat: this analysis is based on reading the code; I didn't run tests to judge the real-world performance.
These users thanked the author Oren Hurvitz for the post (total 7):
Selby EvansZed deTremontJohn MelaShandon LoringMichael TimelessTess JuelAda Radius
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by John Mela »

Many thanks, Oren - this is extremely helpful!
These users thanked the author John Mela for the post (total 2):
Shandon LoringAda Radius
User avatar
Shandon Loring
Posts: 1341
Joined: Sat Oct 26, 2013 3:25 am
Has thanked: 961 times
Been thanked: 1581 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by Shandon Loring »

Oren Hurvitz wrote:
Sun Jul 11, 2021 7:15 am
Thank you Benevolent Overseer!
This is cool info, and helps my thought processes for sure.
These users thanked the author Shandon Loring for the post (total 2):
John MelaAda Radius
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by John Mela »

Shandon Loring wrote:
Sun Jul 11, 2021 10:15 pm
This is cool info, and helps my thought processes for sure.
Same here. It's one thing doing a bit of testing and guessing at how it works, and another to hear from someone who really understands the codebase and takes the time and effort to give an informed and detailed answer like that.

Thanks again, Oren!
These users thanked the author John Mela for the post (total 2):
Shandon LoringAda Radius
IntLibber Brautigan
Posts: 1
Joined: Sat Aug 21, 2021 3:15 pm
Has thanked: 0
Been thanked: 4 times

Re: Are listeners actually expensive, or is this a myth?

Post by IntLibber Brautigan »

As Oren said, it is based on how many scripts are listening to a given channel vs how much chatter there is on that channel. Thus, you want to script your device to use randomly obscure channels as much as possible thusly:

integer chan = (integer)llFrand(999999); //picks a random float between 0 and 999,999 then rounds it up to the closest integer.

if you have two devices talking to each other, the one generating the random channel needs to tell the other one what channel to use, so set a standard channel for it to communicate the new channel to switch to.

In device 1:

Code: Select all

integer setupchan = 999;
integer chan = (integer)llFrand(999999);

llSay(setupchan,(string)chan);
llSetListenEvent(chan,"","","");
In device 2:

Code: Select all

integer chan;
ingeter setupchan = 999;

start_entry()
}
llSetListenEvent(setupchan,"","","");
}

listen(integer channel, key id, string msg, integer num)
{
   key owner = llGetOwnerKey(llGetDetected(llGetKey()));
  if (owner == llGetOwnerKey(llGetKey()) && channel == startchan )
  {
     chan = (integer)msg;
     llEndListenEvent(0);
     llSetListenEvent(chan, "","","");
  }
}
Device 1 generates the random channel, sends it out on startchan (channel 999), Device 2 hears it, changes channels, turns off the listen on the open channel and starts a listen event on the new randomly chosen channel. Always turn off listens you are not using.
These users thanked the author IntLibber Brautigan for the post (total 4):
Ilan TochnerTess JuelGraham MillsAda Radius
User avatar
Tess Juel
Posts: 267
Joined: Sun Sep 11, 2016 4:24 pm
Has thanked: 249 times
Been thanked: 438 times

Re: Are listeners actually expensive, or is this a myth?

Post by Tess Juel »

IntLibber Brautigan wrote:
Sat Aug 21, 2021 3:30 pm
In device 1:

Code: Select all

integer setupchan = 999;
integer chan = (integer)llFrand(999999);
One question about this snippet. In SL it's consdiered good practice to always use negative channels for scripts so it would be

Code: Select all

integer setupchan = -999;
integer chan = (integer)llFrand(-999999);
Is opensim different there?
These users thanked the author Tess Juel for the post (total 4):
Ada RadiusJohn MelaMike LorreyMichael Timeless
User avatar
Tess Juel
Posts: 267
Joined: Sun Sep 11, 2016 4:24 pm
Has thanked: 249 times
Been thanked: 438 times

Re: Are listeners actually expensive, or is this a myth?

Post by Tess Juel »

Oren Hurvitz wrote:
Sun Jul 11, 2021 7:15 am
The usage cost appears to be pretty low, at least if not too many messages are being sent. Sure, OpenSim has to go over all of the potential listeners, but it can probably do this in a few ms. It only becomes a problem if you have a large number of listeners (hundreds?), or if you send many messages per second.
Tell me if I understand this right. Let's say I have a master switch that turns on and off street lights across the entire sim. Or a master texture changer that switches between seasonal foliages for all trees in the sim. There may well be hundreds, maybe even more than a thousand receivers but there won't be a message sent every day or even every month. Are all those open listens still likely to cause problems?
Last edited by Tess Juel on Sun Aug 22, 2021 5:52 am, edited 1 time in total.
These users thanked the author Tess Juel for the post:
Mike Lorrey
User avatar
Oren Hurvitz
Posts: 361
Joined: Sun Dec 23, 2012 8:42 am
Has thanked: 19 times
Been thanked: 499 times
Contact:

Re: Are listeners actually expensive, or is this a myth?

Post by Oren Hurvitz »

This won't be a problem, because listeners don't use CPU if no message is being sent on the channel that they're listening to.

When you do send a message on that channel there might be a bit of lag in the world, as the message is sent to hundreds of listeners. But after the message has been sent the lag will disappear.
These users thanked the author Oren Hurvitz for the post (total 5):
Tess JuelAda RadiusJohn MelaMike LorreyMichael Timeless
Post Reply