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

Skip to content

[perf/fix] Correct projectile tracking updates and reduce list scans#3231

Open
Xekep wants to merge 1 commit intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-projectile-tracking-clean
Open

[perf/fix] Correct projectile tracking updates and reduce list scans#3231
Xekep wants to merge 1 commit intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-projectile-tracking-clean

Conversation

@Xekep
Copy link

@Xekep Xekep commented Feb 28, 2026

Players reported lag under load in packet-heavy combat.

This patch fixes value-type update loss when marking tracked projectiles as killed, replaces repeated LINQ scans in projectile tracking hot paths with loops, and uses consistent UTC timestamps for recency checks.

Behavior is preserved, but tracking is more correct and cheaper under load.

Correct projectile kill marking for ProjectileStruct entries (value-type updates were previously lost), serialize recent-projectile updates in NetHooks_SendData, and reduce repeated list scans in projectile tracking paths.

Also standardize projectile recency timestamps and cleanup thresholds to DateTime.UtcNow to avoid clock-shift inconsistencies.
@greptile-apps
Copy link

greptile-apps bot commented Feb 28, 2026

Greptile Summary

This PR addresses performance and correctness issues in projectile tracking hot paths. It fixes a critical bug where the Killed flag wasn't being set on ProjectileStruct due to value-type semantics in the ForEach lambda, replaces expensive LINQ operations with manual loops for better performance under load, and standardizes timestamp handling with DateTime.UtcNow.

Key Changes:

  • Fixed struct mutation bug in HandleProjectileKill where Killed flag wasn't persisting (GetDataHandlers.cs:3303-3311)
  • Added missing lock around RecentlyCreatedProjectiles in projectile update handler (TShock.cs:1840)
  • Replaced LINQ .Any() with manual loops to reduce allocations in hot paths
  • Moved threshold calculation outside loop in OnSecondUpdate (Bouncer.cs:3065)
  • Switched all timestamp operations from DateTime.Now to DateTime.UtcNow for consistency

The changes preserve existing behavior while making projectile tracking more correct and efficient during packet-heavy combat scenarios.

Confidence Score: 5/5

  • This PR is safe to merge with no concerns
  • All changes are well-reasoned performance optimizations and correctness fixes. The critical struct mutation bug fix is correct, LINQ-to-loop conversions are logically equivalent, locking improvements enhance thread safety, and UTC timestamp usage improves consistency. No new issues introduced.
  • No files require special attention

Important Files Changed

Filename Overview
TShockAPI/Bouncer.cs Optimized threshold calculation by moving it outside the loop and switched to DateTime.UtcNow for consistency
TShockAPI/GetDataHandlers.cs Fixed critical struct mutation bug in projectile kill handling and replaced LINQ with loops for performance; switched to DateTime.UtcNow
TShockAPI/TShock.cs Added missing lock around RecentlyCreatedProjectiles access, replaced LINQ with efficient backwards iteration, and switched to DateTime.UtcNow

Last reviewed commit: 1f6705f

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