Subject: Re: [PATCH v2 08/19] grep.c: Use index api

From: Thomas Gummerer <[email-masked]>
Date: Mon, 15 Jul 2013 11:51:39 +0200

Duy Nguyen <[email-masked]> writes:

> On Sat, Jul 13, 2013 at 12:26 AM, Thomas Gummerer <[email-masked]> wrote:
>> +static int grep_cache(struct cache_entry *ce, void *cb_data)
>>  {
>> -       int hit = 0;
>> -       int nr;
>> -       read_cache();
>> +       struct grep_opts *opts = cb_data;
>>
>> -       for (nr = 0; nr < active_nr; nr++) {
>> -               struct cache_entry *ce = active_cache[nr];
>> -               if (!S_ISREG(ce->ce_mode))
>> -                       continue;
>> -               if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
>> -                       continue;
>> -               /*
>> -                * If CE_VALID is on, we assume worktree file and its cache entry
>> -                * are identical, even if worktree file has been modified, so use
>> -                * cache version instead
>> -                */
>> -               if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
>> -                       if (ce_stage(ce))
>> -                               continue;
>> -                       hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
>> -               }
>> -               else
>> -                       hit |= grep_file(opt, ce->name);
>> -               if (ce_stage(ce)) {
>> -                       do {
>> -                               nr++;
>> -                       } while (nr < active_nr &&
>> -                                !strcmp(ce->name, active_cache[nr]->name));
>> -                       nr--; /* compensate for loop control */
>> -               }
>> -               if (hit && opt->status_only)
>> -                       break;
>> -       }
>> -       return hit;
>> +       if (!S_ISREG(ce->ce_mode))
>> +               return 0;
>> +       if (!match_pathspec_depth(opts->pathspec, ce->name, ce_namelen(ce), 0, NULL))
>> +               return 0;
>
> You do a match_pathspec_depth here..
>
>> @@ -895,10 +887,21 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>>         } else if (0 <= opt_exclude) {
>>                 die(_("--[no-]exclude-standard cannot be used for tracked contents."));
>>         } else if (!list.nr) {
>> +               struct grep_opts opts;
>> +               struct filter_opts *filter_opts = xmalloc(sizeof(*filter_opts));
>> +
>>                 if (!cached)
>>                         setup_work_tree();
>>
>> -               hit = grep_cache(&opt, &pathspec, cached);
>> +               memset(filter_opts, 0, sizeof(*filter_opts));
>> +               filter_opts->pathspec = &pathspec;
>> +               opts.opt = &opt;
>> +               opts.pathspec = &pathspec;
>> +               opts.cached = cached;
>> +               opts.hit = 0;
>> +               read_cache_filtered(filter_opts);
>> +               for_each_cache_entry(grep_cache, &opts);
>
> And here again inside for_each_cache_entry. In the worst case that
> could turn into 2 expensive fnmatch instead of one. Is this conversion
> worth it? Note that match_pathspec is just a deprecated version of
> match_pathspec_depth. They basically do the same thing.

Right, the match_pathspec_depth should in builtin/grep.c should be
removed, it's unnecessary when using for_each_index_entry.  Thanks for
spotting it.  Other than that I still think the change makes sense.