3 ms·
So * Swift's boolean -> integer conversion is oddly slow (should probably be reported) * allocations are expensive (duh) * Converting arbitrary binary data t
by masklinn 11y ago
So
* Swift's boolean -> integer conversion is oddly slow (should probably be reported)
* allocations are expensive (duh)
* Converting arbitrary binary data to a string is deceptively simple which is obvious coming from C or C++ but possibly less so coming from higher-level languages:
> String conversion. If I use byte array to string conversion I move from 0.35ms to 1774.73ms. And if I do what the test needs to do (get the length of the string “s.utf8.count”), I am at 2737.4ms. Which is an additional second spend doing factually nothing.
Except it's not going nothing, it has to
* allocate a buffer for the string (possibly multiple times depending how reservation works by default) (and specifically for Swift there's the potential additional issue NSString bridging)
* validate that the input is decodable and possibly transcode to whatever the internal encoding is if it's not UTF-8
* iterate the string's codepoints and sum the number of UTF8 bytes necessary to encode that codepoints
That's a shit-ton of work compared to doing literally nothing if you just check the number of bytes in the original array.