Skip to content

Conversation

@Lukasa
Copy link
Contributor

@Lukasa Lukasa commented Nov 27, 2020

Motivation:

I recently discovered that UnsafeRawBufferPointer.init(rebasing:) is
surprisingly expensive, with 7 traps and 11 branches. A simple
replacement can make it a lot cheaper, down to two traps and four
branches. This ends up having pretty drastic effects on
ByteBuffer-heavy NIO code, which often outlines the call to that
initializer and loses the ability to make a bunch of site-local
optimisations.

While this has been potentially fixed upstream with
swiftlang/swift#34879, there is no good reason to
wait until Swift 5.4 for this improvement.

Due to the niche use-case, I didn't bother doing this for every
rebasing in the program. In particular, there is at least one
UnsafeBufferPointer(rebasing:) that I didn't do this with, and there are
uses in both NIOTLS and NIOHTTP1 that I didn't change. While we can fix
those if we really need to, it would be nice to avoid this helper
proliferating too far through our codebase.

Modifications:

  • Replaced the use of URBP.init(rebasing:) with a custom hand-rolled
    version that avoids Slice.count.

Result:

Cheaper code. One NIOHTTP2 benchmark sees a 2.9% speedup from this
change alone.

Motivation:

I recently discovered that UnsafeRawBufferPointer.init(rebasing:) is
surprisingly expensive, with 7 traps and 11 branches. A simple
replacement can make it a lot cheaper, down to two traps and four
branches. This ends up having pretty drastic effects on
ByteBuffer-heavy NIO code, which often outlines the call to that
initializer and loses the ability to make a bunch of site-local
optimisations.

While this has been potentially fixed upstream with
swiftlang/swift#34879, there is no good reason to
wait until Swift 5.4 for this improvement.

Due to the niche use-case, I didn't bother doing this for _every_
rebasing in the program. In particular, there is at least one
UnsafeBufferPointer(rebasing:) that I didn't do this with, and there are
uses in both NIOTLS and NIOHTTP1 that I didn't change. While we can fix
those if we really need to, it would be nice to avoid this helper
proliferating too far through our codebase.

Modifications:

- Replaced the use of URBP.init(rebasing:) with a custom hand-rolled
  version that avoids Slice.count.

Result:

Cheaper code. One NIOHTTP2 benchmark sees a 2.9% speedup from this
change alone.
@glbrntt glbrntt merged commit 4bf379b into apple:main Nov 30, 2020
Lukasa added a commit to Lukasa/swift-nio that referenced this pull request Jan 7, 2021
clintonpi added a commit to clintonpi/swift-nio that referenced this pull request Dec 3, 2024
Motivation:

UnsafeRawBufferPointer.init(fastRebase:) and UnsafeMutableRawBufferPointer.init(fastRebase:) were shimmed into NIO in apple#1696. The shim is no longer necessay.

Modifications:

- Revert the use of the `fastRebase` inits to the native `.init(rebasing:)`.

Result:

Use of native APIs instead.
glbrntt pushed a commit that referenced this pull request Dec 3, 2024
Motivation:

`UnsafeRawBufferPointer.init(fastRebase:)` and
`UnsafeMutableRawBufferPointer.init(fastRebase:)` were shimmed into NIO
in #1696. The shim is no longer
necessary.

Modifications:

- Revert the use of the `fastRebase` inits to the native
`.init(rebasing:)`.

Result:

Use of native APIs instead.
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.

2 participants