Skip to content

Commit

Permalink
dir.c: get rid of lazy initialization
Browse files Browse the repository at this point in the history
Remove the "Lazy initialization" in prep_exclude() left behind by
aceb942 (prep_exclude: remove the artificial PATH_MAX limit,
2014-07-14). Let's instead have the one remaining caller that didn't
go through the DIR_INIT macro I added in ce93a4c (dir.[ch]:
replace dir_init() with DIR_INIT, 2021-07-01) use a new
unpack_trees_init() function instead.

We could continue to pre-grow the buffer here to something like
PATH_MAX, but that we were using PATH_MAX at all is just a relic from
this being a fixed buffer from way back in f87f949 (git-ls-files:
--exclude mechanism updates., 2005-07-24). I don't think something
that's about to do I/O on one file here needs to be concerned about
micro-optimizing the allocation pattern.

It would arguably make more sense to not do this in
unpack_trees_start() in "merge-recursive.c", but to instead do it when
we do the "CALLOC_ARRAY(opt->priv)" in "merge_start()", or in turn to
have "init_merge_options()" do that allocation itself.

But as "merge-recursive.c" is going away sooner than later let's just
do the bare minimum here, hopefully we'll be able to remove this
sooner than later, and perhaps our new "unpack_trees_init()" along
with that removal. The "merge-ort.c" allocation pattern is (of course)
closer to the end-state that would make senes for "merge-recursive.c"
in this case.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  • Loading branch information
avar committed Oct 1, 2021
1 parent 6461d6a commit 5a18133
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
8 changes: 0 additions & 8 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -1550,14 +1550,6 @@ static void prep_exclude(struct dir_struct *dir,
if (dir->pattern)
return;

/*
* Lazy initialization. All call sites currently just
* memset(dir, 0, sizeof(*dir)) before use. Changing all of
* them seems lots of work for little benefit.
*/
if (!dir->basebuf.buf)
strbuf_init(&dir->basebuf, PATH_MAX);

/* Read from the parent directories and push them down. */
current = stk ? stk->baselen : -1;
strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
Expand Down
4 changes: 3 additions & 1 deletion dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ struct dir_struct {
unsigned visited_directories;
};

#define DIR_INIT { 0 }
#define DIR_INIT { \
.basebuf = STRBUF_INIT, \
}

struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);

Expand Down
1 change: 1 addition & 0 deletions unpack-trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct unpack_trees_options {
struct checkout_metadata meta;
};
#define UNPACK_TREES_OPTIONS_INIT { \
.msgs_to_free = STRVEC_INIT, \
.dir = DIR_INIT, \
}

Expand Down

0 comments on commit 5a18133

Please sign in to comment.