Speed and memory improvement for populating StringIO