• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

question about timers/hastables

Status
Not open for further replies.
Level 3
Joined
Nov 8, 2009
Messages
39
Basically, I was wondering if there is any reason to flush the children of a timer handle (after the handler function finishes) when using a timer recycler stack. It seems like it's just extra overhead, right?

Somewhat relatedly, would it also hold true that ideally you only want to flush the children of a given unit handle once (when it leaves the map)?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
When you recycle objects, you would normally expect them to be fresh. The stored information does not magically disappear from the hashtable and is not connected to the handles but to their ids. So if there is a new object with the id of an old, destroyed one, which is especially the case when you recycle stuff, it will inherit the old data. And otherwise, if you keep fetching new unique ids to avoid collision, this will pose memory leaks.

It is another story for the case that the objects are statically programmed, will therefore never release its id and may not have to change their assigned values.
 
Level 3
Joined
Nov 8, 2009
Messages
39
Cool, that seems to confirm what I was thinking, but let me run through it again just to make sure.

My understanding is that when you pull the timer out of the recycler, it will have the same id ( i.e. GetHandleId(t) ) as the last time that particular timer was used (unless there are no timers left in the recycler stack, in which case it would be a fresh id). This is why I was wondering if it's worth it to flush the children of the timer id in the handler hashtable (just simple usage for passing 1 or 2 values). Since you're mostly reusing the same range of timer ids over and over again, it seems like it should be fine to never flush them, assuming you're not worried about collision.

Would this sort of usage create memory leaks?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
If you do not have to read their data before setting them again, the flushing can be omitted. Theoretically, it does not create memory leaks, as the same timers with their hashtable entries can be reached over and over again but depending on need, you may have reserved such an amount of timers before you would never require again. Not leaking does not mean that it does not draw its resources.

I do not know your recycler but normally you would wrap it into an own type and having it grant an array-able id via library. Then you would not need to use hashtable within your execution scope. Moreover, practically, you only assign the struct instance to the timer it belongs to. This can be provided via the library as well.
 

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
If you overwrite all the data inside a given ID on the table properly, there is no need to flush it first.

However, it's a very bad programming practice, as it is prone to cause bugs.
It's basicly the same as not initiating a variable. In most programming languages, you do not need to initialize a global variable. If you assign a value to it before retrieving, this is totally fine, but if you for some reason (possible cases you haven't thought about when programming, like the user being dumb and inserting invalid data) do not assign a new value, you have access to the old inherited data.

Not only does flushing improve the internal structure and readability of your code (if someone else or your future-self in two years reads the code, he might be confused about the fact, as it implies that those variables are still used somewhere else), it also avoids bugs with table reads of not initialized data.
 
Level 3
Joined
Nov 8, 2009
Messages
39
Not leaking does not mean that it does not draw its resources.

By this do you mean that while technically not a leak, it still might be occupying memory, drawing CPU cycles, or just making that particular hashtable slower in general? If so, does this sort of thing (when taking place on a modest scale) have a noticeable impact on the performance of modern PCs?

@Zwiebelchen

Hmm, that's interesting to note. It just seems inefficient to me to be flushing the hashtable children of the timer id every single time they expire, especially when there's lots of timers firing within a short span. Maybe I should pass that data with an array[convertedHandleId] instead? Or, is the performance impact of the hashtable negligible anyways?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
well yea, because data in hashtable takes a place in your RAM, not flushing the data will make them stay at your RAM, and if you never come to the same HandleId ever again, it is permament leak(unless you flush the whole hashtable)

I would just flush the child hashtable only when Im done with the timer(right before you call DestroyTimer(..) or when you do something like ReleaseTimer(..) in case you are using some recycler for timers such as TimerUtils by Vex or Mag)

Also keep in mind that the more items are kept in hashtable
 
Status
Not open for further replies.
Top