I'm not sure about this but, isn't more optimal just calling X.pack_stream.pack just once instead of several times? (Just create one buffer instead of multiple buffers). On the other hand, even if it's better, the improvement could be irrelevant