Page 1 of 2
Are listeners actually expensive, or is this a myth?
Posted: Sat Jul 10, 2021 3:38 pm
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!
Re: Are listeners actually expensive, or is this a myth?
Posted: Sat Jul 10, 2021 10:19 pm
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!
But I'm no super-scripter by any means. So..
Watching anxiously for comments..
Re: Are listeners actually expensive, or is this a myth?
Posted: Sun Jul 11, 2021 7:15 am
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.
Re: Are listeners actually expensive, or is this a myth?
Posted: Sun Jul 11, 2021 12:58 pm
by John Mela
Many thanks, Oren - this is extremely helpful!
Re: Are listeners actually expensive, or is this a myth?
Posted: Sun Jul 11, 2021 10:15 pm
by Shandon Loring
Thank you Benevolent Overseer!
This is cool info, and helps my thought processes for sure.
Re: Are listeners actually expensive, or is this a myth?
Posted: Sun Jul 11, 2021 11:52 pm
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!
Re: Are listeners actually expensive, or is this a myth?
Posted: Sat Aug 21, 2021 3:30 pm
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.
Re: Are listeners actually expensive, or is this a myth?
Posted: Sat Aug 21, 2021 4:25 pm
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?
Re: Are listeners actually expensive, or is this a myth?
Posted: Sat Aug 21, 2021 4:38 pm
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?
Re: Are listeners actually expensive, or is this a myth?
Posted: Sun Aug 22, 2021 5:27 am
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.