main: improved performance of various memory-related functions.