Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow async chunkGenerator + improve types #141

Closed
wants to merge 3 commits into from

Conversation

zardoy
Copy link
Contributor

@zardoy zardoy commented Mar 25, 2024

want to make optimizations by putting chunk generator into workers so it doesn't slow down main thread of flying squid

@extremeheat
Copy link
Member

One thing to keep in mind that JavaScript will substantially slow if you pollute the event loop with lots of things, which can happen with use of async in hot places (as this results in creation of intermediate Promise's with callbacks). It creates lots of GC overhead, and creates processing delays for rest of code due to scheduling.

Using async also won't make the code faster/non-blocking, it will in fact block and slow the code even more because that code ultimately will have to run at some point on the current thread, and it'll happen over a longer period of time.

@extremeheat
Copy link
Member

extremeheat commented Mar 25, 2024

As we did in bedrock-protocol, you can greatly improve performance and throughput by removing async/Promise/timer use everywhere you can (for hot code).

If you need to block for long periods of time, doing it in a worker thread and sending results back in form of an event that triggers another synchronous function inside main thread will minimize the overhead.

@zardoy
Copy link
Contributor Author

zardoy commented Mar 25, 2024

One thing to keep in mind that JavaScript will substantially slow if you pollute the event loop with lots of things, which can happen with use of async in hot places (as this results in creation of intermediate Promise's with callbacks). It creates lots of GC overhead, and creates processing delays for rest of code due to scheduling.

Yes but isn't it still better than synchronous code which will just block your event loop completely?

So just as you said I want to move chunk generator into worker and that's why I need this function to be async, I don't want to move prismarine-world into worker instead as by doing just the generator async we can also access already generated chunks while some other chunks are still in process. WDYT? Sorry I missed something

@extremeheat
Copy link
Member

extremeheat commented Mar 25, 2024

I don't have objection to PR to be clear. What I mean is I'd recommend moving away from async world completely if you are targeting performance.

Yes but isn't it still better than synchronous code which will just block your event loop completely?

As mention, you block the event loop completely either way. And because of the overhead, you slow down and use more memory by doing async as opposed to blocking at once. In bedrock-protocol for example we've achieved 400 concurrent bots in one process by removing as much async operations as possible, because event loop yielding => congestion is pretty much the most expensive overhead you can get in JS.

So:

  • JIT optimizations can't usually happen for code that yields to event loop midway (unpredictable code flow)
  • memory usage is greater because functions have to be suspended, more memory has to be copied
  • lots of intermediate garbage creation
  • timers are slow because you have to wait on the OS scheduler
  • big operations take even more time

If you just want better perf without changing too much code I'd look into why the current chunk generator is slow and how you can speed it up. Likely you can optimize it by removing memory allocations/copies.

@zardoy zardoy closed this Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants