Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While loop without a delay starves CPU time in background inside the coroutine's DefaultDispatcher thread, competing with Client thread and causing horrible lag inside dungeons on machines with low amount of CPU cores (<=4)
Add a delay(50) to fix it.
While mainly affecting people with low amount of CPU cores, it will also unnecessarily stall a core and prevent any other tasks from running on that core even on CPUs with lots of cores, just that it's effect on the FPS will be lower.
Screenshots
Image 1 Htop output without the PR:
Image 2 Htop output with the PR (Default dispatcher is not even in the list when I sort by CPU usage):
Image 3 Htop output if i find DefaultDispatcher thread in the list with the PR (Uses 0-1% cpu):
Image 4 Spark report without the PR:
Explanation: The isNotEmpty in kotlin gets compiled to a negated !isEmpty() call. isEmpty() in ConcurrentLinkedQueue calls first() and returns true if it's not null (source: https://github.com/openjdk/jdk8u/blob/master/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java#L428) inside the while loop. There is no delay in the entire while loop since all methods called (isNotEmpty(), poll(), sendPacketAsync()) are non-blocking instant-finishing methods (poll() just returns null if no element is available - the method that blocks till element is available is take(), but it's only available in BlockingLinkedQueue, not ConcurrentLinkedQueue. An alternative fix was to make it BlockingLinkedQueue and call take(), but using blocking methods like take() inside coroutines would prevent another coroutine from using the shared thread so this fix is more feasible.).