canary/data
Eduardo Dantas bc826add94
perf: improve lifetime scheduling and monster AI hot paths (#4023)
Optimize monster hot paths and deferred gameplay scheduling

This commit implements the next slice of the performance and lifetime roadmap for the Canary creature and monster hot paths.

The focus is to reduce dispatcher pressure, async task fanout, spectator churn, pathfinding churn, and target list lifetime risk under monster-heavy workloads while keeping player visible gameplay behavior intact.

The change keeps combat and condition handling on the serial dispatcher path, but moves delayed monster post think work into a budgeted DeferredGameplay lane. This allows monster follow-up work to make progress without competing directly with the most sensitive gameplay operations on the main dispatcher.

Creature async work is now sliced through bounded buckets. This limits how much async creature work can be emitted at once and helps avoid large bursts of scheduled tasks. The deferred async flag clearing behavior is preserved, so creatures are not incorrectly considered ready for another async pass before the deferred work has actually completed.

Monster movement AI refreshes are now coalesced. Instead of scheduling repeated movement refreshes for the same monster in a burst, the system can merge pending refresh intents and execute them more controllably. This reduces redundant dispatcher work and helps monster-heavy scenarios avoid excessive task fanout.

Target lifetime handling was also improved. Monster target tracking now keeps player count bookkeeping balanced even when weak target references expire. This reduces the risk of monsters staying active because an expired player target was removed from the target list without updating the related active player count.

Pathfinding and tile lookup hot paths now reuse scoped floor cursors. This avoids repeated cursor setup work during repeated spectator and pathfinding operations while keeping the safe lifetime boundaries documented. The reuse is scoped so temporary lookup state does not escape beyond the valid operation window.

Benchmark-only monster stress options were added to make heavy monster workloads easier to test. These settings are intended for profiling and stress validation only, not normal production gameplay. A startup warning appears when stress settings are enabled, so operators do not accidentally run production servers with benchmark behavior active.

The change also preserves current client protocol compatibility details unrelated to the monster performance work, but necessary to keep behavior stable. The current 15 13 screenshot event payload encoding and level percent protocol gates remain preserved.

Main changes included in this commit

• Adds benchmark-only monster stress configuration flags

• Adds a startup warning when benchmark monster stress flags are enabled

• Allows stress scenarios such as keeping monsters active for benchmark purposes

• Allows monster versus monster target selection only through the benchmark stress configuration

• Slices creature async work through bounded buckets

• Preserves deferred async flag clearing behavior

• Routes delayed monster post think work through the budgeted DeferredGameplay lane

• Keeps combat and condition execution serial on the dispatcher

• Coalesces monster movement AI refreshes to reduce redundant scheduling

• Improves monster target tracking and target list lifetime safety

• Keeps target player count bookkeeping balanced when weak target references expire

• Improves monster idle behavior under expired target cleanup cases

• Reuses scoped floor cursors for pathfinding and tile lookup hot paths

• Documents the safe lifetime boundaries for cursor reuse

• Reduces spectator and pathfinding churn in monster-heavy workloads

• Preserves the current 15 13 screenshot event payload encoding

• Preserves level percent protocol gates

• Fixes the special Starving Wolf trap interaction

• Adds dispatcher queue latency logging to help monitor responsiveness

• Documents performance and lifetime roadmap evidence

• Documents validation assumptions for production-like scenarios and stress test scenarios

Runtime behavior

Monster AI and creature scheduling should now produce less dispatcher pressure during dense monster activity. Delayed monster post-think work is handled through the deferred gameplay lane with a budget, rather than being allowed to fan out into unbounded dispatcher pressure.

Player visible gameplay contracts remain intact. Combat and conditions continue to run through the serial dispatcher path, so the change does not intentionally reorder sensitive gameplay effects.

Movement and auto-walk behavior should be more predictable under load because repeated monster movement AI refreshes can be coalesced rather than stacking redundant work. Monsters should still move and react, but the refresh work is scheduled with less unnecessary churn.

Target tracking is safer when targets disappear. If a player target expires through a weak reference cleanup path, the monster target side bookkeeping is also updated, so idle and active state decisions are not based on stale player counts.

Stress testing behavior

The new monster stress flags are benchmark-only controls. They make it easier to reproduce monster-heavy workloads for profiling and validation, but they are not intended as normal server behavior.

When these flags are enabled, the server emits a startup warning to make the configuration's non-production nature clear.

Validation performed

• Static validation with git diff check HEAD 5 HEAD

• Reviewed production-like behavior assumptions for dispatcher and monster scheduling

• Reviewed stress test behavior assumptions for monster heavy workloads

• Follow up commits addressed validated comments around target count cleanup

• Follow-up commits addressed trap handling comments

• Follow-up commits addressed async scheduling comments

• Follow up commits addressed documentation comments

Overall, this commit improves the performance and lifetime safety of the Canary monster and creature hot paths. It reduces unnecessary async fanout dispatcher pressure, movement refresh churn, and pathfinding lookup overhead while preserving the serial gameplay contracts that matter for players.
2026-07-07 19:23:39 -03:00
..
chatchannels fix: guild leader channel (#3433) 2025-03-14 18:26:55 -03:00
events ci/cd: fix gha on main branch (#3848) 2026-02-13 01:16:45 -03:00
items feat: update current client 15.25 compatibility (#4020) 2026-07-06 20:42:40 -03:00
json/eventscheduler feat: add event scheduler by json (#3085) 2026-01-08 17:21:22 -03:00
lib/core/quests refactor: quest system to use modular catalog loading (#3763) 2026-01-06 15:12:17 -03:00
libs feat: update current client 15.25 compatibility (#4020) 2026-07-06 20:42:40 -03:00
logs Initial commit 2021-04-26 21:04:01 -03:00
modules feat: update current client 15.25 compatibility (#4020) 2026-07-06 20:42:40 -03:00
npclib feat: 15.13 npc module protocol and update NPC Lua scripts (#3982) 2026-07-01 10:12:58 -03:00
reports Initial commit 2021-04-26 21:04:01 -03:00
scripts perf: improve lifetime scheduling and monster AI hot paths (#4023) 2026-07-07 19:23:39 -03:00
XML feat: protocol 15.11 (weapon proficiency and new imbuement scroll) (#3845) 2026-05-21 14:34:39 -03:00
core.lua [Enhancement] rework for enable choosing of a custom datapack name (#572) 2022-11-18 22:32:44 -03:00
global.lua feat: add Way of the Monk quest and monk-related content (#3794) 2026-02-12 23:03:25 -03:00
stages.lua improve: switch to Stylua formatter (#1582) 2023-09-10 10:17:19 -07:00
update.lua improve: lua format from EmmyLuaCodeStyle (#1513) 2023-08-27 10:49:56 -07:00