Wednesday, January 31, 2018

Swift Imports Fixed-size C Arrays As Tuples

Ole Begemann:

The Swift equivalent of the C type float[4] would be (Float, Float, Float, Float). This has the benefit of incurring no bridging overhead because the Swift compiler can lay out tuples in a C-compatible way.


Swift’s Clang importer sees the char[256] declarations and turns them into 256-tuples(!).


I did a quick test: my version of _FixedArray256 (approximately 1,600 lines of generated Swift code) takes about 8 seconds to compile and adds nearly 750 KB to the compiled binary (with optimizations enabled). The convenience doesn’t come for free.


Instead, let’s just take the idea of obtaining a pointer to the tuple’s storage and using that to initialize a string.

Comments RSS · Twitter

Leave a Comment