-
Notifications
You must be signed in to change notification settings - Fork 42
DispatchData vs Data for API calls #37
Comments
Thanks a lot for that, I should have done this in the first place.
Yes, it is unfortunate that If the goal of this effort is not to produce a very fast or scalable server (and I think we can put a checkmark on this) but one which is fits in best and is very easy to use, I'd say lets just stick to
That should be a non-reason. I think very few people care what BlueSocket does. Just drop it and use something else. It is very easy to build a simple HTTP/1.x server using just DispatchIO and the HTTP parser which is already in. Maybe Johannes can just contribute the Apple one? The harder part may be the TLS support, I'm not sure what the status of that is wrt async APIs. Do we have something here? (I was suggested a DispatchIO compatible TLS API, do we have sth like that already?).
Oh no, please don't do this! Just keep them as static Data or DispatchData objects. A String is a pretty hefty object, don't just create it for below-ASCII stuff like that :-)
That goes along my comment wrt the goal of the project. Yes, if scalability is of no concern, just return Strings and Datas. Summary of my opinion: If this thing is going to be something like Python P.S.: It is trivial to just add a |
That's my plan - I just haven't had time for that, yet.
I know. But pretty much every tutorial I've ever seen for any Swift webserver starts with
What I don't know is: what's the overhead of that? I need to measure it and see, I just haven't had time. If it turns out that the conversion doesn't slow anything down noticeably, then we can just exclusively use |
You don't need to measure it. W/o Foundation support the overhead is significant (full malloc/memcpy). W/ Foundation support it would probably be zero (ARC). If you implement it in plain Swift, you would need to malloc and copy the data as neither String nor Data give you APIs to the underlying storage. In case you expect a lot of string to data conversions, Data should be a lot better for many setups because it likely does have direct Foundation support (I think there was another PR by @seabaylea where we discussed this). Consider String.data(using:). If the encoding matches the underlying String storage, no copying needs to happen. The Data object returned can/could just retain the string and be a view into that String buffer. |
I'm pulling comment from @helje5 from #28 over into this issue, because that issue is getting cluttered.
For what it's worth, I started with
DispatchData
in the APIs, and I wanted to keep it that way. The problem is that, at every turn, I found myself having to convert fromData
/NSData
toDispatchData
and back. For example:BlueSocket
(that I really want to replace with something much simpler) explicitly reads/writes all socket data inNSData
format."OK\r\n"
, that string easily converts to aData
, but getting it into aDispatchData
is more of a pain in the ass, and when you have several things (like(String($0.count, radix: 16) + "\r\n").data(using: .utf8)
) that you are sticking into the same packet, you end up doing that conversion over and over.String
(Because the programmer typed it in to be turned into a response), or encoded as aData
(Because it's the contents of a file pulled off disk or it's the result of aJSONEncoder
call). In both of those cases, it was easier not to have to do a bunch ofData
<->DispatchData
conversions before handing off tohttp
.So you tell me - What do you think the right answer is here?
[Just in case there's any confusion as to my tone here - I'm not being defensive - I honestly want to know. I'm not thrilled with the compromise I made, and I'd love for there to be an obvious or a consensus choice.]
The text was updated successfully, but these errors were encountered: