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
963
> 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.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top