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

feat: support Atomics.waitAsync #687

Open
wants to merge 108 commits into
base: current
Choose a base branch
from
Open

Conversation

metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Oct 30, 2024

  • feat: Support Atomics.waitAsync

metcoder95 and others added 30 commits September 13, 2023 12:59
… 6.12.0 (#453)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@metcoder95 metcoder95 changed the base branch from current to feat/custom_balancer October 30, 2024 10:29
@metcoder95 metcoder95 requested a review from ronag October 30, 2024 10:29
@metcoder95
Copy link
Member Author

Adding test-case is pending

src/worker.ts Outdated
// @ts-expect-error
const { async, value } = Atomics.waitAsync(sharedBuffer, kRequestCountField, lastSeenRequestCount);

return async === true && value;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is this awaited?

Copy link
Member Author

Choose a reason for hiding this comment

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

Here and here.

They're a IIFE that has a catch to handle the errors; it is not awaited explicitly to avoid wrapping a non-promise into a Promise, nevertheless we might be loosing some stack out of the stacktrace. although as it is executed in a single place it might not be that bad but happy to hear your thoughts

Copy link
Collaborator

Choose a reason for hiding this comment

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

I still don't see how it's awaited... Maybe to complicated for me to understand

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe I didn't get what you meant at the first time, but think I might get it now. At a top-level is not really awaited, but more on a fire-and-forget approach, so the Promise is not awaited for resolution but triggered and any error is handled.

This happens on every new message or at a startup

Copy link
Collaborator

Choose a reason for hiding this comment

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

That doesn't sound good? Though I don't know enough of the current design to truly know.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, while writing this, I noticed the same; let me see if there's any side-effect on awaiting on it.
I don't see a potential problem as anyways we already await for the task handler to resolve the task, so it should come for free; but let me check and benchmark

Copy link
Member Author

Choose a reason for hiding this comment

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

Refactored into awaiting for the wake up; although the benchmarks requires some re-work, the refactor reflected a nice performance boost

src/worker.ts Show resolved Hide resolved
ronag
ronag previously approved these changes Nov 4, 2024
returning control to the main thread (avoid having open handles within a thread). If still want to have the possibility
of having open handles or handle asynchrnous tasks, you can set the environment variable `PISCINA_ENABLE_ASYNC_ATOMICS` to `1` or setting `options.atomics` to `async`.

> **Note**: The `async` mode comes with performance penalties and can lead to undesired behaviour if open handles are not tracked correctly.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain the open handle thing? Why is that an issue with async?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly as we are not doing event loop ticks, these can add some overhead if the workers start doing more than it should.

Even waiting for just the worker to wake up added some ticks that showed a difference of ±5% which is really negligible.

I've not strong opinion in this one; but if we want to make this the default behaviour we should document that it is important to not keep the workers doing too much background tasks as they can face some penalty (as well if using async, implementer needs to account for the teardown of the worker when idle, i.e. no more tasks scheduled)

Copy link
Member Author

Choose a reason for hiding this comment

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

If that sounds good, I can adjust the PR for that 👍

@ronag
Copy link
Collaborator

ronag commented Nov 4, 2024

I would recommend making async the default for a future semver major. IMHO it's the best balance between perf and expected behavior.

Base automatically changed from feat/custom_balancer to current November 5, 2024 07:49
@metcoder95 metcoder95 dismissed ronag’s stale review November 5, 2024 07:49

The base branch was changed.

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.

7 participants