Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[perf] Optimize handler dispatch with snapshot visibility safety#3211

Open
Xekep wants to merge 2 commits intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-handler-snapshot
Open

[perf] Optimize handler dispatch with snapshot visibility safety#3211
Xekep wants to merge 2 commits intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-handler-snapshot

Conversation

@Xekep
Copy link

@Xekep Xekep commented Feb 27, 2026

Summary

  • switch invoke path to copy-on-write handler snapshots
  • publish/read snapshot with Volatile.Write/Read for cross-thread visibility

Scope

  • focused change in HandlerList only
  • no unrelated file changes

Avoid per-invocation list allocations and lock holding in HandlerList.Invoke by using a snapshot array refreshed on register/unregister.
@pacenadhif778
Copy link
Contributor

lgtm

@hakusaro
Copy link
Member

@greptile review

@greptile-apps
Copy link

greptile-apps bot commented Feb 27, 2026

Greptile Summary

This PR optimizes the handler dispatch mechanism by implementing a copy-on-write pattern with proper cross-thread visibility guarantees. The changes eliminate locking on the hot path (Invoke) while maintaining thread safety through Volatile.Read/Write.

Key improvements:

  • Replaced lock-based copying in Invoke with lock-free snapshot reads using Volatile.Read
  • Added _handlerSnapshot array field initialized to Array.Empty<HandlerItem>()
  • Updated snapshot with Volatile.Write after modifications in Register and UnRegister
  • Added conditional snapshot update in UnRegister to avoid unnecessary allocations when no handlers are removed
  • Added early return optimization when handler list is empty

Thread safety analysis:

  • Modifications (Register/UnRegister) remain protected by HandlerLock
  • Volatile.Write ensures snapshot updates are visible across threads with proper memory barriers
  • Volatile.Read in Invoke guarantees consistent snapshot visibility without tearing
  • Snapshot arrays are immutable after creation, preventing concurrent modification issues

The implementation correctly balances performance (lock-free reads) with safety (volatile semantics for visibility). No issues found.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The implementation correctly uses the copy-on-write pattern with proper volatile semantics for cross-thread visibility. Lock protection for mutations prevents race conditions, while lock-free reads optimize the hot path. The code is focused, well-structured, and introduces a clear performance improvement without compromising thread safety.
  • No files require special attention

Important Files Changed

Filename Overview
TShockAPI/HandlerList.cs Implements copy-on-write pattern with Volatile.Read/Write for lock-free handler invocation, optimizing hot path performance while maintaining thread safety

Last reviewed commit: 1736b23

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.

3 participants