Nim v1.2 and Zero Copy

Allocating and copying memory once isn’t slow, but when in a loop, or anything else that leads to millions+ of iterations you will find that performance degrades. Zero copy is the technique of referring to memory, or a segment of memory, without making a copy of it.

One example is a string slice where you want to view/compare only a portion of the string. Another example is reading a block of memory from a database or socket, and referring to fields within that block without making a copy each time.

The benefit of using zero copy techniques is that it can lead to huge gains in performance. The downside is that it can lead to bugs that are difficult to track down. For example if the original memory being referenced is freed but you continue to use the reference, or if the reference points to the wrong memory address.

C and C++ are two languages where referencing memory by using pointers is very common. Tools such as valgrind can help to identify memory related bugs, but not in all cases.

Right now Nim (v1.2) only supports this technique by use of the openarray proc. However work is ongoing to support better functionality for zero copy, see:

Related links: [RFC] View/slice #12 and let/var/openArray inside objects/containers #178.

Leave a Reply

Your email address will not be published. Required fields are marked *