Enhance the performance of `lock` utility function