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 in-place mutation of NIOLoopBoundBox.value #2771

Merged
merged 2 commits into from
Jul 8, 2024

Conversation

dnadoba
Copy link
Member

@dnadoba dnadoba commented Jul 7, 2024

Motivation:

NIOLoopBound and NIOLoopBoundBox should be lightweight wrappers and support in-place mutation of CoW (Copy on Write) types.

Modifications:

  • replace set accessor with _modify accessor
  • add test that verifies in-place mutation of CoW types

Result:

Less copying

@dnadoba dnadoba requested a review from weissi July 7, 2024 14:00
@@ -47,9 +47,9 @@ public struct NIOLoopBound<Value>: @unchecked Sendable {
self._eventLoop.preconditionInEventLoop()
return self._value
}
set {
_modify {
Copy link
Member

Choose a reason for hiding this comment

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

@Lukasa What's the current position on _modify, is that now allowed? Alternative is obvs to make a withMutating { inout ... }?

Copy link
Member

Choose a reason for hiding this comment

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

We have used _modify and _read in multiple places now for performance. I think at this point it sadly is allowed to be used.

Copy link
Member

Choose a reason for hiding this comment

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

fine by me

Copy link
Member Author

@dnadoba dnadoba Jul 8, 2024

Choose a reason for hiding this comment

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

I have completely switched to use _modify by default if the property type can be a CoW type.
Combining this with consume self if the property isn't simply a stored property and I can implement efficient computed properties without worrying about triggering CoW. The CoWValue test helper then makes sure this actually checks out and stays like this.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't think that's a good development. There are still open questions about _modify which is why it's underscored. And _modify is probably less benign than other underscored stuff like @inline(__always) or @_cdecl. The precise semantics haven't been settled and things may change.

Anybody aware of a post-2022 effort of getting these accessors specified?

Copy link
Member

Choose a reason for hiding this comment

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

The concern is that it's an underscored (i.e. not ready for prime time) feature whose semantics aren't fully specified.

The standard library is developed and shipped together with the compiler itself which makes it different. Example: Before Swift had a defined memory model, the standard library pretended there to be a defined memory.

Regarding swift-collections: That's up to the Swift collection authors.

Concerns regarding _modify: It's very easy to go wrong. For example if you have this code

_modify {
   print("A")
   yield &self._something
   print("B")
}

then you will not always see B after you saw an A. It depends if there was a throw in the mutation. This can leave your type in an inconsistent (possibly memory-unsafe) state and is arguably hard to see (there's no try or place to catch). Sure, if the yield is the last line in a _modify then it's probably fine.

But fundamentally, we can just offer a func withMutableValue { inout Value ... } which can achieve the same as _modify (worse syntax ofc).


This is just a decision for the NIO team. They maintain the code base, so they need to be comfortable with the amount of undefined, underscored stuff that's in the code base at present. New compiler versions could adjust things for underscored features.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is fair and matches my understanding of the risk. It's a trade off, similar to the use of unsafe or unchecked swift features. The pitch explains this as well. Are there any other known issues today?

possibly memory-unsafe

@weissi is that possible without additional use of unsafe swift?


Agree, thats up to the NIO team.

Copy link
Member

Choose a reason for hiding this comment

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

possibly memory-unsafe
@weissi is that possible without additional use of unsafe swift?

I would assume so because compiler-emitted code could be skipped too (unless it protects against that). The key point is: Without an _modify implementations checked against the current compiler invocations we don't really know.

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 personally okay with using _modify since it is used by the compiler and standard library developers outside the stdlib e.g. swift-collections. It makes the code that developers write fast without them having to discover a modify method. Additionally, we are using more underscored attributes such as @_exported or @_alwaysEmitIntoClient.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're using @_alwaysEmitIntoClient we should definitely rethink that too.

@_exported is underscored, but its behaviour is well understood and safe. _modify is definitely not that.

As you know, the compiler and standard library developers explicitly do not make the same compatibility promises we do. That allows them to freely use these annotations, as they can always resolve issues by dropping all prior Swift versions. We can't do that, so if _modify introduces issues, we'll be forced to fork or revert the change.

@dnadoba dnadoba merged commit e947421 into apple:main Jul 8, 2024
15 of 16 checks passed
@dnadoba dnadoba deleted the dn-loop-box-in-place-mutation branch July 8, 2024 14:19
chkp-aviads added a commit to chkp-aviads/swift-nio that referenced this pull request Jul 21, 2024
* apple/main: (26 commits)
  [GHA] Only format Swift files that are in Git index (apple#2797)
  Ignore format commit from git blame (apple#2796)
  Adopt swift-format (apple#2794)
  Disable warnings as errors on Swift 6 and main (apple#2793)
  ChannelHandler: provide static (un)wrap(In|Out)bound(In|Out) (apple#2791)
  Add manual control to NIOLockedValueBox (apple#2786)
  [GHA] Cxx interoperability compatibility and integration tests check (apple#2790)
  [GHA] Introduce reusable matrix workflow (apple#2789)
  Fix benchmark thresholds update script (apple#2783)
  [GHA] Broken symlink and format check (apple#2787)
  [GHA] Add license header check (apple#2781)
  Improved documentation for HTTP Parts to clarify how often each part is received (apple#2775)
  [GHA] Download the scripts to make workflow reusable (apple#2785)
  Combine the two NIOAsyncChannel channel handlers (apple#2779)
  [GHA] Benchmark job (apple#2780)
  [GHA] Move docs check to script (apple#2776)
  Add benchmark for creating NIOAsyncChannel (apple#2774)
  Avoid creating a yield ID counter per async writer (apple#2768)
  [GHA] Unacceptable language check (apple#2766)
  Allow in-place mutation of `NIOLoopBoundBox.value` (apple#2771)
  ...

# Conflicts:
#	Sources/NIOPosix/BSDSocketAPICommon.swift
#	Sources/NIOPosix/GetaddrinfoResolver.swift
#	Sources/NIOPosix/HappyEyeballs.swift
@glbrntt glbrntt added the semver/patch No public API change. label Jul 24, 2024
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Aug 19, 2024
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [apple/swift-nio](https://togithub.com/apple/swift-nio) | minor |
`2.68.0` -> `2.70.0` |

---

### Release Notes

<details>
<summary>apple/swift-nio (apple/swift-nio)</summary>

###
[`v2.70.0`](https://togithub.com/apple/swift-nio/releases/tag/2.70.0):
SwiftNIO 2.70.0

[Compare
Source](https://togithub.com/apple/swift-nio/compare/2.69.0...2.70.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### SemVer Minor

- `FileSystem.copyItem` can parallelise directory copy by
[@&#8203;UncleMattHope](https://togithub.com/UncleMattHope) in
[https://github.com/apple/swift-nio/pull/2806](https://togithub.com/apple/swift-nio/pull/2806)
- `ChannelOption`: Allow types to be accessed with leading dot syntax by
[@&#8203;ayush1794](https://togithub.com/ayush1794) in
[https://github.com/apple/swift-nio/pull/2816](https://togithub.com/apple/swift-nio/pull/2816)
- Make `EventLoopPromise` conform to Equatable by
[@&#8203;gjcairo](https://togithub.com/gjcairo) in
[https://github.com/apple/swift-nio/pull/2714](https://togithub.com/apple/swift-nio/pull/2714)
- Provide a default `CopyStrategy` overload for copyItem. by
[@&#8203;UncleMattHope](https://togithub.com/UncleMattHope) in
[https://github.com/apple/swift-nio/pull/2818](https://togithub.com/apple/swift-nio/pull/2818)

##### SemVer Patch

- Better align shutdown semantics of testing event loops by
[@&#8203;simonjbeaumont](https://togithub.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2800](https://togithub.com/apple/swift-nio/pull/2800)
- Clone files on Darwin rather than copying them by
[@&#8203;rnro](https://togithub.com/rnro) in
[https://github.com/apple/swift-nio/pull/2823](https://togithub.com/apple/swift-nio/pull/2823)

##### Other Changes

- Fix compose file used in update-benchmark-thresholds script by
[@&#8203;simonjbeaumont](https://togithub.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2808](https://togithub.com/apple/swift-nio/pull/2808)
- Remove advice to generate linux tests. by
[@&#8203;PeterAdams-A](https://togithub.com/PeterAdams-A) in
[https://github.com/apple/swift-nio/pull/2807](https://togithub.com/apple/swift-nio/pull/2807)
- Make `testInstantTCPConnectionResetThrowsError` more reliable by
[@&#8203;hamzahrmalik](https://togithub.com/hamzahrmalik) in
[https://github.com/apple/swift-nio/pull/2810](https://togithub.com/apple/swift-nio/pull/2810)
- \[CI] Add `shellcheck` and fix up warnings by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2809](https://togithub.com/apple/swift-nio/pull/2809)
- \[CI] Fix docs check by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2811](https://togithub.com/apple/swift-nio/pull/2811)
- \[CI] Add Swift 6 language mode workflow by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2812](https://togithub.com/apple/swift-nio/pull/2812)
- Fix test compilation on non-macOS Darwin platforms by
[@&#8203;simonjbeaumont](https://togithub.com/simonjbeaumont) in
[https://github.com/apple/swift-nio/pull/2817](https://togithub.com/apple/swift-nio/pull/2817)
- Add `.index-build` to `.gitignore` by
[@&#8203;MaxDesiatov](https://togithub.com/MaxDesiatov) in
[https://github.com/apple/swift-nio/pull/2819](https://togithub.com/apple/swift-nio/pull/2819)
- \[CI] Add action and workflow to check for semver label by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2814](https://togithub.com/apple/swift-nio/pull/2814)
- Update repository docs for swift-version support and recent CI check
changes by [@&#8203;UncleMattHope](https://togithub.com/UncleMattHope)
in
[https://github.com/apple/swift-nio/pull/2815](https://togithub.com/apple/swift-nio/pull/2815)
- Fix failing build for test by
[@&#8203;ayush1794](https://togithub.com/ayush1794) in
[https://github.com/apple/swift-nio/pull/2824](https://togithub.com/apple/swift-nio/pull/2824)
- Fix typo in comment in `WebSocketErrorCodes.swift` by
[@&#8203;valeriyvan](https://togithub.com/valeriyvan) in
[https://github.com/apple/swift-nio/pull/2604](https://togithub.com/apple/swift-nio/pull/2604)
- \[CI] Add a scheduled workflow for tests and benchmarks by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2822](https://togithub.com/apple/swift-nio/pull/2822)
- \[CI] Fix label check by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2827](https://togithub.com/apple/swift-nio/pull/2827)

#### New Contributors

- [@&#8203;UncleMattHope](https://togithub.com/UncleMattHope) made their
first contribution in
[https://github.com/apple/swift-nio/pull/2806](https://togithub.com/apple/swift-nio/pull/2806)
- [@&#8203;ayush1794](https://togithub.com/ayush1794) made their first
contribution in
[https://github.com/apple/swift-nio/pull/2816](https://togithub.com/apple/swift-nio/pull/2816)
- [@&#8203;valeriyvan](https://togithub.com/valeriyvan) made their first
contribution in
[https://github.com/apple/swift-nio/pull/2604](https://togithub.com/apple/swift-nio/pull/2604)

**Full Changelog**:
apple/swift-nio@2.69.0...2.70.0

###
[`v2.69.0`](https://togithub.com/apple/swift-nio/releases/tag/2.69.0):
SwiftNIO 2.69.0

[Compare
Source](https://togithub.com/apple/swift-nio/compare/2.68.0...2.69.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### SemVer Minor

- Add manual control to `NIOLockedValueBox` by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2786](https://togithub.com/apple/swift-nio/pull/2786)
- ChannelHandler: provide static `(un)wrap(In|Out)bound(In|Out)` by
[@&#8203;weissi](https://togithub.com/weissi) in
[https://github.com/apple/swift-nio/pull/2791](https://togithub.com/apple/swift-nio/pull/2791)

##### SemVer Patch

- Pre-box some errors to reduce allocations by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2765](https://togithub.com/apple/swift-nio/pull/2765)
- Allow in-place mutation of `NIOLoopBoundBox.value` by
[@&#8203;dnadoba](https://togithub.com/dnadoba) in
[https://github.com/apple/swift-nio/pull/2771](https://togithub.com/apple/swift-nio/pull/2771)
- Avoid creating a yield ID counter per async writer by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2768](https://togithub.com/apple/swift-nio/pull/2768)
- Combine the two `NIOAsyncChannel` channel handlers by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2779](https://togithub.com/apple/swift-nio/pull/2779)
- Use the new Android overlay and Bionic module from Swift 6 by
[@&#8203;finagolfin](https://togithub.com/finagolfin) in
[https://github.com/apple/swift-nio/pull/2784](https://togithub.com/apple/swift-nio/pull/2784)
- Change `unsafeDownCast` to `as!` by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2802](https://togithub.com/apple/swift-nio/pull/2802)

##### Other Changes

- CI migration to GitHub Action by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
([https://github.com/apple/swift-nio/pull/2760](https://togithub.com/apple/swift-nio/pull/2760)
[https://github.com/apple/swift-nio/pull/2762](https://togithub.com/apple/swift-nio/pull/2762)
[https://github.com/apple/swift-nio/pull/2763](https://togithub.com/apple/swift-nio/pull/2763)
[https://github.com/apple/swift-nio/pull/2764](https://togithub.com/apple/swift-nio/pull/2764)
[https://github.com/apple/swift-nio/pull/2767](https://togithub.com/apple/swift-nio/pull/2767)
[https://github.com/apple/swift-nio/pull/2766](https://togithub.com/apple/swift-nio/pull/2766)
[https://github.com/apple/swift-nio/pull/2776](https://togithub.com/apple/swift-nio/pull/2776)
[https://github.com/apple/swift-nio/pull/2780](https://togithub.com/apple/swift-nio/pull/2780)
[https://github.com/apple/swift-nio/pull/2785](https://togithub.com/apple/swift-nio/pull/2785)
[https://github.com/apple/swift-nio/pull/2781](https://togithub.com/apple/swift-nio/pull/2781)
[https://github.com/apple/swift-nio/pull/2787](https://togithub.com/apple/swift-nio/pull/2787)
[https://github.com/apple/swift-nio/pull/2783](https://togithub.com/apple/swift-nio/pull/2783)
[https://github.com/apple/swift-nio/pull/2789](https://togithub.com/apple/swift-nio/pull/2789)
[https://github.com/apple/swift-nio/pull/2790](https://togithub.com/apple/swift-nio/pull/2790))
- Ignore format commit from git blame by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2796](https://togithub.com/apple/swift-nio/pull/2796)
[https://github.com/apple/swift-nio/pull/2797](https://togithub.com/apple/swift-nio/pull/2797)
[https://github.com/apple/swift-nio/pull/2801](https://togithub.com/apple/swift-nio/pull/2801)
[https://github.com/apple/swift-nio/pull/2803](https://togithub.com/apple/swift-nio/pull/2803)
- Adopt swift-format by
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2794](https://togithub.com/apple/swift-nio/pull/2794)
- `HTTPPart` Documentation Clarification by
[@&#8203;dimitribouniol](https://togithub.com/dimitribouniol) in
[https://github.com/apple/swift-nio/pull/2775](https://togithub.com/apple/swift-nio/pull/2775)
- Add benchmark for creating `NIOAsyncChannel` by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2774](https://togithub.com/apple/swift-nio/pull/2774)
- Disable warnings as errors on Swift 6 and main by
[@&#8203;glbrntt](https://togithub.com/glbrntt) in
[https://github.com/apple/swift-nio/pull/2793](https://togithub.com/apple/swift-nio/pull/2793)

**Full Changelog**:
apple/swift-nio@2.68.0...2.69.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xOC4xIiwidXBkYXRlZEluVmVyIjoiMzguMzkuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543+cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
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.

5 participants