As part of my work on GitHub - swift-dns/swift-endpoint: A highly-optimized library containing types representing an endpoint, such as DomainName and IPv4/v6Address · GitHub parts of which might soon make it to a swiftlang repo under supervision of the Networking workgroup [related post], I'm trying to make the ip-related implementations even more performant than the overkill they already are.
Those who have worked on performance or studied it a bit know that performance is not always as straightforward as just "make things faster" and there are other considerations that should also be taken into account, some of which are hard to even catch in benchmarks.
One of these considerations is code size.
This brings me to my point, are there any ways for me to express "@inline(always) but not when -Osize"?
Because I do want to ask the compiler to "inline this please", but it's not so critical as to be required to inline when somebody is trying to prefer smaller binary sizes over more performance.
The changes are in this PR:
Specifically, the makeDescription call is now being duplicated while makeDescription (The one that contains the actual impl) is marked as @inline(always). This is for creating RFC 5952-compliant descriptions for an IPv6Address:
/// Intentional branchy code around all `mustUseMixedNotation`s.
/// `mustUseMixedNotation` is often `false` so it will be a well-predicted branch.
if mustUseMixedNotation {
return try unsafe self.makeDescription(
encloseInSquareBrackets: encloseInSquareBrackets,
mustUseMixedNotation: true,
writingToUnsafeMutableBufferPointerOfUInt8:
writingToUnsafeMutableBufferPointerOfUInt8
)
} else {
return try unsafe self.makeDescription(
encloseInSquareBrackets: encloseInSquareBrackets,
mustUseMixedNotation: false,
writingToUnsafeMutableBufferPointerOfUInt8:
writingToUnsafeMutableBufferPointerOfUInt8
)
}
This is so the code is inlined at compile time and mustUseMixedNotation branches are eliminated in the function that is being called.
In this example, I don't really care if the code is inlined or not if the user is asking for size-optimized code. But I don't see a way to express that in Swift, today.
Is this a shortcoming? If yes, what are the feasible approaches to overcome this? Or do you think I should be doing stuff in a different way?