-
Notifications
You must be signed in to change notification settings - Fork 48
Perf: Use string.utf8 field for conversion to data #20
Conversation
(I was confused first, you say |
I have the feeling that Summary: If P.S.: This can turn into another longish discussion, but technically HTTP/1.1 header encoding is ISO-8859-1, not UTF-8. Another reason why I would recommend to keep known headers as structured values, not strings (and transfer unknown ones as octet values instead of Strings). (Having said that, using UTF-8 is common ignorance^D^D^D^D^D practice though, so we may get away w/ that.) |
I agree that Using You're right about raising a bug against Foundation - Phillipe Hausler is aware, and I'lll raise it with Ben Cohen as well. The My normal approach would be that, as this is internal implementation that's not exposed, I'd take the performance improvement on the table, and then improve again in the future if there's scope to do so. |
Yes, sure. I guess my thinking was that it makes more sense to just add this to Foundation func data(using encoding:...) {
if encoding == .utf8 { return Data(self.utf8) }
... old code ...
} ... instead of doing a local workaround, as this benefits all String users. P.S.: In the end yours is a micro optimisation which should be kinda dwarfed by the overhead in keeping the headers as regular strings ... but I like such stuff nevertheless ;-) |
Just for the record my benchmarks actually show different behavior than what you were seeing. However there is actually an even more interesting optimization we can do that gets a 10x or more win that I posted as a sample in the swift bug. This is a very reduced case (without the ability of allowing lossy conversion) https://swift.sandbox.bluemix.net/#/repl/596656240524853a4527b2cb |
It's needed after PR swift-server#55 and I want to see if editing this will trigger a new CI build.
OK, so my quick attempt to fix the merge conflict failed. I'll pull it into Xcode and rework it. |
@carlbrown I'm not sure its worth the effort - work is being done to improve this in Foundation itself, so at the moment this is kind of a placeholder/reminder to track that. It should probably be an Issue... |
Converted to issue #97 |
The implementation of String to Data conversion in HTTPStreamingParser.swift uses:
which does encoding based on the passed in encoding type. If we're only using utf8 encoding, String already contains a utf8 field that contains the UTF8 encoded version, so its more performant to use that directly (up to 2x faster on a concurrent microbench). This PR converts the usage to:
When we add support for requested encoding, we should keep this as a fast path and use
String.data(using:)
for the slow path.