Skip to content

improve: Dispatcher event merging logic#562

Open
jprzimba wants to merge 1 commit intomainfrom
improve-dispatcher
Open

improve: Dispatcher event merging logic#562
jprzimba wants to merge 1 commit intomainfrom
improve-dispatcher

Conversation

@jprzimba
Copy link
Collaborator

@jprzimba jprzimba commented Jan 7, 2026

1. CPU Usage (Identified Bottlenecks)

  • Lock Contention: The dispatcher was locking the mutexes of all worker threads multiple times per cycle to fetch tasks from different groups (Serial, Walk, Parallel). This caused significant overhead on processors with many cores.
  • Merge Redundancy: The functions mergeEvents and mergeAsyncEvents were repeatedly called within loops, forcing unnecessary reprocessing.
  • Busy-Wait: The main loop wasn't efficiently consolidating the check for pending tasks before deciding whether to sleep (wait_for), which could lead to CPU usage even when no immediate tasks were available.
  • Inefficient Calculations: The partitioning of parallel tasks used floating-point calculations (std::ceil) in a critical code path.

Implemented Changes

Task Merge Optimization

  • I implemented the function __mergeEvents() to process all task groups and scheduled tasks in a single pass per thread.
  • Now, each worker thread locks its mutex only once per dispatcher cycle, drastically reducing contention.

Main Loop Cleanup

  • I reorganized init() to consolidate time updates and ensure that merges occur at the beginning of the cycle.
  • Removed redundant calls to UPDATE_OTSYS_TIME().

Simplification of executeEvents

  • Removed merge calls inside the execution loop. Since mergeAll occurs at the start of the cycle, the data is already ready for serial or parallel processing without additional locks.
  • File: dispatcher.cpp:L121-129

Improvement in Parallel Partitioning

  • Replaced the use of std::ceil with faster integer arithmetic to divide tasks among the pool threads.
  • File: dispatcher.hpp:L202-214

These changes should significantly reduce server CPU usage, especially during high load situations or when many asynchronous events are triggered simultaneously.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale label Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant