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

[TO BE DISCUSSED] make generic/closure ByteBuffer methods inlineable #266

Merged
merged 3 commits into from
Apr 9, 2018

Conversation

weissi
Copy link
Member

@weissi weissi commented Apr 2, 2018

I'm not 100% sure I did exactly the right thing and if we should to this. Paging @airspeedswift and @moiseev .

Motivation:

Generics and closures across modules can be problematic as the cause
allocations. ByteBuffer is one of NIO's core data types and therefore
should be well optimised. Generic/closure taking methods so far were a
bit of a problem. And with
SE-0193
accepted, there's not much reason not to use those features even though they
are (at the moment) @_.

For the 1000 requests over 1 connection allocation benchmark we go from

"total_allocations": 325378

to

"total_allocations": 321366

so we lose 4 allocations per HTTP request/response pair, not super
impressive but not nothing either.

Modifications:

Make generic/closure taking methods on ByteBuffer @_inlineable

Result:

Fewer allocations

@weissi weissi requested review from normanmaurer and Lukasa April 2, 2018 10:05
@weissi weissi changed the title make generic/closure ByteBuffer methods inlineable [TO BE DISCUSSED] make generic/closure ByteBuffer methods inlineable Apr 2, 2018
Copy link
Member

@normanmaurer normanmaurer left a comment

Choose a reason for hiding this comment

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

LGTM... the @_versioned is needed to allow inlining of internal methods ?

@weissi
Copy link
Member Author

weissi commented Apr 2, 2018

on Linux with Swift 4.0.3 we go from

"total_allocations": 322051

to

"total_allocations": 318047

so also lost 4 allocations. But I reckon @normanmaurer's HTTP1 optimisations should lower that number much more significantly.

@weissi
Copy link
Member Author

weissi commented Apr 2, 2018

@normanmaurer the actual name for @_versioned will (AFAIK) be @usableFromInline, another discussed name was @abiPublic. And yes, basically it means that a function (even if internal) can be used from other modules if inlined. I had to turn a make of previously private functions internal
as @_versioned can only be attached to internal functions. We could think about prefixing their name with an underscore so that we don't use them by accident in the NIO module.

That change does make ByteBuffers internal public ABI (not API) but that's okay as I don't foresee any ByteBuffer layout changes before we go SemVer major++ and also on the server we don't care about binary frameworks so YOLO.

@moiseev
Copy link

moiseev commented Apr 2, 2018

@_versioned things can also be @_inlineable. So you might be able to squeeze some more improvement from it.

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.

@moiseev @airspeedswift Is the plan to remove the underscore-prefixed spellings of @inlineable and @usableFromInline once SE-193 lands? Or will those continue to compile?

}

private func toIndex(_ value: Int) -> Index {
return Index(truncatingIfNeeded: value)
@_versioned func toIndex(_ value: Int) -> ByteBuffer.Index {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do make the @_versioned properties underscore-prefixed.

Motivation:

Generics and closures across modules can be problematic as the cause
allocations. ByteBuffer is one of NIO's core data types and therefore
should be well optimised. Generic/closure taking methods so far were a
bit of a problem. And with
[SE-0193](https://github.com/apple/swift-evolution/blob/master/proposals/0193-cross-module-inlining-and-specialization.md)
accepted, there's not much reason to use those features even though they
are (at the moment) @_.

For the 1000 requests over 1 connection allocation benchmark we go from

    "total_allocations": 325378

to

    "total_allocations": 321366

so we lose 4 allocations per HTTP request/response pair, not super
impressive but not nothing either.

Modifications:

Make generic/closure taking methods on ByteBuffer `@_inlineable`

Result:

Fewer allocations
@weissi weissi force-pushed the jw-make-bb-inlineable branch from ecad819 to 74906b2 Compare April 3, 2018 08:46
@weissi
Copy link
Member Author

weissi commented Apr 3, 2018

thanks @moiseev . Looks better now?

@weissi weissi added the 🔨 semver/patch No public API change. label Apr 3, 2018
@moiseev
Copy link

moiseev commented Apr 3, 2018

@Lukasa the changes have landed in master. So master branch of Swift right now supports both the old and the new spelling. There are no concrete plans to port it to the 4.2 branch right away. So in order to be compatible, for now, it is safe to stick to the old form of these attributes.

@moiseev
Copy link

moiseev commented Apr 3, 2018

@weissi It's not a matter of how it looks =) I commented primarily because it might enable some other optimizations.

@Lukasa
Copy link
Contributor

Lukasa commented Apr 4, 2018

@moiseev Sorry, I should be clearer. Is there any plan to drop these old names, e.g. in Swift 4.2? I'm very reluctant to ship code that we have reason to believe will break unnecessarily in a future release if we can possibly avoid it.

@weissi
Copy link
Member Author

weissi commented Apr 4, 2018

@Lukasa I think what @moiseev is saying that 4.2 will only have the old names. Swift 5 will probably have old + new.

@normanmaurer
Copy link
Member

@Lukasa WDYT? I think its "ok" to merge

@Lukasa
Copy link
Contributor

Lukasa commented Apr 9, 2018

I don’t think @moiseev has fully answered my question, so I’m still a bit nervous about merging this.

@weissi
Copy link
Member Author

weissi commented Apr 9, 2018

@Lukasa I think we're fine: https://twitter.com/slava_pestov/status/982997370659983360 . Slava implemented this AFAIK

@Lukasa
Copy link
Contributor

Lukasa commented Apr 9, 2018

Awesome, thanks. Lemme re-review with that in mind.

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.

@normanmaurer normanmaurer merged commit 1506a19 into apple:master Apr 9, 2018
@normanmaurer normanmaurer added this to the 1.4.0 milestone Apr 9, 2018
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.

4 participants