roomlist undefined issue, possibly not a bug?

You think you've found a bug? Please report it here.

Moderators: Lapo, Bax

Post Reply
PercyPea
Posts: 4
Joined: 04 Jul 2007, 22:00

roomlist undefined issue, possibly not a bug?

Post by PercyPea »

Hiya, am not sure if this would actually have any perfomance affect, but i noticed recently that the roomlist can baloon in length and just contain 100's of underfineds. I just thought i'd let you know incase this is a problem? I'm using smartfox pro 1.6.1, see below for detailed walkthrough on how i get the long undefined list.

I tested this also with your advance chat example

When you restart smartfox, and join the main lobby, tracing roomlist (simply stick a 'trace(roomlist)' in the chat frames smartfox.onRoomListUpdate function) returns 'undefined,[object Object]'

I'm not sure why there is a first undefined, but the object is obviously the main lobby.

If i open another instance of the advance chat, and create three rooms, when i re-login in the flash IDE version and trace the room list, i get
'undefined,[object Object],[object Object],[object Object],[object Object]'

All good so far.

However, if in yet another instance of the advancedChat swf, i create two more rooms. Then exit the first instance where i created the 3 rooms. In the flash IDE version, when i log in and trace, i get 'undefined,[object Object],undefined,undefined,undefined,[object Object],[object Object]'

the 3 undefineds are where the rooms used to be placed in the list, and I'm guessing they havent been removed fully from the list, as then the two new rooms at the end of the list, their ID wont match the list position ID.

However, if i were to then close that 2nd instance, with the 2 rooms created. I then re-run the flash IDE version and trace the room list. I get 'undefined,[object Object]' again.

Yep, i know, still no problem

but.....

Adding another room with yet another instance of the advanced chat, will mean that next time i trace the roomlist from within the IDE, i get:-
'undefined,[object Object],undefined,undefined,undefined,undefined,undefined,[object Object]'

Basically, the roomlist still remembers that in the past there were 5 rooms created, so it puts this new room after those 5.

Ultimately, this means that if enough rooms are created and closed, you could end up with a roomlist filled with 1000's of undefineds from 'ghost' rooms, and as i say, i'm not sure if that will affect performance or not? :)

It could also be a case that you do a cleanup routine every 30 mins or something like that which i havent noticed :)

Pea
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Nice post :)
The issue is related with how Actionscript arrays are managed internally.

Try this code:

Code: Select all

var list:Array = []

list.push( {label:"apple", id:0} )
list.push( {label:"orange", id:1} )
list.push( {label:"pear", id:2} )
list.push( {label:"banana", id:3} )

trace("->" + list)
delete list[2]
trace("->" + list)
It will output:

Code: Select all

->[object Object],[object Object],[object Object],[object Object]
->[object Object],[object Object],undefined,[object Object]
This one will be even more eloquent:

Code: Select all

var list:Array = []

list[0] = "Hello"
list[10] = "World!"

trace(list)
Result:

Code: Select all

Hello,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,World!
Not bad, isnt' it? :shock: :D

Actually it makes a lot of sense. It simply shows that all the elements between index 0 and 10 are not referencing any values.
At times this can be confusing because you'd expect that the above list would just contain 2 elements, but this is not the case because when I set the value for the array at index 10 I automatically "resize" it to an array of 10 elements.

The room list works exactly like that: it keeps track of rooms by using their numeric ID as the key.
On the server side SFS uses an auto-increment ID for each room created, so after long running sessions you may have very high ID numbers which may lead to lots of those undefined values

Fortunately those empty/void references don't affect performance and they are ignored when looping on the array ( using for...in )

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
PercyPea
Posts: 4
Joined: 04 Jul 2007, 22:00

ooo

Post by PercyPea »

Hi Lapo, in case you saw it, i had written a long post saying why there would be a performance issue. I was though very very wrong :)

I hadnt realised the 'delete' option does something magic to the array and therefore for loops dont worry about scanning those deleted positions.

In a test i did i simply had references to an undefined object, ie

Code: Select all

room1=new Object()
room3=new Object()
array=[room1,room2,room3]
trace(array)
would return [object Object],undefined,[object Object]

so even though tracing the array made it look the same as the roomlist did, the for loops actually go through every single value, including the undefineds, so take alot longer.

Thanks for your post explaining it :)

To be clear to others, if i then were to have

Code: Select all

room1=new Object()
room3=new Object()
array=[room1,room2,room3]
trace(array)
delete array[1]
trace(array)
despite both traces returning the same.

Having now deleted the reference to the undefined room2, when you look through the array using for..in , it would ignore that position, whereas previoulsy it would have used it.
poppop
Posts: 57
Joined: 07 Jul 2007, 10:24

Post by poppop »

Lapo wrote:when I set the value for the array at index 10 I automatically "resize" it to an array of 10 elements.

This is actually a question i was going to ask later -- but this post is already on the issue, so i will ask here. :)


Say i do this:

Code: Select all

list[0] = "Hello" 
list[10000] = "World!" 

Making an array 10,000 long (or more!), would this slow the server down?

Just having an array that size. Would it be a problem doing somthing like this:

Code: Select all

trace(list[7888])
Would a Loop and such a big array take a lot of resouces?

Code: Select all

list[10000] = "hi"
for(i=0;i <list.length;i++){

if(list[i] = "hello"){
trace("great!")
}


Also, if you see my other thread, i cannot seem to make an empty array on the server:

var list:Array = []

list.push("hi")

I just get an error: HERE


Thanks for the help.
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Post by BigFIsh »

Server won't accept the :Array for some reason. So, therefore don't use it. Instead use var mylist = new Array(); in the meanwhile.

Yes, it would in theory slow down the server with massive array with many unknowns.

However, you could use the:

for (var m in mylist) {
}

to loop through every avaliable array that isn't undefined.

Or ever better, read these: http://lab.polygonal.de/2007/05/23/data ... eue-class/
http://lab.polygonal.de/2007/08/13/data ... ked-lists/
it behaves like an array but it is more effective in term of deletion and adding and looping through them.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

poppop:
Making an array 10,000 long (or more!), would this slow the server down?
In general, no. To be more precise, it depends on what you do with it.

If your array does not uses contiguous elements, don't use and indexed for loop, use the for...in instead.
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply