Explain how you would optimize a log-parsing script that is running out of memory on extremely large datasets.