Performance question with user vars

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Performance question with user vars

Postby moccha » 01 Sep 2021, 20:25

I am at a point where I have to decide between using many multiple user vars or one user var (sfsobject) with several values packed in. I need to read from the data regularly, but update the data less frequently.

The data will always be sent to the client in one update, so there's no worry of wasted bandwidth.

Is it better for server performance / memory to store all the variables and access them like this:

Code: Select all

// Init stat data on zone entry
SFSObject statData = new SFSObject();
statData.putInt("level", userLevel);
statData.putInt("magic", userMagic);
... Multiple more data values
UserVariable stats = new SFSUserVariable("stats", statData);

// Battle loop
// Accessing data during battle
if ( user.getVariable("s").getSFSObjectValue().getInt("magic") > 50 )
{
    ... Use spell
}

// Update data at end of battle
SFSObject stats = user.getVariable("stats").getSFSObjectValue();
int newLevel = 10;
stats.putInt("level", newLevel); // Update level
UserVariable newStats = new SFSUserVariable("stats", sfsoObject);
user.setVariable(newStats);


Or is it better to use multiple separate UserVariables like this:

Code: Select all

// Init stat data on zone entry
UserVariable level = new SFSUserVariable("level", 9);
UserVariable magic = new SFSUserVariable("magic", 50);
... Multiple more data values

// Battle loop
// Accessing data during battle
if ( user.getVariable("magic").getIntValue() > 50 )
{
    ... Use spell
}

// Update data at end of battle
int newLevel = 10;
UserVariable level = new SFSUserVariable("level", newLevel);
user.setVariable(level);


I am less familiar with how Java will handle each case and what goes on behind the curtains.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Performance question with user vars

Postby Lapo » 02 Sep 2021, 07:30

Hi,
packing the data in one variable will slightly improve performance. Nothing major though. Both approaches are valid.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Re: Performance question with user vars

Postby moccha » 02 Sep 2021, 15:10

Okay, thank you for the tip. I wasn't sure if the extra SFSObject creation and calling an extra function when reading it would have a bad impact on memory usage. I'll continue on as planned.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 64 guests