Repository navigation
Conversation
The timers of the objects and the periodic ticks of the characters were kept in sorted vectors, rebuilt every tick from queued add/erase requests. With many timers this dominated the tick time. Use a std::multimap ordered by expiration time instead. Every object stores the iterator of its own entry, so adding, removing or rescheduling it doesn't need any search, and the node is extracted and reused when it's rescheduled, so it doesn't allocate either. Each tick only visits the expired entries from the head. Entries expiring in the same tick still fire ordered by expiration time, then by insertion order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem.
CWorldTickerkeeps every pending object timer (TIMER, decay, spawns, memory items, spell effects, ...) and the periodic tick of every awake character in sortedstd::vectors. In every tick with at least one add or remove request the whole vector is rebuilt: removed entries are copied out, added ones arestd::merged in, and expired ones are erased from the front. On a live shard awake NPCs reschedule every tick, so the rebuild runs in practically every tick and costs O(total timers), even though almost all timers are just waiting. With many timers this dominates the tick time.Change. The timers are kept in a
std::multimapordered by expiration time (src/game/CTimedObject.h,src/game/CWorldTicker.cpp):(N = pending timers, C = add/remove requests in the tick, k = timers expiring in the tick.)
Benchmarks (Release, Windows,
NetworkThreads=0, client logged in, 45 s runs,Tick()time summed over 10 s windows):Tick()per 10 s@Timerscripts/s)Tick()per 10 sTick()per 10 sWith many timers the tick is about 6x faster and uses about 3x less CPU. Under heavy script load (scripts take ~85 % of the tick) the gain is small: about 7 % of the tick time and 14 % of the CPU. Most of the tick is scripts there, and the O(log N) inserts into a tree of ~220k nodes (50-93 ms per 10 s) cost more than the queued adds of the old code (~3 ms). In a small world the difference is within the measurement noise. The machine is noisy (up to 2x between runs of the same binary), so the numbers above compare runs of the same batch.
Type of Change
Related Issues
Addresses item P1 of the performance audit in #1671 (world timers rebuilt every tick).
Alternative implementation of the same change: #1672 (timing wheel,
perf/ticker-wheel). Only one of the two should be merged; see "Alternative" below.Testing
No new unit tests: the container is a standard
std::multimap, the logic around it is the same entry bookkeeping as before. The randomized unit test of #1672 uses an ordered container with the same semantics as its reference model.In-game tests on a clean instance with the latest ScriptPack and a real client, Debug builds (all asserts active), before vs after. Every timer checks it fires not early, not more than 500 ms late, not twice, not after being removed or cancelled, and in order where required. 24 scenarios: ordering, equal due times, removal before/after firing, cancel and re-arm, self-rescheduling and self-removal from
@Timer,TIMERMS=0,TIMER/TIMERDunits, negative timers, a stress test of 1000 timers with random removals and reschedules, equipped items, NPC periodic ticks with chars killed/removed during the tick, contained timers, decay,CAN_O_NOSLEEPin sleeping sectors, spawns, items moved between ground/backpack/equipped while a timer is pending.No error, assert or crash in any run. Lateness and the behavior of the informational scenarios (sleeping sectors, containers, dupe) are identical before and after.
Checklist
Changelog.txt)No script-visible change: timers fire at the same times. The only difference is the order of timers with exactly the same expiration time: before it was arbitrary (sorted by object pointer), now it is the order they were set.
Alternative: timing wheel ( #1672 )
The other PR replaces the sorted vectors with a timing wheel: 25 ms slots over a window of
TimerWheelSlotsslots (newsphere.inisetting, default ~55 minutes), an overflow list for later timers, intrusive O(1) add/remove.Tick()per 10 s, 200k timersTick()per 10 s, heavy reschedulingTree pros:
src/game/CTickWheel.h), easier to review and maintain;sphere.inisetting to tune;Tree cons:
Wheel cons:
The wheel is faster or equal in every scenario; this PR is the simpler option if reviewability matters more than the last 10-15 % (and much more under heavy rescheduling).
Additional Notes
@Timerof A removes B, or sets B's timer to later, and B expired in the same tick, B's@Timerstill runs (on an object being deleted), and the new timeout is cleared afterwards (the reschedule is lost). Fixing it changes script-visible behavior, so it is left for a separate PR.NetworkThreads=0: withNetworkThreads=1the current code (also without this PR) crashes intermittently inCNetworkOutput(purecall / access violation), about 3 runs out of 15.