Skip to content

Commit

Permalink
Saves more init time + fixes a minor SSshuttle bug (#445)
Browse files Browse the repository at this point in the history
* Micro-optimize GetIdFromArguments to be 48% faster, gaining 0.48s of init time on local (likely more in prod) (#69659)


About The Pull Request

    Avoids stringifying key unless its necessary. This was done redundantly twice, but I locked it to just the isnum path, as REF will always return a string, and the other path passes istext.
    Use sortTim directly instead of sort_list. sort_list is just sortTim but it copies the list, so it's just wasted cost.

I still would like the bespoke element key option, as that's the only way to drastically cut down costs on things like item descriptions and decals, but this is good for the general use case, and makes it marginally less pressing.

I also want to test if we'd be better off inserting into the list in sorted order rather than sorting it all in the end, but I suspect not.

* Load circuit components from USB ports on demand, saving 0.5s of init time (0.7s on prod) (#69664)

We create 2,383 circuit components (on whatever map I was looking at on Sybil at the time, don't know) from USB ports every round, quite pricey. This makes them initialize once when a USB is first plugged in.

* speed

* big ports

* this has been annoying me

* Add defines for byond-tracy support (#70931)

Adds `USE_BYOND_TRACY`, which automatically loads a given
prof.dll/libprof.so using https://github.com/mafemergency/byond-tracy/.

Not intended for use in production, and we do not ship a copy of
byond-tracy. It is extremely easy to compile yourself, but if you're
someone interesting enough to play around with this then let me know and
I can just give you a build.

I'm going to be using this for init profiling.

* optimize icon smoothing

* cleanup windows and fix compile

* fix it for real

* fix alot of bugs

* fix mineral turrfs

* debug time

* remove debug code

---------

Co-authored-by: Mothblocks <[email protected]>
  • Loading branch information
Kapu1178 and Mothblocks authored Sep 2, 2023
1 parent d9fb581 commit 5dc2711
Show file tree
Hide file tree
Showing 74 changed files with 323 additions and 276 deletions.
5 changes: 4 additions & 1 deletion code/__DEFINES/dcs/flags.dm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#define ELEMENT_INCOMPATIBLE 1

// /datum/element flags
/// Causes the detach proc to be called when the host object is being deleted
/// Causes the detach proc to be called when the host object is being deleted.
/// Should only be used if you need to perform cleanup not related to the host object.
/// You do not need this if you are only unregistering signals, for instance.
/// You would need it if you are doing something like removing the target from a processing list.
#define ELEMENT_DETACH (1 << 0)
/**
* Only elements created with the same arguments given after `id_arg_index` share an element instance
Expand Down
8 changes: 8 additions & 0 deletions code/__DEFINES/mapswitch.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Uses the left operator when compiling, uses the right operator when not compiling.
// Currently uses the CBT macro, but if http://www.byond.com/forum/post/2831057 is ever added,
// or if map tools ever agree on a standard, this should switch to use that.
#ifdef CBT
#define MAP_SWITCH(compile_time, map_time) ##compile_time
#else
#define MAP_SWITCH(compile_time, map_time) ##map_time
#endif
1 change: 1 addition & 0 deletions code/__DEFINES/matrices.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define TRANSLATE_MATRIX(offset_x, offset_y) matrix(1, 0, (offset_x), 0, 1, (offset_y))
87 changes: 45 additions & 42 deletions code/__HELPERS/icon_smoothing.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@
#define DEFAULT_UNDERLAY_ICON 'icons/turf/floors.dmi'
#define DEFAULT_UNDERLAY_ICON_STATE "plating"


/**
* Checks if `src` can smooth with `target`, based on the [/area/var/area_limited_icon_smoothing] variable of their areas.
*
* * If `target` doesn't have an area (E.g. the edge of the z level), return `FALSE`.
* * If one area has `area_limited_icon_smoothing` set, and the other area's type doesn't match it, return `FALSE`.
* * Else, return `TRUE`.
*
* Arguments:
* * target - The atom we're trying to smooth with.
*/
/atom/proc/can_area_smooth(atom/target)
var/area/target_area = get_area(target)
var/area/source_area = get_area(src)
if(!target_area)
return FALSE
if(target_area.area_limited_icon_smoothing && !istype(source_area, target_area.area_limited_icon_smoothing))
return FALSE
if(source_area.area_limited_icon_smoothing && !istype(target_area, source_area.area_limited_icon_smoothing))
return FALSE
return TRUE
/// Test if thing (an atom) can smooth with an adjacent turf. This is a macro because it is a very very hot proc.
#define CAN_AREAS_SMOOTH(thing, turf, val) \
do{ \
if(isnull(turf)) { \
break; \
}; \
var/area/source_area = get_step(thing, 0)?.loc; \
var/area/target_area = turf:loc; \
if(isnull(target_area)) { \
break; \
};\
if(target_area.area_limited_icon_smoothing && !istype(source_area, target_area.area_limited_icon_smoothing)) { \
break; \
}; \
if(source_area.area_limited_icon_smoothing && !istype(target_area, source_area.area_limited_icon_smoothing)) { \
break; \
}; \
val = TRUE; \
}while(FALSE)

///Scans all adjacent turfs to find targets to smooth with.
/atom/proc/calculate_adjacencies()
Expand Down Expand Up @@ -210,7 +208,9 @@
if(!target_turf)
return NULLTURF_BORDER

if(!can_area_smooth(target_turf))
var/can_area_smooth
CAN_AREAS_SMOOTH(src, target_turf, can_area_smooth)
if(isnull(can_area_smooth))
return NO_ADJ_FOUND

if(isnull(canSmoothWith)) //special case in which it will only smooth with itself
Expand Down Expand Up @@ -255,36 +255,39 @@
set_adj_in_dir: { \
do { \
var/turf/neighbor = get_step(src, direction); \
if(neighbor && can_area_smooth(neighbor)) { \
var/neighbor_smoothing_groups = neighbor.smoothing_groups; \
if(neighbor_smoothing_groups) { \
for(var/target in canSmoothWith) { \
if(canSmoothWith[target] & neighbor_smoothing_groups[target]) { \
new_junction |= direction_flag; \
break set_adj_in_dir; \
}; \
}; \
}; \
if(smooth_obj) { \
for(var/atom/movable/thing as anything in neighbor) { \
var/thing_smoothing_groups = thing.smoothing_groups; \
if(!thing.anchored || isnull(thing_smoothing_groups)) { \
continue; \
}; \
var/can_area_smooth; \
CAN_AREAS_SMOOTH(src, neighbor, can_area_smooth); \
if(neighbor && can_area_smooth) { \
var/neighbor_smoothing_groups = neighbor.smoothing_groups; \
if(neighbor_smoothing_groups) { \
for(var/target in canSmoothWith) { \
if(canSmoothWith[target] & thing_smoothing_groups[target]) { \
if(canSmoothWith[target] & neighbor_smoothing_groups[target]) { \
new_junction |= direction_flag; \
break set_adj_in_dir; \
}; \
}; \
}; \
if(smooth_obj) { \
for(var/atom/movable/thing as anything in neighbor) { \
var/thing_smoothing_groups = thing.smoothing_groups; \
if(!thing.anchored || isnull(thing_smoothing_groups)) { \
continue; \
}; \
for(var/target in canSmoothWith) { \
if(canSmoothWith[target] & thing_smoothing_groups[target]) { \
new_junction |= direction_flag; \
break set_adj_in_dir; \
}; \
}; \
}; \
}; \
} else if (smooth_border) { \
new_junction |= direction_flag; \
}; \
} else if (smooth_border) { \
new_junction |= direction_flag; \
}; \
} while(FALSE) \
}


for(var/direction in GLOB.cardinals) //Cardinal case first.
SET_ADJ_IN_DIR(direction, direction)

Expand Down
28 changes: 17 additions & 11 deletions code/controllers/subsystem/dcs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,28 @@ PROCESSING_SUBSYSTEM_DEF(dcs)
var/datum/element/eletype = arguments[1]
var/list/fullid = list("[eletype]")
var/list/named_arguments = list()

for(var/i in initial(eletype.id_arg_index) to length(arguments))
var/key = arguments[i]
var/value

if(istext(key))
value = arguments[key]
if(!(istext(key) || isnum(key)))
key = REF(key)
key = "[key]" // Key is stringified so numbers dont break things
if(!isnull(value))
if(!(istext(value) || isnum(value)))
value = REF(value)
named_arguments["[key]"] = value
else
var/value = arguments[key]
if (isnull(value))
fullid += key
else
if (!istext(value) && !isnum(value))
value = REF(value)
named_arguments[key] = value

continue

if (isnum(key))
fullid += "[key]"
else
fullid += REF(key)

if(length(named_arguments))
named_arguments = sort_list(named_arguments)
named_arguments = sortTim(named_arguments, /proc/cmp_text_asc)
fullid += named_arguments

return list2params(fullid)
2 changes: 1 addition & 1 deletion code/controllers/subsystem/shuttle.dm
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ SUBSYSTEM_DEF(shuttle)
if(!supply)
log_mapping("No /obj/docking_port/mobile/supply placed on the map!")

if(CONFIG_GET(flag/arrivals_shuttle_require_undocked))
if(CONFIG_GET(flag/arrivals_shuttle_require_undocked) && arrivals)
arrivals.Launch(TRUE)

return ..()
Expand Down
4 changes: 2 additions & 2 deletions code/datums/components/twohanded.dm
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
if(SEND_SIGNAL(parent, COMSIG_TWOHANDED_WIELD, user) & COMPONENT_TWOHANDED_BLOCK_WIELD)
return // blocked wield from item
wielded = TRUE
ADD_TRAIT(parent,TRAIT_WIELDED,src)
ADD_TRAIT(parent,TRAIT_WIELDED, REF(src))
RegisterSignal(user, COMSIG_MOB_SWAP_HANDS, PROC_REF(on_swap_hands))

// update item stats and name
Expand Down Expand Up @@ -206,7 +206,7 @@
wielded = FALSE
UnregisterSignal(user, COMSIG_MOB_SWAP_HANDS)
SEND_SIGNAL(parent, COMSIG_TWOHANDED_UNWIELD, user)
REMOVE_TRAIT(parent,TRAIT_WIELDED,src)
REMOVE_TRAIT(parent,TRAIT_WIELDED, REF(src))

// update item stats
var/obj/item/parent_item = parent
Expand Down
5 changes: 4 additions & 1 deletion code/datums/components/usb_port.dm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

circuit_components = list()

set_circuit_components(circuit_component_types)
src.circuit_component_types = circuit_component_types

/datum/component/usb_port/proc/set_circuit_components(list/components)
var/should_register = FALSE
Expand Down Expand Up @@ -136,6 +136,9 @@
/datum/component/usb_port/proc/on_atom_usb_cable_try_attach(datum/source, obj/item/usb_cable/connecting_cable, mob/user)
SIGNAL_HANDLER

if (!length(circuit_components))
set_circuit_components(circuit_component_types)

var/atom/atom_parent = parent

if (!isnull(attached_circuit))
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/ELEMENT_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ See _element.dm for detailed explanations

```dm
/datum/element/myelement
element_flags = ELEMENT_BESPOKE | ELEMENT_COMPLEX_DETACH | ELEMENT_DETACH | ELEMENT_NOTAREALFLAG // code/__DEFINES/dcs/flags.dm
element_flags = ELEMENT_BESPOKE | ELEMENT_COMPLEX_DETACH | ELEMENT_DETACH_ON_HOST_DESTROY | ELEMENT_NOTAREALFLAG // code/__DEFINES/dcs/flags.dm
//id_arg_index = 2 // Use with ELEMENT_BESPOKE
var/list/myvar = list()
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/art.dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/datum/element/art
element_flags = ELEMENT_BESPOKE|ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH // Detach for turfs
id_arg_index = 2
var/impressiveness = 0

Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/bane.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Deals extra damage to mobs of a certain type or species.
/datum/element/bane
element_flags = ELEMENT_DETACH|ELEMENT_BESPOKE
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
/// can be a mob or a species.
var/target_type
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/bed_tucking.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Tucking element, for things that can be tucked into bed.
/datum/element/bed_tuckable
element_flags = ELEMENT_BESPOKE|ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
/// our pixel_x offset - how much the item moves x when in bed (+x is closer to the pillow)
var/x_offset = 0
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/bump_click.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Simulates a click on the attached atom when it's bumped, if the bumper and their active object meet certain criteria.
*/
/datum/element/bump_click
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH // Detach for turfs
id_arg_index = 2
///Tool behaviours to check for on the bumper's active held item before clicking the attached atom with it.
var/list/tool_behaviours
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/chemical_transfer.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* victim_message uses %ATTACKER for the same.
*/
/datum/element/chemical_transfer
element_flags = ELEMENT_BESPOKE|ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
///chance for the chemical transfer to proc.
var/transfer_prob
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/climbable.dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/datum/element/climbable
element_flags = ELEMENT_BESPOKE|ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH // Detach for turfs
id_arg_index = 2
///Time it takes to climb onto the object
var/climb_time = (2 SECONDS)
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/crackable.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Adds crack overlays to an object when integrity gets low
/datum/element/crackable
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
var/list/icon/crack_appearances
/// The level at which the object starts showing cracks, 1 being at full health and 0.5 being at half health
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/curse_announcement.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Possible improvements for the future: add an option to allow the cursed affix to be a prefix. right now only coded for suffixes
*/
/datum/element/curse_announcement
element_flags = ELEMENT_BESPOKE|ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
///message sent on announce
var/announcement_message
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/cursed.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*Attaching this element to something will make it float, and get a special ai controller!
*/
/datum/element/cursed
element_flags = ELEMENT_DETACH

/datum/element/cursed/Attach(datum/target, slot)
. = ..()
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/delete_on_drop.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Attaches to an item, if that item is dropped on the floor delete it
*/
/datum/element/delete_on_drop
element_flags = ELEMENT_DETACH
var/list/myvar = list()

/datum/element/delete_on_drop/Attach(datum/target)
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/deliver_first.dm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define DENY_SOUND_COOLDOWN (2 SECONDS)
/datum/element/deliver_first
element_flags = ELEMENT_DETACH | ELEMENT_BESPOKE
element_flags = ELEMENT_BESPOKE
id_arg_index = 2
///typepath of the area we will be allowed to be opened in
var/goal_area_type
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/drag_pickup.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Used for paper bins.
*/
/datum/element/drag_pickup
element_flags = ELEMENT_DETACH

/datum/element/drag_pickup/Attach(datum/target)
if(!ismovable(target))
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/easily_fragmented.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/datum/element/easily_fragmented
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2

var/break_chance
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/empprotection.dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/datum/element/empprotection
element_flags = ELEMENT_DETACH | ELEMENT_BESPOKE
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH // Detach for turfs
id_arg_index = 2
var/flags = NONE

Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/eyestab.dm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// An element that lets you stab people in the eyes when targeting them
/datum/element/eyestab
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
element_flags = ELEMENT_BESPOKE
id_arg_index = 2

/// The amount of damage to do per eyestab
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/frozen.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ GLOBAL_LIST_INIT(freon_color_matrix, list("#2E5E69", "#60A2A8", "#A1AFB1", rgb(0

///simple element to handle frozen obj's
/datum/element/frozen
element_flags = ELEMENT_DETACH

/datum/element/frozen/Attach(datum/target)
. = ..()
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/haunted.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
///Attaching this element to something will make it float, get a special ai controller, and gives it a spooky outline.
/datum/element/haunted
element_flags = ELEMENT_DETACH

/datum/element/haunted/Attach(datum/target)
. = ..()
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/honkspam.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// Attachable to items. Plays a bikehorn sound whenever attack_self is called (with a cooldown).
/datum/element/honkspam
element_flags = ELEMENT_DETACH

/datum/element/honkspam/Attach(datum/target)
. = ..()
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/kneecapping.dm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* Passing all the checks will cancel the entire attack chain.
*/
/datum/element/kneecapping
element_flags = ELEMENT_DETACH

/datum/element/kneecapping/Attach(datum/target)
if(!isitem(target))
Expand Down
1 change: 0 additions & 1 deletion code/datums/elements/kneejerk.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// An element which enables certain items to tap people on their knees to measure brain health
/datum/element/kneejerk
element_flags = ELEMENT_DETACH

/datum/element/kneejerk/Attach(datum/target)
. = ..()
Expand Down
Loading

0 comments on commit 5dc2711

Please sign in to comment.