Performance question

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

Moderators: Lapo, Bax

TAKARA
Posts: 2
Joined: 12 Jun 2021, 13:21

Performance question

Postby TAKARA » 12 Jun 2021, 14:31

I have user item data stored in a database as a comma delimited string. Currently it gets the data when logging in and stores it to a UtfString uservar, which for example looks like "4,3,3,4". The string can be up to 400 items long. I'm using Java 9, so each string character is a byte long and shouldn't take up too much memory while they're logged in.

The user doesn't need to receive the full item data string very often. When the user requests an item change, the server takes the string, convert it to an List<Integer> and does work, and then converts it back to a string and saves the variable. I was doing this so that a string is only sitting in memory instead of a List<Integer>, since GC will clean up all temp Lists.

However, I am wondering if I should just store the List<Integer> using .setProperty when the user logs in and then use .getProperty on a new List to perform changes to prevent converting back and forth between String and List.

What I'm doing now:

Code: Select all

String inventory = user.getVariable("i").getStringValue();
items = Stream.of(inventory.split(","))
              .map(String::trim)
              .map(Integer::parseInt)
              .collect(Collectors.toList());
….

// After modification, resave
String data = inventoryData.stream().map( s -> s.toString()).collect( Collectors.joining( "," ) );

UserVariable inventory = new SFSUserVariable(“i”, data);


What I may change to:

Code: Select all

 List<Integer> items = (List<Integer>) user.getProperty("i");

….

// After modification, resave
user.setProperty("i", items);


Which approach seems best ? Is converting between String worth it to save memory vs CPU? Any input is good.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Performance question

Postby Lapo » 13 Jun 2021, 11:02

HI,
no I don't think it is worth it. Because the conversion itself will generate a significant amount of garbage in memory.
Storing the List as a User's property seems the better way to go.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
TAKARA
Posts: 2
Joined: 12 Jun 2021, 13:21

Re: Performance question

Postby TAKARA » 13 Jun 2021, 19:55

Thank you for the advice. The other problem I wondered if you had a solution to was how to avoid the "unchecked cast" error that comes from using getProperty().

I could simply use a @SuppressWarnings("unchecked") to ignore the warning since I know it will always be a List<Integer>, since for me List<?> does not work in my code flow. I know this isn't directly related to SFS, but please let me know if you know a better / safer way. I have been looking around the forums but can't find anything.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Performance question

Postby Lapo » 14 Jun 2021, 07:38

Hi,
the unchecked cast is just a warning so you can ignore it or, as you said, use the compiler directive to avoid the warning in your compilation report.

I am not aware of other ways to remove it.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 36 guests