Table or array (the title wasn't long enough to let me post)

Started by PunkNoodle, Feb 12, 2017, 01:41 AM

Previous topic - Next topic

PunkNoodle

Note: This is a repost from another forum because I know here it gets more visibility, also if possible I'd like @Shadow himself to have a look and give an opinion about it.

Which one of these two methods is better and why?
I'm not interested to know which one is better as much as knowing why one is actually better. Keep that in mind before replying


1. Keeping players in an array. Shadow's method

playerson <- [];

function onPlayerJoin( player )
{
playerson.push( player.ID );
}

function onPlayerPart( player, reason )
{
playerson.remove( player.ID );
}


2. Keeping players in a table. himselfe's method

players <- {};

function onPlayerJoin( player )
{
players.rawset(player.Name,player);
}

function onPlayerPart( player, reason )
{
players.rawdelete(player.Name);
}

vito

What is means 'better'? It depends what do you need to do. Arrays should be faster anyway.

DizzasTeR


KAKAN

playerson.push( player ) instead of ID, is better.



use tables if you need to, for ex, an account system. Store the information and if the player joins the server again, instead of fetching the data from the database, you can use the table to retrieve it much quicker( since it stores the data on RAM. ). While, for ex, storing the list of online administrators or something similiar will be better with a array, than in a table, since you don't need key-value thingy there.



Quote from: vito on Feb 12, 2017, 02:39 AMWhat is means 'better'? It depends what do you need to do. Arrays should be faster anyway.
Yes they're, but only if you got like a million rows or they're nearly equal.
Arrays are better when you got less RAM and have to store more data, but instead of using multi-dimensional array for key-value storing, you may use tables instead.
oh no

vito

Also which symbols is allowed to be a key? I've checked even numbers is allowed.
Quote from: KAKAN on Feb 12, 2017, 04:03 AMuse tables if you need to, for ex, an account system. Store the information and if the player joins the server again, instead of fetching the data from the database,
I am sure most of vc-mp servers is not using such architecture currently.

KAKAN

Quote from: vito on Feb 12, 2017, 04:20 AMI am sure most of vc-mp servers is not using such architecture currently.
Couldn't understand.

Quote from: vito on Feb 12, 2017, 04:20 AMAlso which symbols is allowed to be a key? I've checked even numbers is allowed.
I guess its allowed...
oh no

vito

Quote from: KAKAN on Feb 12, 2017, 04:52 AMCouldn't understand.
People here usually using an array for player's data and when player disconnecting they removing this data. Unfortunately storing player's data when he is offline is too hard for most of server's developers.
Quote from: KAKAN on Feb 12, 2017, 04:52 AMI guess its allowed...
So no any limits for name of key in tables?

DizzasTeR

You can use strings as well as numbers for keys in Tables, that has always annoyed me in case of arrays.

vito

Quote from: Doom_Kill3R on Feb 12, 2017, 05:33 AMYou can use strings as well as numbers for keys in Tables, that has always annoyed me in case of arrays.

Well, I have tested

players <- {};
local arr = ["a", "1a", "фы", "`", "\"", ".", 0x01, false, [], ""];
for(local i = 0; i < arr.len(); i++){
players.rawset(arr[i],(arr[i] +"-"+ i));
print(players.rawget(arr[i]));
}
it seems only 'null' can't be a key. Everything else is fine.

Very strange for me.

KAKAN

Quote from: vito on Feb 12, 2017, 04:59 AM
Quote from: KAKAN on Feb 12, 2017, 04:52 AMCouldn't understand.
People here usually using an array for player's data and when player disconnecting they removing this data. Unfortunately storing player's data when he is offline is too hard for most of server's developers.
For a normal VCMP server, this won't be needed since you're not going to have more than 1000 people. But for old, popular servers, this might be a necessity. For ex, LW: it has around 100k players registered.

Quote from: vito on Feb 12, 2017, 09:43 AMit seems only 'null' can't be a key. Everything else is fine.

Very strange for me.
I don't understand how that's strange.
oh no

jWeb

Quote from: KAKAN on Feb 12, 2017, 04:03 AMplayerson.push( player ) instead of ID, is better.

That approach is flawed because of http://forum.vc-mp.org/?topic=2997.0 This implementation was further discussed here http://vcmp.liberty-unleashed.co.uk/forum/index.php?topic=2419.0 but doesn't work because the developers failed to understand or know what one of the main questions in that topic meant "Is the Player instance ever changed by the server without notification? So that I can switch the key to the new instance.".



Quote from: ...happymint_ on Feb 12, 2017, 01:41 AMplayers <- {};

function onPlayerJoin( player )
{
players.rawset(player.Name,player);
}

function onPlayerPart( player, reason )
{
players.rawdelete(player.Name);
}

That also is flawed. Not in general. But only for VC:MP scripters. Because you know.

Why? Well, let's say that player 1 changes name from "looser" too "bigger looser" and player 2 changes name from "tiny looser" to "looser". Well then. A simple mistake in forgetting to update a table and your server logic is f*ed. And don't say you won't forget. Because you will. Somehow, somewhere.



And last but not least. Any answer given to you here is a total waste of time. Why? Because you don't understand the differences between tables and arrays. And because again, you're asking the wrong question. You shouldn't strive to understand which one is better. You should strive to understand how they work and then you'll inherently know which one is better for a certain situation.



Quote from: ...happymint_ on Feb 12, 2017, 01:41 AMplayerson <- [];

function onPlayerJoin( player )
{
playerson.push( player.ID );
}

function onPlayerPart( player, reason )
{
playerson.remove( player.ID );
}

This again proves that you don't know how they work. Because if you use push instead of direct indexing then you're going to have to perform a manual search to find a certain ID.

I'm not even sure wtf this question is about anymore. Storing numbers, storing strings or storing players.

vito

Quote from: KAKAN on Feb 12, 2017, 11:14 AMFor a normal VCMP server, this won't be needed since you're not going to have more than 1000 people. But for old, popular servers, this might be a necessity. For ex, LW: it has around 100k players registered.
There is only 40k total accounts in PAC, but tables can be used not only for cache.

KAKAN

Quote from: vito on Feb 12, 2017, 11:31 AM
Quote from: KAKAN on Feb 12, 2017, 11:14 AMFor a normal VCMP server, this won't be needed since you're not going to have more than 1000 people. But for old, popular servers, this might be a necessity. For ex, LW: it has around 100k players registered.
There is only 40k total accounts in PAC, but tables can be used not only for cache.
whatever, it was just for an example.
oh no