Skip to content

Commit 6a0b0b6

Browse files
pcloudsgitster
authored andcommitted
tree.c: update read_tree_recursive callback to pass strbuf as base
This allows the callback to use 'base' as a temporary buffer to quickly assemble full path "without" extra allocation. The callback has to restore it afterwards of course. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b260d26 commit 6a0b0b6

File tree

7 files changed

+50
-37
lines changed

7 files changed

+50
-37
lines changed

archive.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,26 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
157157
return write_entry(args, sha1, path.buf, path.len, mode);
158158
}
159159

160+
static int write_archive_entry_buf(const unsigned char *sha1, struct strbuf *base,
161+
const char *filename, unsigned mode, int stage,
162+
void *context)
163+
{
164+
return write_archive_entry(sha1, base->buf, base->len,
165+
filename, mode, stage, context);
166+
}
167+
160168
static void queue_directory(const unsigned char *sha1,
161-
const char *base, int baselen, const char *filename,
169+
struct strbuf *base, const char *filename,
162170
unsigned mode, int stage, struct archiver_context *c)
163171
{
164172
struct directory *d;
165-
d = xmallocz(sizeof(*d) + baselen + 1 + strlen(filename));
173+
d = xmallocz(sizeof(*d) + base->len + 1 + strlen(filename));
166174
d->up = c->bottom;
167-
d->baselen = baselen;
175+
d->baselen = base->len;
168176
d->mode = mode;
169177
d->stage = stage;
170178
c->bottom = d;
171-
d->len = sprintf(d->path, "%.*s%s/", baselen, base, filename);
179+
d->len = sprintf(d->path, "%.*s%s/", (int)base->len, base->buf, filename);
172180
hashcpy(d->sha1, sha1);
173181
}
174182

@@ -191,28 +199,28 @@ static int write_directory(struct archiver_context *c)
191199
}
192200

193201
static int queue_or_write_archive_entry(const unsigned char *sha1,
194-
const char *base, int baselen, const char *filename,
202+
struct strbuf *base, const char *filename,
195203
unsigned mode, int stage, void *context)
196204
{
197205
struct archiver_context *c = context;
198206

199207
while (c->bottom &&
200-
!(baselen >= c->bottom->len &&
201-
!strncmp(base, c->bottom->path, c->bottom->len))) {
208+
!(base->len >= c->bottom->len &&
209+
!strncmp(base->buf, c->bottom->path, c->bottom->len))) {
202210
struct directory *next = c->bottom->up;
203211
free(c->bottom);
204212
c->bottom = next;
205213
}
206214

207215
if (S_ISDIR(mode)) {
208-
queue_directory(sha1, base, baselen, filename,
216+
queue_directory(sha1, base, filename,
209217
mode, stage, c);
210218
return READ_TREE_RECURSIVE;
211219
}
212220

213221
if (write_directory(c))
214222
return -1;
215-
return write_archive_entry(sha1, base, baselen, filename, mode,
223+
return write_archive_entry(sha1, base->buf, base->len, filename, mode,
216224
stage, context);
217225
}
218226

@@ -260,7 +268,7 @@ int write_archive_entries(struct archiver_args *args,
260268
err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec,
261269
args->pathspec.has_wildcard ?
262270
queue_or_write_archive_entry :
263-
write_archive_entry,
271+
write_archive_entry_buf,
264272
&context);
265273
if (err == READ_TREE_RECURSIVE)
266274
err = 0;
@@ -286,14 +294,14 @@ static const struct archiver *lookup_archiver(const char *name)
286294
return NULL;
287295
}
288296

289-
static int reject_entry(const unsigned char *sha1, const char *base,
290-
int baselen, const char *filename, unsigned mode,
297+
static int reject_entry(const unsigned char *sha1, struct strbuf *base,
298+
const char *filename, unsigned mode,
291299
int stage, void *context)
292300
{
293301
int ret = -1;
294302
if (S_ISDIR(mode)) {
295303
struct strbuf sb = STRBUF_INIT;
296-
strbuf_addstr(&sb, base);
304+
strbuf_addbuf(&sb, base);
297305
strbuf_addstr(&sb, filename);
298306
if (!match_pathspec(context, sb.buf, sb.len, 0, NULL, 1))
299307
ret = READ_TREE_RECURSIVE;

builtin/checkout.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int post_checkout_hook(struct commit *old, struct commit *new,
6262

6363
}
6464

65-
static int update_some(const unsigned char *sha1, const char *base, int baselen,
65+
static int update_some(const unsigned char *sha1, struct strbuf *base,
6666
const char *pathname, unsigned mode, int stage, void *context)
6767
{
6868
int len;
@@ -71,11 +71,11 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
7171
if (S_ISDIR(mode))
7272
return READ_TREE_RECURSIVE;
7373

74-
len = baselen + strlen(pathname);
74+
len = base->len + strlen(pathname);
7575
ce = xcalloc(1, cache_entry_size(len));
7676
hashcpy(ce->sha1, sha1);
77-
memcpy(ce->name, base, baselen);
78-
memcpy(ce->name + baselen, pathname, len - baselen);
77+
memcpy(ce->name, base->buf, base->len);
78+
memcpy(ce->name + base->len, pathname, len - base->len);
7979
ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
8080
ce->ce_namelen = len;
8181
ce->ce_mode = create_ce_mode(mode);

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
489489
}
490490

491491
static int show_tree_object(const unsigned char *sha1,
492-
const char *base, int baselen,
492+
struct strbuf *base,
493493
const char *pathname, unsigned mode, int stage, void *context)
494494
{
495495
printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");

builtin/ls-tree.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
6161
}
6262
}
6363

64-
static int show_tree(const unsigned char *sha1, const char *base, int baselen,
64+
static int show_tree(const unsigned char *sha1, struct strbuf *base,
6565
const char *pathname, unsigned mode, int stage, void *context)
6666
{
6767
int retval = 0;
@@ -79,7 +79,7 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
7979
*/
8080
type = commit_type;
8181
} else if (S_ISDIR(mode)) {
82-
if (show_recursive(base, baselen, pathname)) {
82+
if (show_recursive(base->buf, base->len, pathname)) {
8383
retval = READ_TREE_RECURSIVE;
8484
if (!(ls_options & LS_SHOW_TREES))
8585
return retval;
@@ -90,7 +90,8 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
9090
return 0;
9191

9292
if (chomp_prefix &&
93-
(baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
93+
(base->len < chomp_prefix ||
94+
memcmp(ls_tree_prefix, base->buf, chomp_prefix)))
9495
return 0;
9596

9697
if (!(ls_options & LS_NAME_ONLY)) {
@@ -112,7 +113,7 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
112113
printf("%06o %s %s\t", mode, type,
113114
find_unique_abbrev(sha1, abbrev));
114115
}
115-
write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
116+
write_name_quotedpfx(base->buf + chomp_prefix, base->len - chomp_prefix,
116117
pathname, stdout, line_termination);
117118
return retval;
118119
}

merge-recursive.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,20 @@ struct tree *write_tree_from_memory(struct merge_options *o)
275275
}
276276

277277
static int save_files_dirs(const unsigned char *sha1,
278-
const char *base, int baselen, const char *path,
278+
struct strbuf *base, const char *path,
279279
unsigned int mode, int stage, void *context)
280280
{
281-
int len = strlen(path);
282-
char *newpath = xmalloc(baselen + len + 1);
281+
int baselen = base->len;
283282
struct merge_options *o = context;
284283

285-
memcpy(newpath, base, baselen);
286-
memcpy(newpath + baselen, path, len);
287-
newpath[baselen + len] = '\0';
284+
strbuf_addstr(base, path);
288285

289286
if (S_ISDIR(mode))
290-
string_list_insert(&o->current_directory_set, newpath);
287+
string_list_insert(&o->current_directory_set, base->buf);
291288
else
292-
string_list_insert(&o->current_file_set, newpath);
293-
free(newpath);
289+
string_list_insert(&o->current_file_set, base->buf);
294290

291+
strbuf_setlen(base, baselen);
295292
return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
296293
}
297294

tree.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
3030
return add_cache_entry(ce, opt);
3131
}
3232

33-
static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
33+
static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
34+
const char *pathname, unsigned mode, int stage,
35+
void *context)
3436
{
35-
return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
37+
return read_one_entry_opt(sha1, base->buf, base->len, pathname,
38+
mode, stage,
3639
ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
3740
}
3841

3942
/*
4043
* This is used when the caller knows there is no existing entries at
4144
* the stage that will conflict with the entry being added.
4245
*/
43-
static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
46+
static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
47+
const char *pathname, unsigned mode, int stage,
48+
void *context)
4449
{
45-
return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
50+
return read_one_entry_opt(sha1, base->buf, base->len, pathname,
51+
mode, stage,
4652
ADD_CACHE_JUST_APPEND);
4753
}
4854

@@ -70,7 +76,7 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
7076
continue;
7177
}
7278

73-
switch (fn(entry.sha1, base->buf, base->len,
79+
switch (fn(entry.sha1, base,
7480
entry.path, entry.mode, stage, context)) {
7581
case 0:
7682
continue;

tree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "object.h"
55

66
extern const char *tree_type;
7+
struct strbuf;
78

89
struct tree {
910
struct object object;
@@ -22,7 +23,7 @@ void free_tree_buffer(struct tree *tree);
2223
struct tree *parse_tree_indirect(const unsigned char *sha1);
2324

2425
#define READ_TREE_RECURSIVE 1
25-
typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
26+
typedef int (*read_tree_fn_t)(const unsigned char *, struct strbuf *, const char *, unsigned int, int, void *);
2627

2728
extern int read_tree_recursive(struct tree *tree,
2829
const char *base, int baselen,

0 commit comments

Comments
 (0)