Skip to content

Commit

Permalink
Optimize memory usage by eliminating/changing some lists
Browse files Browse the repository at this point in the history
Ported from TG: Object armors are no longer defined in (unique) lists but rather datums that can be cached depending on their armor values.
Add LAZYSET define to lazily initialize a list then assigning a key to a value
Add alldirs2 global which is the same as alldirs except diagonals go first
Optimize mob memory by making alerts list lazy
Optimize obj/machinery memory by making use_log and settagwhitelist lists lazy
Optimize atom memory by not creating hud_list list for all atoms
Optimize turf memory by not creating footstep_sounds list for all turfs
Clean up code where possible
  • Loading branch information
dearmochi committed Jun 21, 2020
1 parent ebeee92 commit 65d0eca
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 218 deletions.
2 changes: 2 additions & 0 deletions code/__HELPERS/lists.dm
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ proc/dd_sortedObjectList(list/incoming)
// LAZYING PT 2: THE LAZENING
#define LAZYREINITLIST(L) LAZYCLEARLIST(L); LAZYINITLIST(L);

// Lazying Episode 3
#define LAZYSET(L, K, V) LAZYINITLIST(L); L[K] = V;

//same, but returns nothing and acts on list in place
/proc/shuffle_inplace(list/L)
Expand Down
3 changes: 2 additions & 1 deletion code/_globalvars/mapping.dm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#define Z_SOUTH 3
#define Z_WEST 4

GLOBAL_LIST_INIT(cardinal, list( NORTH, SOUTH, EAST, WEST ))
GLOBAL_LIST_INIT(cardinal, list(NORTH, SOUTH, EAST, WEST))
GLOBAL_LIST_INIT(alldirs, list(NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST))
GLOBAL_LIST_INIT(alldirs2, list(NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST, NORTH, SOUTH, EAST, WEST))
GLOBAL_LIST_INIT(diagonals, list(NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST))

// This must exist early on or shit breaks bad
Expand Down
21 changes: 11 additions & 10 deletions code/_onclick/hud/alert.dm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
if(!category)
return

var/obj/screen/alert/alert
if(alerts[category])
alert = alerts[category]
var/obj/screen/alert/alert = LAZYACCESS(alerts, category)
if(alert)
if(alert.override_alerts)
return 0
if(new_master && new_master != alert.master)
Expand Down Expand Up @@ -57,7 +56,7 @@
alert.icon_state = "[initial(alert.icon_state)][severity]"
alert.severity = severity

alerts[category] = alert
LAZYSET(alerts, category, alert) // This also creates the list if it doesn't exist
if(client && hud_used)
hud_used.reorganize_alerts()
alert.transform = matrix(32, 6, MATRIX_TRANSLATE)
Expand All @@ -72,7 +71,7 @@

// Proc to clear an existing alert.
/mob/proc/clear_alert(category, clear_override = FALSE)
var/obj/screen/alert/alert = alerts[category]
var/obj/screen/alert/alert = LAZYACCESS(alerts, category)
if(!alert)
return 0
if(alert.override_alerts && !clear_override)
Expand Down Expand Up @@ -585,12 +584,14 @@ so as to remain in compliance with the most up-to-date laws."
// Re-render all alerts - also called in /datum/hud/show_hud() because it's needed there
/datum/hud/proc/reorganize_alerts()
var/list/alerts = mymob.alerts
if(!alerts)
return FALSE
var/icon_pref
if(!hud_shown)
for(var/i = 1, i <= alerts.len, i++)
for(var/i in 1 to alerts.len)
mymob.client.screen -= alerts[alerts[i]]
return 1
for(var/i = 1, i <= alerts.len, i++)
return TRUE
for(var/i in 1 to alerts.len)
var/obj/screen/alert/alert = alerts[alerts[i]]
if(alert.icon_state == "template")
if(!icon_pref)
Expand All @@ -611,10 +612,10 @@ so as to remain in compliance with the most up-to-date laws."
. = ""
alert.screen_loc = .
mymob.client.screen |= alert
return 1
return TRUE

/mob
var/list/alerts = list() // contains /obj/screen/alert only // On /mob so clientless mobs will throw alerts properly
var/list/alerts // lazy list. contains /obj/screen/alert only // On /mob so clientless mobs will throw alerts properly

/obj/screen/alert/Click(location, control, params)
if(!usr || !usr.client)
Expand Down
66 changes: 66 additions & 0 deletions code/datums/armor.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#define ARMORID "armor-[melee]-[bullet]-[laser]-[energy]-[bomb]-[bio]-[rad]-[fire]-[acid]"

/proc/getArmor(melee = 0, bullet = 0, laser = 0, energy = 0, bomb = 0, bio = 0, rad = 0, fire = 0, acid = 0)
. = locate(ARMORID)
if (!.)
. = new /datum/armor(melee, bullet, laser, energy, bomb, bio, rad, fire, acid)

/datum/armor
var/melee
var/bullet
var/laser
var/energy
var/bomb
var/bio
var/rad
var/fire
var/acid

/datum/armor/New(melee = 0, bullet = 0, laser = 0, energy = 0, bomb = 0, bio = 0, rad = 0, fire = 0, acid = 0)
src.melee = melee
src.bullet = bullet
src.laser = laser
src.energy = energy
src.bomb = bomb
src.bio = bio
src.rad = rad
src.fire = fire
src.acid = acid
tag = ARMORID

/datum/armor/proc/modifyRating(melee = 0, bullet = 0, laser = 0, energy = 0, bomb = 0, bio = 0, rad = 0, fire = 0, acid = 0)
return getArmor(src.melee + melee, src.bullet + bullet, src.laser + laser, src.energy + energy, src.bomb + bomb, src.bio + bio, src.rad + rad, src.fire + fire, src.acid + acid)

/datum/armor/proc/modifyAllRatings(modifier = 0)
return getArmor(melee + modifier, bullet + modifier, laser + modifier, energy + modifier, bomb + modifier, bio + modifier, rad + modifier, fire + modifier, acid + modifier)

/datum/armor/proc/setRating(melee, bullet, laser, energy, bomb, bio, rad, fire, acid)
return getArmor((isnull(melee) ? src.melee : melee),\
(isnull(bullet) ? src.bullet : bullet),\
(isnull(laser) ? src.laser : laser),\
(isnull(energy) ? src.energy : energy),\
(isnull(bomb) ? src.bomb : bomb),\
(isnull(bio) ? src.bio : bio),\
(isnull(rad) ? src.rad : rad),\
(isnull(fire) ? src.fire : fire),\
(isnull(acid) ? src.acid : acid))

/datum/armor/proc/getRating(rating)
return vars[rating]

/datum/armor/proc/getList()
return list("melee" = melee, "bullet" = bullet, "laser" = laser, "energy" = energy, "bomb" = bomb, "bio" = bio, "rad" = rad, "fire" = fire, "acid" = acid)

/datum/armor/proc/attachArmor(datum/armor/AA)
return getArmor(melee + AA.melee, bullet + AA.bullet, laser + AA.laser, energy + AA.energy, bomb + AA.bomb, bio + AA.bio, rad + AA.rad, fire + AA.fire, acid + AA.acid)

/datum/armor/proc/detachArmor(datum/armor/AA)
return getArmor(melee - AA.melee, bullet - AA.bullet, laser - AA.laser, energy - AA.energy, bomb - AA.bomb, bio - AA.bio, rad - AA.rad, fire - AA.fire, acid - AA.acid)

/datum/armor/vv_edit_var(var_name, var_value)
if (var_name == NAMEOF(src, tag))
return FALSE
. = ..()
tag = ARMORID // update tag in case armor values were edited

#undef ARMORID
Loading

0 comments on commit 65d0eca

Please sign in to comment.