DeyaEldeen DeyaEldeen - 1 year ago 97
C Question

wrapping a library like facebook's ZStandard in swift?

Facebook created an opensource fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios, called ZStandard.


I have been looking for a tutorial that describes the c to swift wrapping, like this, but looks not comprehensive enough, what are the prerequests do I have to know before writing a wrapper?

  • when I finish writing it, I will also make it open source.

Answer Source

Thank you for a good question. I've looked at the library and played with it, it seems pretty interesting.

I would say you need to be comfortable using the ZSTD C library in a C program. You need to be also comfortable programming in Swift. Depending on the parts of the API you want to wrap, you may need to understand how to deal with raw memory in Swift (Unsafe... types).

Some challenges to consider when wrapping ZSTD:

  • The streaming API with dictionaries is experimental and subject to change, yet use of dictionaries is one of the advantages of ZSTD.
  • When dealing with memory buffers, we want to minimize copying them, since the buffers may be quite large and copying them would adversely affect performance. This, of course, complicates memory management.

There is a multitude of approaches you can choose from when writing a wrapper. For example, you can write wrappers in C that will expose simple APIs you will wrap in Swift. You can include C code in your wrapper framework, or you can keep it in separate C libraries.

I have come up with a simple example of a wrapper around a small subset of the API, you can take a look at

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download