Last commit from ancestors algorithm optimization