I would recommend rewriting as c structs. I know that it will be a pain, but it will not only write to disk quickly, but also work much better.
Before anyone gets upset, I'm not saying that people should always use structures, but there are situations where it is really better for performance. Especially if you pre-allocate your memory in about 20k contiguous blocks at a time (with pointers to the block), instead of creating / allocating many small pieces in a repeating loop.
those. if your loop constantly selects objects, it slows down. If you have pre-allocated 1000 structures and just have an array of pointers (or one pointer), this will be a large value faster.
(I had situations where even my desktop was too slow, and it did not have enough memory to cope with the fact that millions of objects are created in a row)
Jacob source share