Skip to content

[perf] Use TryGetValue for net module handler resolution#3229

Merged
hakusaro merged 1 commit intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-netmodule-trygetvalue-clean
Mar 5, 2026
Merged

[perf] Use TryGetValue for net module handler resolution#3229
hakusaro merged 1 commit intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-netmodule-trygetvalue-clean

Conversation

@Xekep
Copy link
Contributor

@Xekep Xekep commented Feb 28, 2026

This path runs for every net module packet.

This patch replaces a double dictionary lookup (ContainsKey + indexer) with a single TryGetValue lookup.

Behavior stays the same; it is only a small hot-path cleanup.

Replace double dictionary lookup (ContainsKey + indexer) with a single TryGetValue path in NetModulePacketHandler. This is a small but safe hot-path cleanup.
@greptile-apps
Copy link

greptile-apps bot commented Feb 28, 2026

Greptile Summary

This PR optimizes the net module packet handler by replacing a double dictionary lookup with a single TryGetValue call. The change is functionally equivalent but more efficient.

  • Replaced ContainsKey + indexer pattern with TryGetValue in OnReceive method
  • Reduces dictionary lookups from 2 to 1 for every net module packet processed
  • Follows idiomatic C# best practices for dictionary access
  • No behavioral changes, purely a performance optimization

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The change is a straightforward performance optimization with no behavioral changes. The TryGetValue pattern is functionally identical to ContainsKey + indexer but more efficient. The code is simple, well-understood, and the optimization is a common C# best practice.
  • No files require special attention

Important Files Changed

Filename Overview
TShockAPI/Handlers/NetModules/NetModulePacketHandler.cs Optimized dictionary lookup from ContainsKey+indexer to TryGetValue, reducing from 2 to 1 lookup in hot path

Last reviewed commit: 4ba2f1b

@hakusaro hakusaro merged commit 50b1502 into Pryaxis:general-devel Mar 5, 2026
2 checks passed
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.

2 participants