GameNetworkingSockets icon indicating copy to clipboard operation
GameNetworkingSockets copied to clipboard

Provide API to pool `CSteamNetworkingMessage`

Open copyrat90 opened this issue 9 months ago • 3 comments

It seems that CSteamNetworkingMessage doesn't pool messages. This means that for each call to ISteamNetworkingUtils::AllocateMessage(), it dynamically allocates 264 bytes structure (on 64-bit environments). This could be slow if you need to send loads of messages frequently.

I tried providing my own version of AllocateMessage() that allocates a message from my own pool, and initialize the CSteamNetworkingMessage properly. But I realized you can't do that, because there's no way to initialize CSteamNetworkingMessage's private fields: m_links & m_linksSecondaryQueue (Well, technically, I can just memset(msg, 0, 264) to initialize the hidden fields... But not only that's UB, I can't tell if Steamworks SDK version of the message uses the same CSteamNetworkingMessage under the hood.)

It would be nice if there's an way to pool the message structure itself with the exposed m_pfnRelease, like you can with m_pfnFreeData for the payloads.

Also, it would be great if you can also pool the message structure for the received messages too.

copyrat90 avatar Mar 31 '25 13:03 copyrat90

Looks like the Steamworks SDK version uses the same CSteamNetworkingMessage internally. (Not 100% sure, but at least in the steam_api64.dll, there's the assembly code to initialize the m_links & m_linksSecondaryQueue.)

This is pretty hacky, but I can just copy-paste the CSteamNetworkingMessage declaration to my library for now.

copyrat90 avatar Apr 04 '25 07:04 copyrat90

Use SetCustomMemoryAllocator

Alluseri avatar May 23 '25 12:05 Alluseri

Use SetCustomMemoryAllocator

That would set a global allocator for GNS, not specific to the CSteamNetworkingMessage pooling.

Also, that function is only available on the standalone GNS; It is not available on the Steamworks SDK.

copyrat90 avatar May 27 '25 01:05 copyrat90