Skip to content

[perf] Make projectile cleanup deterministic in OnSecondUpdate#3212

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

[perf] Make projectile cleanup deterministic in OnSecondUpdate#3212
Xekep wants to merge 1 commit intoPryaxis:general-develfrom
TerraZ-Team:pr/micro-perf-projectile-cleanup

Conversation

@Xekep
Copy link
Contributor

@Xekep Xekep commented Feb 27, 2026

Summary

  • run recent projectile cleanup directly in second-update path
  • remove stale entries in place with RemoveAll under lock

Scope

  • focused change in Bouncer.OnSecondUpdate only
  • no unrelated file changes

Run cleanup on the second-update path directly and remove stale entries in place with RemoveAll. This avoids spawning a background task each second and avoids replacing the list reference while other code may lock it.
@hakusaro
Copy link
Member

@greptile review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 27, 2026

Greptile Summary

Refactored projectile cleanup in OnSecondUpdate to run synchronously instead of in a background task, improving determinism and thread safety. The changes include switching from DateTime.Now to DateTime.UtcNow, optimizing cleanup with RemoveAll instead of Where().ToList(), and calculating the threshold once before the loop for better performance.

Key improvements:

  • Eliminated potential race conditions by removing Task.Run - cleanup now runs on the main thread where all handlers access the projectile list
  • More efficient memory usage with in-place RemoveAll instead of allocating new lists
  • Consistent time handling with DateTime.UtcNow
  • Threshold calculated once instead of per-player

Note: Documentation in TSPlayer.cs:1053 still references async execution and should be updated.

Confidence Score: 5/5

  • This PR is safe to merge with no risk - it improves both correctness and performance
  • The changes are well-focused and actually improve thread safety by eliminating async execution that was causing potential race conditions with unlocked reads throughout the codebase. The logic is equivalent, more efficient, and more deterministic.
  • No files require special attention - the change is straightforward and correct

Important Files Changed

Filename Overview
TShockAPI/Bouncer.cs Improved projectile cleanup by removing async execution, using UTC time, and optimizing with RemoveAll instead of Where().ToList()

Last reviewed commit: 584073c

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 27, 2026

Additional Comments (1)

TShockAPI/TSPlayer.cs
documentation is outdated - cleanup no longer runs in an async task

		/// Keeps track of recently created projectiles by this player. TShock.cs OnSecondUpdate() removes from this synchronously.

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