• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS: Smart pointers discussion

mrianuraa

Member
Joined
Dec 5, 2019
Messages
55
Reaction score
21
Hello,

I'm analyzing tfs for some time and my main feeling is: It is quite complicated to avoid racing conditions while developing custom systems or even in simplier situations for example: When monster dies, there is a delay between "onDeatch" function call. Now you have to perform deep architecture analyze if onDeatch can be called immediately when hp <= 0 detected.
What do You think about using more smart pointers?
One Tfs maintainer proposed intrusive_ptr from boost, but I'm not really convinced, that it would be safier/easier than using current "intrusive ptr" implementation.
What about performing deep analyze and convert as much dynamic pointers to unique_ptr and use more shared_ptrs ?

Do You think, that shared_ptr will highly decrease performance or will make huge ram footprint ?

Regards,
Mariusz
 
Last edited:
Using current "intrusive ptr" implementation is quite poor and do not follow the rule of self limiting like for example in the unique ptr, that you can not copy, but you are allowed only to move content. The main question is intrusive_ptr vs shared_ptr - If such change would lead to bad overall tfs performance and ram usage. Im analyzing onDeath thread safety only here.
 
The main question is intrusive_ptr vs shared_ptr - If such change would lead to bad overall tfs performance and ram usage.

It won't, but certainly would help to get rid of issues with lifetime of objects. The current approach is impossible to track as you don't know where object was held and where its reference was increased. I was actually tired myself to track issues related to lifetime of objects, so at the time I wrote the following response to that intrusive pointer discussion:


It was over 3 years ago and by that time I already rewrote Medivia game server (which is not based on TFS, but an old OTServ distribution from before TFS was even forked) to use only shared and weak pointers. It does save a lot of issues while tracking possible bugs and the performance or memory footprint is rather small to not even worry about due to all the benefits.

Anyway, keep in mind that you can save most of possible minor performance degradation by simply passing shared pointer as const reference, so it doesn't copy-construct itself in trivial cases.

On a side note, a huge advantage of a weak pointer over a raw pointer is the ability to determine whether the object died before accessing it since it simply won't lock, so you won't be able to acquire shared_ptr out of weak_ptr.
 
Thank you for such a good news, i was using Smart pointes in work mamy times and im quite fluent in using them, but i hadnt time to do good performance test. I will try to replce current obsolete raw pointers.
 
On such game server structure when we have multiple objects being created and destroyed (even while moving gold, we create and delete items), isn't best to just use an object pool for items so we stop caring about pointers? This is the approach game servers use, and also CipSoft is doing it like this for their game server.
 
Back
Top