How to rebuild mesh in optimized way? Example inside
There isn't one.  Because you created individual blocks, even when they're joined they're still independent meshes. Tools like dissolve faces only work on completely connected meshes.