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

Call NIOAsyncWriterSinkDelegate outside of the lock #2547

Merged
merged 3 commits into from
Oct 12, 2023

Conversation

FranzBusch
Copy link
Member

Motivation

The current NIOAsyncWriter implementation expects that the delegate is called while holding the lock to avoid reentrancy issues. However, this prevents us from executing the delegate calls directly on the EventLoop if we are on it already.

Modification

This moves all of the delegate calls outside of the locks and adds protection against reentrancy into the state machine.

Result

Less allocations.

delegate: delegate
)

return .callDidYield(delegate)
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens to sequence in this branch? It looks like we just ignore it

Copy link
Member Author

Choose a reason for hiding this comment

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

I see where the confusion is coming from. We are just yielding it to the delegate right away when we return callDidYield. The caller of this method still has the sequence.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, gotcha, thanks for clarifying.

# Motivation
The current `NIOAsyncWriter` implementation expects that the delegate is called while holding the lock to avoid reentrancy issues. However, this prevents us from executing the delegate calls directly on the `EventLoop` if we are on it already.

# Modification
This moves all of the delegate calls outside of the locks and adds protection against reentrancy into the state machine.

# Result
Less allocations.

Clarify the reentrancy problems in docs and protect against them in the writer
@FranzBusch FranzBusch force-pushed the fb-async-writer-performance branch from cde1274 to 378e01f Compare October 11, 2023 21:03
@FranzBusch FranzBusch added the 🔨 semver/patch No public API change. label Oct 11, 2023
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

LGTM, nice improvement.

@FranzBusch FranzBusch enabled auto-merge (squash) October 12, 2023 15:01
@FranzBusch FranzBusch merged commit 2910d6b into apple:main Oct 12, 2023
@FranzBusch FranzBusch deleted the fb-async-writer-performance branch October 12, 2023 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants