unpack-trees: fix accidentally quadratic behavior

While unpacking trees (e.g. during git checkout), when we hit a cache entry that's past and outside our path, we cut off iteration. This provides about a 45% speedup on git checkout between master and master^20000 on Twitter's monorepo. Speedup in general will depend on repostitory structure, number of changes, and packfile packing decisions. Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Jan 22, 2016 at 14:58 UTC a6720955f19ea10bf9569d04480deed25b1bccf7
1 file changed +12 -1
unpack-trees.c
+12 -1
@@ -695,8 +695,19 @@ static int find_cache_pos(struct traverse_info *info,
695 ++o->cache_bottom;
696 continue;
697 }
698 - if (!ce_in_traverse_path(ce, info))
698 + if (!ce_in_traverse_path(ce, info)) {
699 + /*
700 + * Check if we can skip future cache checks
701 + * (because we're already past all possible
702 + * entries in the traverse path).
703 + */
704 + if (info->traverse_path) {
705 + if (strncmp(ce->name, info->traverse_path,
706 + info->pathlen) > 0)
707 + break;
708 + }
709 continue;
710 + }
711 ce_name = ce->name + pfxlen;
712 ce_slash = strchr(ce_name, '/');
713 if (ce_slash)