Thursday, February 1, 2018

C String Functions in Swift

Helge Heß:

The pointer you get back points into a buffer, which doesn’t exist anymore. That is why you need to be super careful when accessing C APIs.


When using C API with Swift Strings (be it a simple puts or maybe libxml2), be aware that such calls are really expensive (a malloc+free per call). If you want to do this a lot, you may want to convert Strings to UTF-8 unsafe buffers very early on, and use those.

Is this Swift behaviour reasonable? For a high level language I would say yes. Yet Swift also claims to be useful for system and server programming, and in such scenarios it is really hard to access standard Swift types in a performance sensible way (do zero copy, avoid allocs, etc).

Comments RSS · Twitter

Leave a Comment