Optimize a resource-constrained algorithm to run efficiently on an embedded or local client device for Tonal without exhausting memory.