vgstation-coders / vgstation13

Butts
GNU General Public License v3.0
260 stars 542 forks source link

space turf initialization is slow #36631

Open SECBATON-GRIFFON opened 1 month ago

SECBATON-GRIFFON commented 1 month ago

[performance]

Description of issue

it takes up approximately 2.9 of the 10 or so seconds of initialization time in the objects subsystem

Specific information for locating

the main bulk of this seems to come from assigning the appearance to the turf in initialize(), the line itself is about 1.5 seconds of this, where as the x + y xor'd with not x * y plus z modulo 26 line takes up another 1.5 seconds don't really know a good way to cut this down while keeping it the same so leaving the info here for others

Length of time in which bug has been known to occur

since it was added?

Server revision

fabe5b914fd6d316af6cda73131caded3b7a0978

toomykins commented 1 month ago

dunno how you tested this but a small amount of optimizations could be made off the bat

original code

/turf/space/initialize()
    if(loc)
        var/area/A = loc
        A.area_turfs += src
    if(!parallax_appearances)
        parallax_appearances = list()
        for(var/i in 0 to 25)
            var/I = "[i]"
            icon_state = I
            var/image/parallax_overlay = image('icons/turf/space_parallax1.dmi', I)
            parallax_overlay.plane = SPACE_DUST_PLANE
            parallax_overlay.alpha = 80
            parallax_overlay.blend_mode = BLEND_ADD
            overlays += parallax_overlay
            parallax_appearances[I] = appearance
            overlays.Cut()
    appearance = parallax_appearances["[((x + y) ^ ~(x * y) + z) % 26]"]

could probs be turned into something like this

    if (loc)
        var/area/A = loc
        A.area_turfs += src
    if (!parallax_appearances)
        parallax_appearances = list()
        var/image/parallax_overlay
        var/icon_state
        var/appearance

        for (var/i in 0 to 25)
            icon_state = "[i]"
            parallax_overlay = image('icons/turf/space_parallax1.dmi', icon_state)
            parallax_overlay.plane = SPACE_DUST_PLANE
            parallax_overlay.alpha = 80
            parallax_overlay.blend_mode = BLEND_ADD
            overlays += parallax_overlay
            parallax_appearances[icon_state] = parallax_overlay.appearance

        overlays.Cut()

    var/appearance_key = "[((x + y) ^ ~(x * y) + z) % 26]"
    appearance = parallax_appearances[appearance_key]

or even find a simpler way to do the modulus

SECBATON-GRIFFON commented 1 month ago

tried that, didn't make much of a difference, that's why i said the appearance equals line is the crux of it all. also the modulus seems to be a PRNG thats only a little faster than rand()