System Recycle

Reaction score
341
Recycle
v0.1.4

What is recycle?
Recycle keeps your handle count lower by re-using already created handles. Say you create two groups, then 'Recycle' them. Then somewhere else you want to create five more groups, your handle count will only increase 3 instead of 5 because it will re-use the two you made before.​

How do I use it?

Normally when you create a new handle you do something like CreateGroup(). Instead you should use GetGroup(). That way the system will give you an already created/un-used group for use. If no free groups exists a new one will be created. This is the same for all the handles in this system.

Another important thing is to never destroy the handle, instead use RecycleHandle (replace Handle with the type). Below is an example explaining a little more.​

JASS:
scope testt initializer onInit

    globals
        private group array g
    endglobals
    
    private function test takes nothing returns nothing
        set g[1] = GetGroup()
        set g[2] = GetGroup()
        call BJDebugMsg(I2S(GetHandleId(g[1]))) // Creates new handle
        call BJDebugMsg(I2S(GetHandleId(g[2]))) // Creates new handle
        call RecycleGroup(g[1])
        call RecycleGroup(g[2])
        set g[3] = GetGroup()
        set g[4] = GetGroup()
        call BJDebugMsg(I2S(GetHandleId(g[3]))) // Gets g[2]'s old handle
        call BJDebugMsg(I2S(GetHandleId(g[4]))) // Gets g[1]'s old handle
    endfunction
    
    private function onInit takes nothing returns nothing
        call TimerStart(GetTimer(), 0.00, false, function test)
    endfunction

endscope

Similar Systems?
Yes there are similar systems that recycle an individual handle type. But this system allows you to specify any handle type you would like. If you are just going to recycle groups then use GroupUtils, same goes for other systems that recycle a handle. But if you plan on recycling multiple handle-types this system should suite you.

The Code


JASS:
library Recycle

//! textmacro RecycleHandle takes HANDLE, NAME, CREATE, CLEAR

    globals
        private $HANDLE$ array $NAME$
        private integer $NAME$_COUNT = 0
    endglobals

    function Get$NAME$ takes nothing returns $HANDLE$
        if $NAME$_COUNT == 0 or $NAME$[$NAME$_COUNT-1 ] == null then
            debug if $NAME$[$NAME$_COUNT-1 ] == null then
            debug   call BJDebugMsg("Don't destroy recycled $NAME$'s!")
            debug endif
            return $CREATE$
        endif
        set $NAME$_COUNT = $NAME$_COUNT - 1
        if $NAME$_COUNT > 8191 then
            debug call BJDebugMsg("You have too many handles in your map, and recylcing still won't help you.")
            return null
        endif
        return $NAME$[$NAME$_COUNT]
        // returns the last free handle in the list.
    endfunction

    function Recycle$NAME$ takes $HANDLE$ h returns nothing
        $CLEAR$ // Cleans up the handle
        set $NAME$[$NAME$_COUNT] = h
        set $NAME$_COUNT = $NAME$_COUNT + 1
        // This function adds the recycled handle to the end of the list of free
        // handles, thus allowing it to be used again.
    endfunction

//! endtextmacro

    //! runtextmacro RecycleHandle("timer", "Timer", "CreateTimer()", "call PauseTimer(h)")
    //! runtextmacro RecycleHandle("group", "Group", "CreateGroup()", "call GroupClear(h)")
    //! runtextmacro RecycleHandle("force", "Force", "CreateForce()", "call ForceClear(h)")
    //! runtextmacro RecycleHandle("region", "Region", "CreateRegion()", "")
    //! runtextmacro RecycleHandle("texttag", "TextTag", "CreateTextTag()", "call SetTextTagText(h, \"\", 0)")
    //! runtextmacro RecycleHandle("dialog", "Dialog", "DialogCreate()", "call DialogClear(h)")


endlibrary
 

Jesus4Lyf

Good Idea™
Reaction score
397
Wtf?
JASS:
globals
        private hashtable HASH=InitHashtable()
    endglobals
    
//! textmacro RecycleHandle takes HANDLE, NAME, CREATE, CLEAR

    globals
        private $HANDLE$ array $NAME$
        private integer $NAME$_Count = -1
        private integer $NAME$_OPEN_COUNT = 0
        private integer array $NAME$_INDEX
    endglobals
    
    function Get$NAME$ takes nothing returns $HANDLE$
        if $NAME$_OPEN_COUNT == 0 then 
        // If there are no free handles
            set $NAME$_Count = $NAME$_Count+1
            set $NAME$[$NAME$_Count] = $CREATE$ 
            // Creates the handle
            call SaveInteger(HASH, GetHandleId($NAME$[$NAME$_Count]), 0, $NAME$_Count)
            // Saves the position of the handle.
            return $NAME$[$NAME$_Count]
        endif
        set $NAME$_OPEN_COUNT = $NAME$_OPEN_COUNT - 1
        // Removes the handle about to be returned from the list of free handles.
        return $NAME$[$NAME$_INDEX[$NAME$_OPEN_COUNT+1]]
        // returns the last free handle in the list.
    endfunction
    
    function Recycle$NAME$ takes $HANDLE$ h returns nothing
        $CLEAR$ // Cleans up the handle
        set $NAME$_OPEN_COUNT = $NAME$_OPEN_COUNT + 1
        set $NAME$_INDEX[$NAME$_OPEN_COUNT] = LoadInteger(HASH, GetHandleId(h), 0)
        // This function adds the recycled handle to the end of the list of free
        // handles, thus allowing it to be used again.
    endfunction

Why do people over complicate recycling?
JASS:
//globals
    //    private hashtable HASH=InitHashtable() // A hashtable?...
    //endglobals
    
//! textmacro RecycleHandle takes HANDLE, NAME, CREATE, CLEAR

    globals
        private $HANDLE$ array $NAME$
        private integer $NAME$_Count = 0
    endglobals
    
    function Get$NAME$ takes nothing returns $HANDLE$
        if $NAME$_COUNT == 0 then
            return $CREATE$
        endif
        set $NAME$_COUNT = $NAME$_COUNT - 1
        return $NAME$[$NAME$_COUNT]
        // returns the last free handle in the list.
    endfunction
    
    function Recycle$NAME$ takes $HANDLE$ h returns nothing
        $CLEAR$ // Cleans up the handle
        set $NAME$[$NAME$_COUNT] = h
        set $NAME$_COUNT = $NAME$_COUNT + 1
        // This function adds the recycled handle to the end of the list of free
        // handles, thus allowing it to be used again.
    endfunction
 

quraji

zap
Reaction score
144
This is kind of overcomplicated, just use a stack (I was too lazy to do the textmacro stuff):

JASS:
globals
    private integer stack_size = 0
    private group array stack
endglobals

function RecycleGroup takes group g returns nothing
    if (g!=null) then                  // don't store null groups!
        call GroupClear(g)             // clear it
        set stack[stack_size] = g      // add it to the stack
        set stack_size = stack_size+1  // increment the count
    endif
endfunction

function GetGroup takes nothing returns group
    if (stack_size==0) then // stack is empty, return a new group
        return CreateGroup()
    endif
    set stack_size = stack_size-1
    return stack[stack_size] // return the group on the top of the stack
endfunction


EDIT: Wow, I was late. That's what I get for getting distracted mid-post :p
 

Jesus4Lyf

Good Idea™
Reaction score
397
Lol, I used 1 dummy in whole map. :p
I do that too, but for attaching effects you need multiple.
What about TextTags, isn't there a limit of like 100?
Yessir, but those handle numbers are recycled when they time out or are destroyed. It's 100 at once, I'm rather sure... (Or whatever that number is.)

I don't see why you'd recycle the rest at all. I wouldn't even use forces in a map... Can just use an array of players... :)
 

Jesus4Lyf

Good Idea™
Reaction score
397
You can recycle lightning. :)
I probably wouldn't.

[DEL]This should have a few more things added (maybe):
JASS:
globals
    group GROUP=CreateGroup()
    location LOCATION=Location(0,0)
endglobals

They're just conventions I use for my own maps... But I use LOCATION for GetZ things, and for the GROUP, I do all enums with it. It seems to eliminate the usefulness of group recycling except for registering what units a spell has already hit. :)[/DEL]

Meh, never mind. <_< Mappers should do this themselves.
 

Viikuna

No Marlo no game.
Reaction score
265
It is 100 texttags per player max. ( Since you can create them locally ) Also, as far as I know, those hardcoded texttags like critical messages, or manaburn numbers are counted to that 100 too.

Texttags have their own handle id stack thingy, so they take those same handle ids no matter what you do. I dont really see any reason to recycle them.
 

Romek

Super Moderator
Reaction score
964
> Yes there are similar systems that recycle an individual handle type. But this system allows you to specify any handle type you would like
Other systems only recycle one time for a reason; why would you need to recycle any other type?

There's no need to recycle anything except timers and groups, I think.
Apparently there can be some issues with destroying them.
Though group and timer recycling has been made before, many times.

Why do you always try to reinvent what has been done before (+ some useless features always added).
And there's really no need to submit everything you make.

Say you create two groups, then 'Recycle' them. Then somewhere else you want to create five more groups, your handle count will only increase 3 instead of 5 because it will re-use the two you made before.
Are you forgetting that destroying handles lowers the handle count?
 

Jesus4Lyf

Good Idea™
Reaction score
397
JASS:
    function Get$NAME$ takes nothing returns $HANDLE$
        if $NAME$_COUNT == 0 or $NAME$[$NAME$_COUNT-1 ] == null then
            debug if $NAME$[$NAME$_COUNT-1 ] == null then
            debug   call BJDebugMsg(&quot;Don&#039;t destroy recycled $NAME$&#039;s!&quot;)
            debug endif
            return $CREATE$
        endif
        set $NAME$_COUNT = $NAME$_COUNT - 1
        if $NAME$_COUNT &gt; 8191 then
            debug call BJDebugMsg(&quot;You have too many handles in your map, and recylcing still won&#039;t help you.&quot;)
            return null
        endif
        return $NAME$[$NAME$_COUNT]
        // returns the last free handle in the list.
    endfunction

  1. Either put it in a debug clause, or get rid of: or $NAME$[$NAME$_COUNT-1 ] == null
  2. This is the wrong time to check it, and put it in a debug clause in the Recycle function: if $NAME$_COUNT > 8191 then
 

Jesus4Lyf

Good Idea™
Reaction score
397
Don't have double free protection?
How do you suggest it be implemented?

Either O(n) complexity or attaching to things.

Personal opinion:
Attaching would make this fail for 1.23, and I don't think it worth it (excuse that I bring this up, I'm currently working on 1.23 map). Mappers don't get protection against destroying something twice. >_<

But, this is a valid concern for the author.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top