improve the efficiency of TemplateMachine