ArchipelagoMW / Archipelago.MultiClient.Net

A client library for use with .NET based prog-langs for interfacing with Archipelago hosts.
MIT License
13 stars 10 forks source link

Use BlockingCollection instead of ConcurrentQueue. #79

Closed dariof4 closed 1 year ago

dariof4 commented 1 year ago

Use BlockingCollection instead of ConcurrentQueue in ArchipelagoSocketHelper_system.net.websockets.cs to fix high cpu usage due to continuosly polling for packets to send.

This was tested on a toy implementation and it seems to work just fine, it would be appreciated if someone tried this in an actual session though.

This is how much cpu a sample console app that just connects uses. without pr: 2023-05-29_00-27 with pr: check

There's one thing I noticed with this change, if multiple packets are continuously added with SendMultiplePacketsAsync very fast they may get sent within a single message to the socket instead of multiple, since more packets are added asynchronously to sendQueue while the while loop is already running and adding packets to packetList. I don't think this has any adverse effect though.

Also, the check to see if packetList is empty shouldn't be needed anymore (since the method blocks on sendQueue.Take() if senQueue is empty), but I wasn't sure so I kept it.