Performance optimization of MM script