Skip to content

Commit b511164

Browse files
committed
Merge branch 'rs/name-rev-with-mempool'
Many small allocations "git name-rev" makes have been updated to allocate from a mem-pool. * rs/name-rev-with-mempool: name-rev: use mem_pool_strfmt() mem-pool: add mem_pool_strfmt()
2 parents 6f74483 + f39addd commit b511164

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

builtin/name-rev.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "commit-slab.h"
1616
#include "commit-graph.h"
1717
#include "wildmatch.h"
18+
#include "mem-pool.h"
1819

1920
/*
2021
* One day. See the 'name a rev shortly after epoch' test in t6120 when
@@ -155,30 +156,25 @@ static struct rev_name *create_or_update_name(struct commit *commit,
155156
return name;
156157
}
157158

158-
static char *get_parent_name(const struct rev_name *name, int parent_number)
159+
static char *get_parent_name(const struct rev_name *name, int parent_number,
160+
struct mem_pool *string_pool)
159161
{
160-
struct strbuf sb = STRBUF_INIT;
161162
size_t len;
162163

163164
strip_suffix(name->tip_name, "^0", &len);
164165
if (name->generation > 0) {
165-
strbuf_grow(&sb, len +
166-
1 + decimal_width(name->generation) +
167-
1 + decimal_width(parent_number));
168-
strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
169-
name->generation, parent_number);
166+
return mem_pool_strfmt(string_pool, "%.*s~%d^%d",
167+
(int)len, name->tip_name,
168+
name->generation, parent_number);
170169
} else {
171-
strbuf_grow(&sb, len +
172-
1 + decimal_width(parent_number));
173-
strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
174-
parent_number);
170+
return mem_pool_strfmt(string_pool, "%.*s^%d",
171+
(int)len, name->tip_name, parent_number);
175172
}
176-
return strbuf_detach(&sb, NULL);
177173
}
178174

179175
static void name_rev(struct commit *start_commit,
180176
const char *tip_name, timestamp_t taggerdate,
181-
int from_tag, int deref)
177+
int from_tag, int deref, struct mem_pool *string_pool)
182178
{
183179
struct prio_queue queue;
184180
struct commit *commit;
@@ -195,9 +191,10 @@ static void name_rev(struct commit *start_commit,
195191
if (!start_name)
196192
return;
197193
if (deref)
198-
start_name->tip_name = xstrfmt("%s^0", tip_name);
194+
start_name->tip_name = mem_pool_strfmt(string_pool, "%s^0",
195+
tip_name);
199196
else
200-
start_name->tip_name = xstrdup(tip_name);
197+
start_name->tip_name = mem_pool_strdup(string_pool, tip_name);
201198

202199
memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
203200
prio_queue_put(&queue, start_commit);
@@ -235,7 +232,8 @@ static void name_rev(struct commit *start_commit,
235232
if (parent_number > 1)
236233
parent_name->tip_name =
237234
get_parent_name(name,
238-
parent_number);
235+
parent_number,
236+
string_pool);
239237
else
240238
parent_name->tip_name = name->tip_name;
241239
ALLOC_GROW(parents_to_queue,
@@ -415,7 +413,7 @@ static int name_ref(const char *path, const struct object_id *oid,
415413
return 0;
416414
}
417415

418-
static void name_tips(void)
416+
static void name_tips(struct mem_pool *string_pool)
419417
{
420418
int i;
421419

@@ -428,7 +426,7 @@ static void name_tips(void)
428426
struct tip_table_entry *e = &tip_table.table[i];
429427
if (e->commit) {
430428
name_rev(e->commit, e->refname, e->taggerdate,
431-
e->from_tag, e->deref);
429+
e->from_tag, e->deref, string_pool);
432430
}
433431
}
434432
}
@@ -561,6 +559,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
561559

562560
int cmd_name_rev(int argc, const char **argv, const char *prefix)
563561
{
562+
struct mem_pool string_pool;
564563
struct object_array revs = OBJECT_ARRAY_INIT;
565564
int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
566565
struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
@@ -587,6 +586,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
587586
OPT_END(),
588587
};
589588

589+
mem_pool_init(&string_pool, 0);
590590
init_commit_rev_name(&rev_names);
591591
git_config(git_default_config, NULL);
592592
argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
@@ -648,7 +648,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
648648
adjust_cutoff_timestamp_for_slop();
649649

650650
for_each_ref(name_ref, &data);
651-
name_tips();
651+
name_tips(&string_pool);
652652

653653
if (annotate_stdin) {
654654
struct strbuf sb = STRBUF_INIT;
@@ -676,6 +676,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
676676
always, allow_undefined, data.name_only);
677677
}
678678

679+
UNLEAK(string_pool);
679680
UNLEAK(revs);
680681
return 0;
681682
}

mem-pool.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,45 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
107107
return r;
108108
}
109109

110+
static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
111+
va_list ap)
112+
{
113+
struct mp_block *block = pool->mp_block;
114+
char *next_free = block ? block->next_free : NULL;
115+
size_t available = block ? block->end - block->next_free : 0;
116+
va_list cp;
117+
int len, len2;
118+
char *ret;
119+
120+
va_copy(cp, ap);
121+
len = vsnprintf(next_free, available, fmt, cp);
122+
va_end(cp);
123+
if (len < 0)
124+
BUG("your vsnprintf is broken (returned %d)", len);
125+
126+
ret = mem_pool_alloc(pool, len + 1); /* 1 for NUL */
127+
128+
/* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
129+
if (ret == next_free)
130+
return ret;
131+
132+
len2 = vsnprintf(ret, len + 1, fmt, ap);
133+
if (len2 != len)
134+
BUG("your vsnprintf is broken (returns inconsistent lengths)");
135+
return ret;
136+
}
137+
138+
char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...)
139+
{
140+
va_list ap;
141+
char *ret;
142+
143+
va_start(ap, fmt);
144+
ret = mem_pool_strvfmt(pool, fmt, ap);
145+
va_end(ap);
146+
return ret;
147+
}
148+
110149
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
111150
{
112151
size_t len = st_mult(count, size);

mem-pool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
4747
char *mem_pool_strdup(struct mem_pool *pool, const char *str);
4848
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len);
4949

50+
/*
51+
* Allocate memory from the memory pool and format a string into it.
52+
*/
53+
char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...);
54+
5055
/*
5156
* Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
5257
* pool will be empty and not contain any memory. It still needs to be free'd

0 commit comments

Comments
 (0)