10 thousand times faster Swift
medium.com
10 thousand times faster Swift
1–10 of 37 posts
Re: 10 thousand times faster Swift
#2Re: 10 thousand times faster Swift
#3Use C or C++ instead?
Re: 10 thousand times faster Swift
#4* 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.
Re: 10 thousand times faster Swift
#5 for _ in 0..
Looking at the assembly... the call to `flatuseStruct` is moved outside the loop in Release builds. You're only measuring 1 thousand iterations of `flatuseStruct`, not 1 million.Your red flag should have been this:
> One million times decoding of a small object graph took 0.35ms
That's literally impossible. That's doing 2.8 billion iterations per second. A single function call generally takes 2 nanoseconds (you can't do 1 billion per second).
Re: 10 thousand times faster Swift
#6Re: 10 thousand times faster Swift
#7Re: 10 thousand times faster Swift
#8Bad news: the optimizer is moving your functions outside the loop. for _ in 0.. Looking at the assembly... the call to `flatuseStruct` is moved outside the loop in Release builds. You're only measuring 1 thousand iterations of `flatuseStruct`, not 1 million. Your red flag should have been this: > One million times decoding of a small object graph took 0.35ms That's literally impossible. That's doing 2.8 billion itera…
Re: 10 thousand times faster Swift
#9Bad news: the optimizer is moving your functions outside the loop. for _ in 0.. Looking at the assembly... the call to `flatuseStruct` is moved outside the loop in Release builds. You're only measuring 1 thousand iterations of `flatuseStruct`, not 1 million. Your red flag should have been this: > One million times decoding of a small object graph took 0.35ms That's literally impossible. That's doing 2.8 billion itera…
https://gist.github.com/mzaks/e3a2dc7ccdfc2397bc26c55eb6dc8a...
the output is now:
Eager run
=================================
1557 ms encode
264 ms decode
34 ms use
206 ms dealloc
504 ms decode+use+dealloc
0,38 ms direct
0,32 ms using struct
=================================
Total counter1 is 8644311667000000
Total counter2 is 8644311667000000
Total counter3 is 8644311667000000
Encoded size is 315 bytes, should be 344 if not using unique strings
=================================
As you can see all three counters are equal.Re: 10 thousand times faster Swift
#10Bad news: the optimizer is moving your functions outside the loop. for _ in 0.. Looking at the assembly... the call to `flatuseStruct` is moved outside the loop in Release builds. You're only measuring 1 thousand iterations of `flatuseStruct`, not 1 million. Your red flag should have been this: > One million times decoding of a small object graph took 0.35ms That's literally impossible. That's doing 2.8 billion itera…