Files
0716e3fff4 Atmos refactor & speedup by utilizing BYOND 516 vector functions (#96448)
## About The Pull Request

### Summary
This PR changes internal structure of `/datum/gas_mixture`:
`gases[gas_id][MOLES]` refactored into `moles[gas_id]`,
`gases[gas_id][ARCHIVE]` into `moles_archive[gas_id]` and
`gases[gas_id][GAS_META]` into `gas_meta` static variable. This allows
us to use BYOND 516 vector functions for calculating total moles and
heat capacity. Also it simplifies some parts of the code, allowing us to
get rid of macros `ADD_GAS` and `ASSERT_GAS`. According to the profiler
`/turf/open/process_cell` time is reduced by ~20%.

### Details
`gas_mixture.gases` was a nested 2d-list with MOLES, ARCHIVE and
GAS_META for each gas_id. For example, to get gas moles you had to do
`gases[gas_id][MOLES]`. I've changed this structure to be as follows:
`moles[gas_id]` - moles for the gas, `moles_archive[gas_id]` - archived
version of moles, `gas_meta[KEY][gas_id]` - static var with meta
information for the gas.

Since I removed key GAS_META from the gases, `gas_meta` was moved to the
static variable and the order of keys in the array was changed from
`gas_meta[gas_id][META_KEY]' to 'gas_meta[META_KEY][gas_id]`. This was
done to allow using it in vector calculations (for example heat capacity
or fusion power). Static variable access is very fast and it is
considered as accessing a global in the bytecode.

Byond 516 introduced new vector functions: `values_sum`, `values_dot`
and others. These functions are very fast, but operate only on
associative lists. This allows us to change the way we calculate
total_moles and heat_capacity - very hot and heavily used functions.
`total_moles()` became just `values_sum(moles)`, and `heat_capacity` is
just a dot product: `values_dot(moles,
gas_meta[META_GAS_SPECIFIC_HEAT])`.

As a side bonus, since `moles` is just an associative list, you don't
really need old macros `ADD_GAS` and `ASSERT_GAS` - all they did was to
make a copy of a list[3] with default value [0, 0, gas_meta] for
specific gas. Now when you're adding gas you can just use `moles[gas_id]
+= amount` and when you query amount of gas you can just query the key
(for example `moles[/datum/gas/oxygen]`) if the key does not exist, it
returns null and works as 0 for all arithmetic and logic operations. For
example, old code would be `if (!air.gases[/datum/gas/oxygen] ||
air.gases[/datum/gas/oxygen][MOLES] < 1)` and now it is `if
(moles[/datum/gas/oxygen] < 1)`. This simplifies some parts of the code
and also speeds things up.

For the performance comparison I used Tracy profiler. I've done many
different tests, and they all show slightly different numbers, but
overall speedup for `process_cell` is about 20%. (-20% to average time
per call from ). My testing setup was as follows:
Load Icebox, drop 30/60/90 radius bomb in the middle of the bridge, set
code to blue, wait 10 minutes until the round ends.
Also I fixed random seed in the master controller and in the planetary
gas randomization so generated maps are the same between tests.
Althought it's not very realistic, it generates a lot of samples for the
`process_cell` (around ~3.5M per 10 minutes).
Another test I did was a plasmafire in an 8x8 space, on runtime station,
it showed (-24% time on process_cell).
Another test was a emagged holodeck burn test, it showed (-13% time)
As for other functions of gas_mixture: `total_moles`: -50%(2x speedup),
`heat_capacity`: -65%(3x speedup), `share`: -30%, `react`: -20%. Timings
of all those functions is in microseconds range and they are very hot
(call count is in the same order as process_cell)

<details><summary>Some pictures from profiler</summary>
<img width="569" height="642" alt="process_cell"
src="https://github.com/user-attachments/assets/76fa0c27-719d-485d-9bfc-859fef788999"
/>
<img width="572" height="315" alt="image"
src="https://github.com/user-attachments/assets/f68497e9-4db8-4a9c-b43f-ad04e6dc5cac"
/>
<img width="569" height="317" alt="image"
src="https://github.com/user-attachments/assets/d7249e7b-f344-47a6-8b39-1bab0521182d"
/>
<img width="541" height="316" alt="image"
src="https://github.com/user-attachments/assets/1bef71fd-533d-40fa-a85e-7a803ad322f7"
/>
<img width="519" height="409" alt="image"
src="https://github.com/user-attachments/assets/51165289-174e-403d-a09c-787dd9af136a"
/>
<img width="523" height="318" alt="image"
src="https://github.com/user-attachments/assets/fe4b1db0-17d8-47f9-8fd0-e2ecef2ee66a"
/>
</details>

<details><summary>Setting up a profiler</summary>
If you wanna to reproduce my results here is a list of steps

1. download: https://github.com/goonstation/byond-tracy-writer (this one
has offsets for my version 1677)
2. build the dll, drop in the tgstation/ folder
3. download rtracy https://github.com/Dimach/rtracy
4. download Tracy profiler (0.13.1) https://github.com/wolfpld/tracy
5. uncomment `#define USE_BYOND_TRACY` in `_compile_options.dm`
6. build tgstation
7. open dream daemon, run the desired test, after round end dream daemon
closes
8. navigate to tgstation/data/profiler, find the `123412341234.utracy`
file
9. run `rtracty 123412341234.utracy`
10. open tracy-profiler.exe, press Connect, save the profiler data
11. repeat steps 5-10 with another branch, save another profiler data
12. open tracy-profiler, open first data, press compare, open second
data
</details>

## Why It's Good For The Game

## Changelog
🆑
refactor: Atmos refactor & speedup by utilizing BYOND 516 vector
functions
/🆑

---------

Co-authored-by: san7890 <the@san7890.com>
2026-07-14 20:34:31 -07:00

113 lines
4.1 KiB
Plaintext

/datum/supply_pack/materials
group = "Canisters & Materials"
/datum/supply_pack/materials/cardboard50
name = "50 Cardboard Sheets"
desc = "Create a bunch of boxes."
cost = CARGO_CRATE_VALUE * 2
contains = list(/obj/item/stack/sheet/cardboard/fifty)
crate_name = "cardboard sheets crate"
/datum/supply_pack/materials/license50
name = "50 Empty License Plates"
desc = "Create a bunch of license plates."
cost = CARGO_CRATE_VALUE * 2 // 50 * 25 + 700 - 1000 = 950 credits profit
access_view = ACCESS_BRIG_ENTRANCE
contains = list(/obj/item/stack/license_plates/empty/fifty)
crate_name = "empty license plate crate"
/datum/supply_pack/materials/plastic50
name = "50 Plastic Sheets"
desc = "Build a limitless amount of toys with fifty plastic sheets!"
cost = CARGO_CRATE_VALUE * 2
contains = list(/obj/item/stack/sheet/plastic/fifty)
crate_name = "plastic sheets crate"
/datum/supply_pack/materials/sandstone30
name = "30 Sandstone Blocks"
desc = "Neither sandy nor stony, these thirty blocks will still get the job done."
cost = CARGO_CRATE_VALUE * 2
contains = list(/obj/item/stack/sheet/mineral/sandstone/thirty)
crate_name = "sandstone blocks crate"
/datum/supply_pack/materials/wood50
name = "50 Wood Planks"
desc = "Turn cargo's boring metal groundwork into beautiful \
panelled flooring and much more with fifty wooden planks!"
cost = CARGO_CRATE_VALUE * 4
contains = list(/obj/item/stack/sheet/mineral/wood/fifty)
crate_name = "wood planks crate"
/datum/supply_pack/materials/foamtank
name = "Firefighting Foam Tank Crate"
desc = "Contains a tank of firefighting foam. Also known as \"plasmaman's bane.\""
cost = CARGO_CRATE_VALUE * 3
contains = list(/obj/structure/reagent_dispensers/foamtank)
crate_name = "foam tank crate"
crate_type = /obj/structure/closet/crate/large
/datum/supply_pack/materials/fueltank
name = "Fuel Tank Crate"
desc = "Contains a welding fuel tank. Caution, highly flammable."
cost = CARGO_CRATE_VALUE * 1.6
contains = list(/obj/structure/reagent_dispensers/fueltank)
crate_name = "fuel tank crate"
crate_type = /obj/structure/closet/crate/large
/datum/supply_pack/materials/hightankfuel
name = "Large Fuel Tank Crate"
desc = "Contains a high-capacity fuel tank. Keep contents away from open flame."
cost = CARGO_CRATE_VALUE * 4
access_view = ACCESS_ENGINEERING
contains = list(/obj/structure/reagent_dispensers/fueltank/large)
crate_name = "high-capacity fuel tank crate"
crate_type = /obj/structure/closet/crate/large
/datum/supply_pack/materials/watertank
name = "Water Tank Crate"
desc = "Contains a tank of dihydrogen monoxide... sounds dangerous."
cost = CARGO_CRATE_VALUE * 1.2
contains = list(/obj/structure/reagent_dispensers/watertank)
crate_name = "water tank crate"
crate_type = /obj/structure/closet/crate/large
/datum/supply_pack/materials/gas_canisters
cost = CARGO_CRATE_VALUE * 0.05
contains = list(/obj/machinery/portable_atmospherics/canister)
crate_type = /obj/structure/closet/crate/large
test_ignored = TRUE
/datum/supply_pack/materials/gas_canisters/generate_supply_packs()
var/list/canister_packs = list()
var/obj/machinery/portable_atmospherics/canister/fakeCanister = /obj/machinery/portable_atmospherics/canister
// This is the amount of moles in a default canister
var/moleCount = (initial(fakeCanister.maximum_pressure) * initial(fakeCanister.filled)) * initial(fakeCanister.volume) / (R_IDEAL_GAS_EQUATION * T20C)
for(var/gasType in GLOB.meta_gas_info[META_GAS_ID])
var/datum/gas/gas = gasType
var/name = initial(gas.name)
if(!initial(gas.purchaseable))
continue
var/datum/supply_pack/materials/pack = new
pack.name = "[name] Canister"
pack.desc = "Contains a canister of [name]."
if(initial(gas.dangerous))
pack.access = ACCESS_ATMOSPHERICS
pack.access_view = ACCESS_ATMOSPHERICS
pack.crate_name = "[name] canister crate"
pack.id = "[type]([name])"
pack.cost = cost + moleCount * initial(gas.base_value) * 1.6
pack.cost = CEILING(pack.cost, 10)
pack.test_ignored = FALSE
pack.contains = list(GLOB.gas_id_to_canister[initial(gas.id)])
pack.crate_type = crate_type
canister_packs += pack
return canister_packs