Skip to content

perf(network): Reduce loop complexity for transport messages and remove unused static variables#2659

Merged
xezon merged 4 commits into
TheSuperHackers:mainfrom
Caball009:perf_network
Jul 24, 2026
Merged

perf(network): Reduce loop complexity for transport messages and remove unused static variables#2659
xezon merged 4 commits into
TheSuperHackers:mainfrom
Caball009:perf_network

Conversation

@Caball009

@Caball009 Caball009 commented Apr 28, 2026

Copy link
Copy Markdown

This PR makes modest performance improvements:

  1. Improves loop logic in a couple of places (early breaks and keep track of the current index to avoid restarting inner loops).
  2. Removes unused static variables.

See commits for cleaner diffs.

TODO:

  • Verify everything works ok.

@Caball009 Caball009 added Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Network Anything related to network, servers labels Apr 28, 2026
@Caball009 Caball009 changed the title perf(network): Improve performancy by reducing packet allocations and improving loop logic perf(network): Improve performance by reducing packet allocations and improving loop logic Apr 28, 2026
@Caball009
Caball009 force-pushed the perf_network branch 2 times, most recently from 7ebd8f1 to a2267a1 Compare April 28, 2026 20:41
@stephanmeesters

Copy link
Copy Markdown

I think it would be nice if we can change the signature of NetPacket from class NetPacket : public MemoryPoolObject to struct NetPacket. The memory pooling right now doesn't really get used, especially with your change? By the looks of it this would mostly need changes around NetPacket::ConstructBigCommandPacketList

@Caball009

Copy link
Copy Markdown
Author

I think it would be nice if we can change the signature of NetPacket from class NetPacket : public MemoryPoolObject to struct NetPacket. The memory pooling right now doesn't really get used, especially with your change? By the looks of it this would mostly need changes around NetPacket::ConstructBigCommandPacketList

Perhaps I can just put the relevant logic from Connection::sendNetCommandMsg in a callback and pass that to NetPacket::ConstructBigCommandPacketList.

@Caball009

Copy link
Copy Markdown
Author

I think it would be nice if we can change the signature of NetPacket from class NetPacket : public MemoryPoolObject to struct NetPacket. The memory pooling right now doesn't really get used, especially with your change? By the looks of it this would mostly need changes around NetPacket::ConstructBigCommandPacketList

I think it's best to do this in a separate pull request because it involves another set of changes.

@Caball009
Caball009 force-pushed the perf_network branch 2 times, most recently from 3fe1916 to d4f75e9 Compare June 2, 2026 20:59
@Caball009
Caball009 marked this pull request as ready for review July 9, 2026 18:59
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown

Greptile Summary

This PR makes modest but sound performance improvements across the networking layer: it removes two unreferenced static counters (numPackets, numCommands) from ConnectionManager::doRelay(), and refactors several buffer-scanning loops to add an early break on the first empty slot and to carry a bufferIndex forward across iterations rather than restarting from slot 0 every time.

  • Early-break optimisation (ConnectionManager, LANAPI, NAT): adds else { break; } when m_inBuffer[i].length == 0, relying on the invariant that the buffer is always packed contiguously. This invariant holds because doRecv() fills from slot 0 forward (via bufferIndex that starts at 0 each call) and all consumers clear slots sequentially from slot 0, so the first empty slot is always the boundary of unprocessed data.
  • bufferIndex carry-forward (Transport::doRecv, Transport::doSend latency path): replaces restarting the inner scan from i=0 for each packet with a single monotonically-advancing index, reducing worst-case work from O(N²) to O(N) across a batch of packets.
  • MAX_MESSAGESARRAY_SIZE and intsize_t for loop variables: minor modernisation; the existing DEBUG_ASSERTCRASH(bufferIndex < MAX_MESSAGES, ...) (previously commented out) is now active, providing a debug-build warning when the buffer is full.

Confidence Score: 5/5

Safe to merge. The changes are a focused optimisation to loop traversal; all correctness-critical invariants hold.

The early-break optimisation is valid because doRecv() always fills m_inBuffer contiguously from slot 0 using a bufferIndex that resets at the start of each call, and all consumers clear slots sequentially from slot 0, preserving a packed-buffer layout. The bufferIndex carry-forward in doRecv() and the doSend() latency path is correct for the same reason.

No files require special attention. Transport.cpp has the most structural change but is correct for all practical configurations.

Important Files Changed

Filename Overview
Core/GameEngine/Source/GameNetwork/Transport.cpp Most complex change: introduces shared bufferIndex in doRecv() and doSend() to avoid restarting inner scans from 0; also activates the previously-commented-out DEBUG_ASSERTCRASH for buffer-full detection. Logic is correct given m_inBuffer is always contiguously packed.
Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp Removes dead static counters, refactors while-loop-with-manual-increment to for-loops, and adds early break on first empty inBuffer slot.
Core/GameEngine/Source/GameNetwork/LANAPI.cpp Switches loop bounds from MAX_MESSAGES to ARRAY_SIZE, preserves the !LANbuttonPushed guard, and adds early break. All LAN message types fall through to m_inBuffer[i].length = 0 outside the switch, maintaining the packed-buffer invariant.
Core/GameEngine/Source/GameNetwork/NAT.cpp Adds early break and switches to ARRAY_SIZE. PROBE and KEEPALIVE packets are always cleared; unrecognised packets are not cleared (pre-existing behaviour) but remain filled and thus never trigger the early break.

Reviews (5): Last reviewed commit: "Used '>' and '<=' instead of '==' and '!..." | Re-trigger Greptile

Comment thread Core/GameEngine/Source/GameNetwork/LANAPI.cpp Outdated
@Caball009 Caball009 changed the title perf(network): Improve performance by reducing packet allocations and improving loop logic perf(network): Reduce packet allocations and improve loop logic Jul 9, 2026

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This pull does a number of things.

Comment thread Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp Outdated
Comment thread Core/GameEngine/Include/GameNetwork/NetPacket.h Outdated
Comment thread Core/GameEngine/Source/GameNetwork/NetPacket.cpp Outdated
Comment thread Core/GameEngine/Source/GameNetwork/NetPacket.cpp Outdated
Comment thread Core/GameEngine/Source/GameNetwork/NAT.cpp
Comment thread Core/GameEngine/Source/GameNetwork/LANAPI.cpp
Comment thread Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@Caball009

Copy link
Copy Markdown
Author

This pull does a number of things.

I can put the loop logic in another PR and put #2866 in here to replace all memory pooled uses of NetPacket (either with a single dynamic allocation or stack allocation if possible).

@xezon

xezon commented Jul 12, 2026

Copy link
Copy Markdown

Whatever makes most sense.

@Caball009
Caball009 marked this pull request as draft July 13, 2026 13:13
@Caball009 Caball009 changed the title perf(network): Reduce packet allocations and improve loop logic perf(network): Improve performance by refactoring loop logic and removing unused static variables Jul 13, 2026
@Caball009
Caball009 marked this pull request as ready for review July 13, 2026 15:13

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Makes sense as far as I can tell.

@Caball009

Caball009 commented Jul 24, 2026

Copy link
Copy Markdown
Author

Rebased.

I noticed no issues in my testing. Can be merged as far as I'm concerned.

@xezon xezon changed the title perf(network): Improve performance by refactoring loop logic and removing unused static variables perf(network): Reduce loop complexity for transport messages and remove unused static variables Jul 24, 2026
@xezon
xezon merged commit 810371b into TheSuperHackers:main Jul 24, 2026
17 checks passed
@Caball009
Caball009 deleted the perf_network branch July 24, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Performance Is a performance concern

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants