Re: [PATCH v2 08/19] grep.c: Use index api
- From
Duy Nguyen <pclouds@gmail.com>
- Date
- Jul 14, 2013, 03:32 UTC
- Message-ID
- <CACsJy8CMqJ5TF1V7CMTQjnAUp=zXPYxotq5uLqvaF0Sikzv+uw@mail.gmail.com>
- In-Reply-To
- <1373650024-3001-9-git-send-email-t.gummerer@gmail.com>
On Sat, Jul 13, 2013 at 12:26 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
Show 40 quoted lines
> +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..
Show 19 quoted lines
> @@ -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.
-- Duy