To reduce locking time and to be able to efficiently write large messages, "writer" and "reader" objects could be useful. A writer object then could be used to incrementally build up a larger message without locking. Then if the message is complete it could be send with one invocation and very short locking time. The writer object manages it's own memory and therefore the number of mallocs are reduced (once the writer has the right memory size, no more mallocs are needed). The reader object would serve the same purpose for the other direction: you could read a larger message from a buffer into the reader with short locking time and then incrementally get all the Lua values out of the reader without locking.