• 🏆 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!

GetPlayerIdFast

Status
Not open for further replies.
Level 13
Joined
Nov 7, 2014
Messages
571
It seems that the type player extends agent // a single player reference variables's handle id (which you get from GetHandleId(...)) get cached when they are first created, which seems to allow one to make a faster GetPlayerId function (according to my fps benchmark at least) =).

JASS:
library GetPlayerIdFast initializer init

globals
    // These need to be the first handle variables that are going to get initialized in the globals block of war3map.j
    // otherwise the offset (0x00100008) is going to be wrong!
    //
    // The offset (0x00100008) would have to be updated if Blizzard makes changes to common.j or blizzard.j
    // that involve initialization of handle variables in the globals block
    //
    constant player player_0 = Player(0)
    constant player player_1 = Player(1)
    constant player player_2 = Player(2)
    constant player player_3 = Player(3)
    constant player player_4 = Player(4)
    constant player player_5 = Player(5)
    constant player player_6 = Player(6)
    constant player player_7 = Player(7)
    constant player player_8 = Player(8)
    constant player player_9 = Player(9)
    constant player player_10 = Player(10)
    constant player player_11 = Player(11)
    constant player player_12 = Player(12)
    constant player player_13 = Player(13)
    constant player player_14 = Player(14)
    constant player player_15 = Player(15)
endglobals

// NOTE:
//    GetPlayerIdFast is only faster if it get's inlined (will not get inlined in DEBUG_MODE)
//    and if we try to use a variable:
//        return GetHandleId(p) - player_0_handle_id
//    it will become slower than GetPlayerId, i.e it must use a constant offset (0x00100008)
//
function GetPlayerIdFast takes player p returns integer
    return GetHandleId(p) - 0x00100008
endfunction

// NOTE: do NOT try to use hook because it doesn't seem to replace GetPlayerId with the inlined version of GetPlayerIdFast!
//
// hook GetPlayerId GetPlayerIdFast

function GetTriggerPlayerId takes nothing returns integer
    return GetHandleId(GetTriggerPlayer()) - 0x00100008
endfunction

private function init takes nothing returns nothing
static if DEBUG_MODE then
    if GetHandleId(player_0) - 0x00100008 != 0 then
        call BJDebugMsg("|cffFF0000[GetPlayerIdFast] error: incorrect initialization|r")
    endif
endif
endfunction

endlibrary
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
I assume a global variable is still faster.
Especially when there is no difference between player id and player.
Aka, good use of structs.

Also, isnt it better to save the offset at the init?
Aka
JASS:
set offset = GetHandleId(player_0)
and
JASS:
return GetHandleId(p) - offset
 

Deleted member 219079

D

Deleted member 219079

Global variable lookup would impact performance.
 

~El

~El

Level 17
Joined
Jun 13, 2016
Messages
558
Is there even a tangible performance benefit to using this method, rather than just the default GetPlayerId?

I applaud your effort, this is a genuinely nice idea, but I can't help but think that going this extra mile (not to mention potentially breaking compatibility with previous/further patches) is not really worth it. We need some actual benchmarks here :v
 
Status
Not open for further replies.
Top