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

Avoid creating a yield ID counter per async writer #2768

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/NIOCore/AsyncSequences/NIOAsyncWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import DequeModule
import NIOConcurrencyHelpers
import _NIODataStructures

@usableFromInline
let yieldIDCounter = ManagedAtomic<UInt64>(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

This would probably be nicer as a static, but this is otherwise reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed but wasn't sure where to put it. The obvious place is NIOAsyncWriter but that's generic so can't have a static let.

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if I agree with this move. I understand that we want to reduce allocations but IMO the correct move forward is to replace SwiftAtomics.ManagedAtomic with Synchronisation.Atomic which should avoid the allocation as well since it is ~Copyable. We ought to be able to do this conditionally when the compiler is Swift 6. Though availability might be tricky. If availability blocks us we can move this into a global. I would suggest renaming though to scope it better e.g. let _asyncWriterYieldIDCounter or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree re: using Synchronization.Atomic but as noted, the availability makes this difficult. I've renamed the global though.


/// The delegate of the ``NIOAsyncWriter``. It is the consumer of the yielded writes to the ``NIOAsyncWriter``.
/// Furthermore, the delegate gets informed when the ``NIOAsyncWriter`` terminated.
///
Expand Down Expand Up @@ -434,14 +437,11 @@ extension NIOAsyncWriter {
}
}

@usableFromInline
/* private */ internal let _yieldIDCounter = ManagedAtomic<UInt64>(0)

@inlinable
func generateUniqueYieldID() -> YieldID {
// Using relaxed is fine here since we do not need any strict ordering just a
// unique ID for every yield.
.init(value: self._yieldIDCounter.loadThenWrappingIncrement(ordering: .relaxed))
.init(value: yieldIDCounter.loadThenWrappingIncrement(ordering: .relaxed))
}
}

Expand Down