Skip to content

perf: replaced Mutex with Spinlock in Dispatcher and Draw Pipeline#1238

Merged
mehah merged 7 commits intomainfrom
drawpool_lockfree
Jul 19, 2025
Merged

perf: replaced Mutex with Spinlock in Dispatcher and Draw Pipeline#1238
mehah merged 7 commits intomainfrom
drawpool_lockfree

Conversation

@mehah
Copy link
Copy Markdown
Collaborator

@mehah mehah commented Jul 18, 2025

Description

This update replaces the use of mutex with spinlock in two critical parts of the system: the event dispatcher and the game draw pipeline. The change aims to optimize performance in multithreaded scenarios where critical sections are very short and accessed frequently.

📌 Technical Context

🧵 Event Dispatcher (Thread-local Buffers)

  • Each thread maintains its own event buffer, avoiding contention during collection.
  • At the end of the frame, all buffers are merged into a central structure.
  • Previously, this merge process used a mutex, introducing OS-level overhead.
  • It now uses a spinlock, allowing threads to wait without being suspended.

🎮 Draw Pipeline (Main Thread + Worker)

  • The rendering system is split into:
    • Main thread: issues GPU draw commands.
    • Worker thread: gathers rendering data and sends it to the main thread.
  • Synchronization between the two used to rely on mutex, now replaced with spinlock.
  • As a result, a noticeable framerate improvement was observed due to reduced latency in the data exchange.

⚙️ Why Spinlock Instead of Mutex?

  • mutex uses OS-level syscalls (e.g., futex) during contention, which can cause expensive context switches.
  • spinlock avoids these syscalls by keeping the thread in a busy-wait loop, which is ideal when:
    • The critical section is very short.
    • The lock hold time is extremely low.
    • Operations are frequent and lightweight, such as in game loops and event merging.
  • With spinlock, the synchronization cost drops from microseconds (mutex + syscall) to a few CPU cycles, improving:
    • Latency
    • Throughput
    • Framerate stability

🚀 Observed Results

  • Significant reduction in lock contention latency.
  • More stable framerate, with fewer micro-stutters during synchronization peaks.
  • Lower jitter between event collection and frame rendering.

⚠️ Notes

  • spinlock usage is carefully scoped to ultra-short critical sections to avoid excessive busy-waiting.
  • Strategy will be revisited if contention levels increase in future versions.

@mehah mehah changed the title Drawpool lockfree perf: replaced Mutex with Spinlock in Dispatcher and Draw Pipeline Jul 18, 2025
@mehah mehah merged commit 5ef4b7e into main Jul 19, 2025
11 checks passed
@mehah mehah deleted the drawpool_lockfree branch July 19, 2025 14:52
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant