@matheusbsbs / matheus-bagliano / commits / cd1464083c
Fork 0

gitweb: Use git-for-each-ref to generate list of heads and/or tags

Add two subroutines: git_get_heads_list and git_get_refs_list, which fill out needed parts of refs info (heads and tags respectively) info using single call to git-for-each-ref, instead of using git-peek-remote to get list of references and using parse_ref for each ref to get ref info, which in turn uses at least one call of git command. Replace call to git_get_refs_list in git_summary by call to git_get_references, git_get_heads_list and git_get_tags_list (simplifying this subroutine a bit). Use git_get_heads_list in git_heads and git_get_tags_list in git_tags. Modify git_tags_body slightly to accept output from git_get_tags_list. Remove no longer used, and a bit hackish, git_get_refs_list. parse_ref is no longer used, but is left for now. Generating "summary" and "tags" views should be much faster for projects which have large number of tags. CHANGES IN OUTPUT: Before, if ref in refs/tags was tag pointing to commit we used committer epoch as epoch for ref, and used tagger epoch as epoch only for tag pointing to object of other type. If ref in refs/tags was commit, we used committer epoch as epoch for ref (see parse_ref; we sorted in gitweb by 'epoch' field). Currently we use committer epoch for refs pointing to commit objects, and tagger epoch for refs pointing to tag object, even if tag points to commit. Simple ab benchmark before and after this patch for my git.git repository (git/jnareb-git.git) with some heads and tags added as compared to git.git repository, shows around 2.4-3.0 times speedup for "summary" and "tags" views: summary 3134 +/- 24.2 ms --> 1081 +/- 30.2 ms tags 2886 +/- 18.9 ms --> 1196 +/- 15.6 ms Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Nov 2, 2006 at 20:23 UTC cd1464083cd9e61a55148c21fd9b8eb3bf76d190
1 file changed +92 -61
gitweb/gitweb.perl
+92 -61
@@ -1294,47 +1294,88 @@ sub parse_ls_tree_line ($;%) {
1294 ## ......................................................................
1295 ## parse to array of hashes functions
1296
1297 -sub git_get_refs_list {
1298 - my $type = shift || "";
1299 - my %refs;
1300 - my @reflist;
1301 -
1302 - my @refs;
1303 - open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1297 +sub git_get_heads_list {
1298 + my $limit = shift;
1299 + my @headslist;
1300 +
1301 + open my $fd, '-|', git_cmd(), 'for-each-ref',
1302 + ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1303 + '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1304 + 'refs/heads'
1305 or return;
1306 while (my $line = <$fd>) {
1306 - chomp $line;
1307 - if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1308 - if (defined $refs{$1}) {
1309 - push @{$refs{$1}}, $2;
1310 - } else {
1311 - $refs{$1} = [ $2 ];
1312 - }
1307 + my %ref_item;
1308
1314 - if (! $4) { # unpeeled, direct reference
1315 - push @refs, { hash => $1, name => $3 }; # without type
1316 - } elsif ($3 eq $refs[-1]{'name'}) {
1317 - # most likely a tag is followed by its peeled
1318 - # (deref) one, and when that happens we know the
1319 - # previous one was of type 'tag'.
1320 - $refs[-1]{'type'} = "tag";
1321 - }
1309 + chomp $line;
1310 + my ($refinfo, $committerinfo) = split(/\0/, $line);
1311 + my ($hash, $name, $title) = split(' ', $refinfo, 3);
1312 + my ($committer, $epoch, $tz) =
1313 + ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1314 + $name =~ s!^refs/heads/!!;
1315 +
1316 + $ref_item{'name'} = $name;
1317 + $ref_item{'id'} = $hash;
1318 + $ref_item{'title'} = $title || '(no commit message)';
1319 + $ref_item{'epoch'} = $epoch;
1320 + if ($epoch) {
1321 + $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1322 + } else {
1323 + $ref_item{'age'} = "unknown";
1324 }
1325 +
1326 + push @headslist, \%ref_item;
1327 }
1328 close $fd;
1329
1326 - foreach my $ref (@refs) {
1327 - my $ref_file = $ref->{'name'};
1328 - my $ref_id = $ref->{'hash'};
1330 + return wantarray ? @headslist : \@headslist;
1331 +}
1332 +
1333 +sub git_get_tags_list {
1334 + my $limit = shift;
1335 + my @tagslist;
1336
1330 - my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1331 - my %ref_item = parse_ref($ref_file, $ref_id, $type);
1337 + open my $fd, '-|', git_cmd(), 'for-each-ref',
1338 + ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1339 + '--format=%(objectname) %(objecttype) %(refname) '.
1340 + '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1341 + 'refs/tags'
1342 + or return;
1343 + while (my $line = <$fd>) {
1344 + my %ref_item;
1345
1333 - push @reflist, \%ref_item;
1346 + chomp $line;
1347 + my ($refinfo, $creatorinfo) = split(/\0/, $line);
1348 + my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1349 + my ($creator, $epoch, $tz) =
1350 + ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1351 + $name =~ s!^refs/tags/!!;
1352 +
1353 + $ref_item{'type'} = $type;
1354 + $ref_item{'id'} = $id;
1355 + $ref_item{'name'} = $name;
1356 + if ($type eq "tag") {
1357 + $ref_item{'subject'} = $title;
1358 + $ref_item{'reftype'} = $reftype;
1359 + $ref_item{'refid'} = $refid;
1360 + } else {
1361 + $ref_item{'reftype'} = $type;
1362 + $ref_item{'refid'} = $id;
1363 + }
1364 +
1365 + if ($type eq "tag" || $type eq "commit") {
1366 + $ref_item{'epoch'} = $epoch;
1367 + if ($epoch) {
1368 + $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1369 + } else {
1370 + $ref_item{'age'} = "unknown";
1371 + }
1372 + }
1373 +
1374 + push @tagslist, \%ref_item;
1375 }
1335 - # sort refs by age
1336 - @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1337 - return (\@reflist, \%refs);
1376 + close $fd;
1377 +
1378 + return wantarray ? @tagslist : \@tagslist;
1379 }
1380
1381 ## ----------------------------------------------------------------------
@@ -2264,8 +2305,7 @@ sub git_tags_body {
2305 for (my $i = $from; $i <= $to; $i++) {
2306 my $entry = $taglist->[$i];
2307 my %tag = %$entry;
2267 - my $comment_lines = $tag{'comment'};
2268 - my $comment = shift @$comment_lines;
2308 + my $comment = $tag{'subject'};
2309 my $comment_short;
2310 if (defined $comment) {
2311 $comment_short = chop_str($comment, 30, 5);
@@ -2298,7 +2338,7 @@ sub git_tags_body {
2338 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2339 if ($tag{'reftype'} eq "commit") {
2340 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2301 - " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2341 + " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2342 } elsif ($tag{'reftype'} eq "blob") {
2343 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2344 }
@@ -2323,23 +2363,23 @@ sub git_heads_body {
2363 my $alternate = 1;
2364 for (my $i = $from; $i <= $to; $i++) {
2365 my $entry = $headlist->[$i];
2326 - my %tag = %$entry;
2327 - my $curr = $tag{'id'} eq $head;
2366 + my %ref = %$entry;
2367 + my $curr = $ref{'id'} eq $head;
2368 if ($alternate) {
2369 print "<tr class=\"dark\">\n";
2370 } else {
2371 print "<tr class=\"light\">\n";
2372 }
2373 $alternate ^= 1;
2334 - print "<td><i>$tag{'age'}</i></td>\n" .
2335 - ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2336 - $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2337 - -class => "list name"},esc_html($tag{'name'})) .
2374 + print "<td><i>$ref{'age'}</i></td>\n" .
2375 + ($curr ? "<td class=\"current_head\">" : "<td>") .
2376 + $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2377 + -class => "list name"},esc_html($ref{'name'})) .
2378 "</td>\n" .
2379 "<td class=\"link\">" .
2340 - $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2341 - $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2342 - $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2380 + $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2381 + $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2382 + $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2383 "</td>\n" .
2384 "</tr>";
2385 }
@@ -2489,18 +2529,9 @@ sub git_summary {
2529
2530 my $owner = git_get_project_owner($project);
2531
2492 - my ($reflist, $refs) = git_get_refs_list();
2493 -
2494 - my @taglist;
2495 - my @headlist;
2496 - foreach my $ref (@$reflist) {
2497 - if ($ref->{'name'} =~ s!^heads/!!) {
2498 - push @headlist, $ref;
2499 - } else {
2500 - $ref->{'name'} =~ s!^tags/!!;
2501 - push @taglist, $ref;
2502 - }
2503 - }
2532 + my $refs = git_get_references();
2533 + my @taglist = git_get_tags_list(15);
2534 + my @headlist = git_get_heads_list(15);
2535
2536 git_header_html();
2537 git_print_page_nav('summary','', $head);
@@ -2792,9 +2823,9 @@ sub git_tags {
2823 git_print_page_nav('','', $head,undef,$head);
2824 git_print_header_div('summary', $project);
2825
2795 - my ($taglist) = git_get_refs_list("tags");
2796 - if (@$taglist) {
2797 - git_tags_body($taglist);
2826 + my @tagslist = git_get_tags_list();
2827 + if (@tagslist) {
2828 + git_tags_body(\@tagslist);
2829 }
2830 git_footer_html();
2831 }
@@ -2805,9 +2836,9 @@ sub git_heads {
2836 git_print_page_nav('','', $head,undef,$head);
2837 git_print_header_div('summary', $project);
2838
2808 - my ($headlist) = git_get_refs_list("heads");
2809 - if (@$headlist) {
2810 - git_heads_body($headlist, $head);
2839 + my @headslist = git_get_heads_list();
2840 + if (@headslist) {
2841 + git_heads_body(\@headslist, $head);
2842 }
2843 git_footer_html();
2844 }