Skip to content

Commit 08bb69d

Browse files
rscharfegitster
authored andcommitted
describe: use prio_queue_replace()
Optimize the sequence get+put to peek+replace to avoid one unnecessary heap rebalance. Do that by tracking partial get operations in a prio_queue wrapper, struct lazy_queue, and using wrapper functions that turn get into peek and put into replace as needed. This is simpler than tracking the state explicitly in the calling code. We get a nice speedup on top of the previous patch's conversion to prio_queue: Benchmark 1: ./git_2.50.1 describe $(git rev-list v2.41.0..v2.47.0) Time (mean ± σ): 1.559 s ± 0.002 s [User: 1.493 s, System: 0.051 s] Range (min … max): 1.556 s … 1.563 s 10 runs Benchmark 2: ./git_describe_pq describe $(git rev-list v2.41.0..v2.47.0) Time (mean ± σ): 1.204 s ± 0.001 s [User: 1.138 s, System: 0.051 s] Range (min … max): 1.202 s … 1.205 s 10 runs Benchmark 3: ./git describe $(git rev-list v2.41.0..v2.47.0) Time (mean ± σ): 850.9 ms ± 1.6 ms [User: 786.6 ms, System: 49.8 ms] Range (min … max): 849.1 ms … 854.1 ms 10 runs Summary ./git describe $(git rev-list v2.41.0..v2.47.0) ran 1.41 ± 0.00 times faster than ./git_describe_pq describe $(git rev-list v2.41.0..v2.47.0) 1.83 ± 0.00 times faster than ./git_2.50.1 describe $(git rev-list v2.41.0..v2.47.0) Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66e2adb commit 08bb69d

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

builtin/describe.c

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,22 +250,58 @@ static int compare_pt(const void *a_, const void *b_)
250250
return 0;
251251
}
252252

253-
static bool all_have_flag(const struct prio_queue *queue, unsigned flag)
253+
struct lazy_queue {
254+
struct prio_queue queue;
255+
bool get_pending;
256+
};
257+
258+
#define LAZY_QUEUE_INIT { { compare_commits_by_commit_date }, false }
259+
260+
static void *lazy_queue_get(struct lazy_queue *queue)
261+
{
262+
if (queue->get_pending)
263+
prio_queue_get(&queue->queue);
264+
else
265+
queue->get_pending = true;
266+
return prio_queue_peek(&queue->queue);
267+
}
268+
269+
static void lazy_queue_put(struct lazy_queue *queue, void *thing)
270+
{
271+
if (queue->get_pending)
272+
prio_queue_replace(&queue->queue, thing);
273+
else
274+
prio_queue_put(&queue->queue, thing);
275+
queue->get_pending = false;
276+
}
277+
278+
static bool lazy_queue_empty(const struct lazy_queue *queue)
279+
{
280+
return queue->queue.nr == (queue->get_pending ? 1 : 0);
281+
}
282+
283+
static void lazy_queue_clear(struct lazy_queue *queue)
284+
{
285+
clear_prio_queue(&queue->queue);
286+
queue->get_pending = false;
287+
}
288+
289+
static bool all_have_flag(const struct lazy_queue *queue, unsigned flag)
254290
{
255-
for (size_t i = 0; i < queue->nr; i++) {
256-
struct commit *commit = queue->array[i].data;
291+
for (size_t i = queue->get_pending ? 1 : 0; i < queue->queue.nr; i++) {
292+
struct commit *commit = queue->queue.array[i].data;
257293
if (!(commit->object.flags & flag))
258294
return false;
259295
}
260296
return true;
261297
}
262298

263-
static unsigned long finish_depth_computation(struct prio_queue *queue,
299+
static unsigned long finish_depth_computation(struct lazy_queue *queue,
264300
struct possible_tag *best)
265301
{
266302
unsigned long seen_commits = 0;
267-
while (queue->nr) {
268-
struct commit *c = prio_queue_get(queue);
303+
while (!lazy_queue_empty(queue)) {
304+
struct commit *c = lazy_queue_get(queue);
269305
struct commit_list *parents = c->parents;
270306
seen_commits++;
271307
if (c->object.flags & best->flag_within) {
@@ -277,7 +313,7 @@ static unsigned long finish_depth_computation(struct prio_queue *queue,
277313
struct commit *p = parents->item;
278314
repo_parse_commit(the_repository, p);
279315
if (!(p->object.flags & SEEN))
280-
prio_queue_put(queue, p);
316+
lazy_queue_put(queue, p);
281317
p->object.flags |= c->object.flags;
282318
parents = parents->next;
283319
}
@@ -319,7 +355,7 @@ static void append_suffix(int depth, const struct object_id *oid, struct strbuf
319355
static void describe_commit(struct object_id *oid, struct strbuf *dst)
320356
{
321357
struct commit *cmit, *gave_up_on = NULL;
322-
struct prio_queue queue = { compare_commits_by_commit_date };
358+
struct lazy_queue queue = LAZY_QUEUE_INIT;
323359
struct commit_name *n;
324360
struct possible_tag all_matches[MAX_TAGS];
325361
unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
@@ -363,9 +399,9 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
363399
}
364400

365401
cmit->object.flags = SEEN;
366-
prio_queue_put(&queue, cmit);
367-
while (queue.nr) {
368-
struct commit *c = prio_queue_get(&queue);
402+
lazy_queue_put(&queue, cmit);
403+
while (!lazy_queue_empty(&queue)) {
404+
struct commit *c = lazy_queue_get(&queue);
369405
struct commit_list *parents = c->parents;
370406
struct commit_name **slot;
371407

@@ -399,7 +435,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
399435
t->depth++;
400436
}
401437
/* Stop if last remaining path already covered by best candidate(s) */
402-
if (annotated_cnt && !queue.nr) {
438+
if (annotated_cnt && lazy_queue_empty(&queue)) {
403439
int best_depth = INT_MAX;
404440
unsigned best_within = 0;
405441
for (cur_match = 0; cur_match < match_cnt; cur_match++) {
@@ -422,7 +458,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
422458
struct commit *p = parents->item;
423459
repo_parse_commit(the_repository, p);
424460
if (!(p->object.flags & SEEN))
425-
prio_queue_put(&queue, p);
461+
lazy_queue_put(&queue, p);
426462
p->object.flags |= c->object.flags;
427463
parents = parents->next;
428464

@@ -437,7 +473,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
437473
strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
438474
if (suffix)
439475
strbuf_addstr(dst, suffix);
440-
clear_prio_queue(&queue);
476+
lazy_queue_clear(&queue);
441477
return;
442478
}
443479
if (unannotated_cnt)
@@ -453,11 +489,11 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
453489
QSORT(all_matches, match_cnt, compare_pt);
454490

455491
if (gave_up_on) {
456-
prio_queue_put(&queue, gave_up_on);
492+
lazy_queue_put(&queue, gave_up_on);
457493
seen_commits--;
458494
}
459495
seen_commits += finish_depth_computation(&queue, &all_matches[0]);
460-
clear_prio_queue(&queue);
496+
lazy_queue_clear(&queue);
461497

462498
if (debug) {
463499
static int label_width = -1;

0 commit comments

Comments
 (0)