Skip to content

Commit 6784fab

Browse files
committed
Merge branch 'dk/blame-janitorial'
Code clean-up. * dk/blame-janitorial: builtin/blame.c::find_copy_in_blob: no need to scan for region end blame.c: prepare_lines should not call xrealloc for every line builtin/blame.c::prepare_lines: fix allocation size of sb->lineno builtin/blame.c: eliminate same_suspect() builtin/blame.c: struct blame_entry does not need a prev link
2 parents 62bef66 + 3ee8944 commit 6784fab

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

builtin/blame.c

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ static void drop_origin_blob(struct origin *o)
197197
* scoreboard structure, sorted by the target line number.
198198
*/
199199
struct blame_entry {
200-
struct blame_entry *prev;
201200
struct blame_entry *next;
202201

203202
/* the first line of this group in the final image;
@@ -256,15 +255,6 @@ struct scoreboard {
256255
int *lineno;
257256
};
258257

259-
static inline int same_suspect(struct origin *a, struct origin *b)
260-
{
261-
if (a == b)
262-
return 1;
263-
if (a->commit != b->commit)
264-
return 0;
265-
return !strcmp(a->path, b->path);
266-
}
267-
268258
static void sanity_check_refcnt(struct scoreboard *);
269259

270260
/*
@@ -277,13 +267,11 @@ static void coalesce(struct scoreboard *sb)
277267
struct blame_entry *ent, *next;
278268

279269
for (ent = sb->ent; ent && (next = ent->next); ent = next) {
280-
if (same_suspect(ent->suspect, next->suspect) &&
270+
if (ent->suspect == next->suspect &&
281271
ent->guilty == next->guilty &&
282272
ent->s_lno + ent->num_lines == next->s_lno) {
283273
ent->num_lines += next->num_lines;
284274
ent->next = next->next;
285-
if (ent->next)
286-
ent->next->prev = ent;
287275
origin_decref(next->suspect);
288276
free(next);
289277
ent->score = 0;
@@ -534,7 +522,7 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
534522
prev = ent;
535523

536524
/* prev, if not NULL, is the last one that is below e */
537-
e->prev = prev;
525+
538526
if (prev) {
539527
e->next = prev->next;
540528
prev->next = e;
@@ -543,8 +531,6 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
543531
e->next = sb->ent;
544532
sb->ent = e;
545533
}
546-
if (e->next)
547-
e->next->prev = e;
548534
}
549535

550536
/*
@@ -555,14 +541,12 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
555541
*/
556542
static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
557543
{
558-
struct blame_entry *p, *n;
544+
struct blame_entry *n;
559545

560-
p = dst->prev;
561546
n = dst->next;
562547
origin_incref(src->suspect);
563548
origin_decref(dst->suspect);
564549
memcpy(dst, src, sizeof(*src));
565-
dst->prev = p;
566550
dst->next = n;
567551
dst->score = 0;
568552
}
@@ -742,7 +726,7 @@ static int find_last_in_target(struct scoreboard *sb, struct origin *target)
742726
int last_in_target = -1;
743727

744728
for (e = sb->ent; e; e = e->next) {
745-
if (e->guilty || !same_suspect(e->suspect, target))
729+
if (e->guilty || e->suspect != target)
746730
continue;
747731
if (last_in_target < e->s_lno + e->num_lines)
748732
last_in_target = e->s_lno + e->num_lines;
@@ -762,7 +746,7 @@ static void blame_chunk(struct scoreboard *sb,
762746
struct blame_entry *e;
763747

764748
for (e = sb->ent; e; e = e->next) {
765-
if (e->guilty || !same_suspect(e->suspect, target))
749+
if (e->guilty || e->suspect != target)
766750
continue;
767751
if (same <= e->s_lno)
768752
continue;
@@ -939,7 +923,6 @@ static void find_copy_in_blob(struct scoreboard *sb,
939923
mmfile_t *file_p)
940924
{
941925
const char *cp;
942-
int cnt;
943926
mmfile_t file_o;
944927
struct handle_split_cb_data d;
945928

@@ -950,13 +933,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
950933
*/
951934
cp = nth_line(sb, ent->lno);
952935
file_o.ptr = (char *) cp;
953-
cnt = ent->num_lines;
954-
955-
while (cnt && cp < sb->final_buf + sb->final_buf_size) {
956-
if (*cp++ == '\n')
957-
cnt--;
958-
}
959-
file_o.size = cp - file_o.ptr;
936+
file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
960937

961938
/*
962939
* file_o is a part of final image we are annotating.
@@ -992,7 +969,7 @@ static int find_move_in_parent(struct scoreboard *sb,
992969
while (made_progress) {
993970
made_progress = 0;
994971
for (e = sb->ent; e; e = e->next) {
995-
if (e->guilty || !same_suspect(e->suspect, target) ||
972+
if (e->guilty || e->suspect != target ||
996973
ent_score(sb, e) < blame_move_score)
997974
continue;
998975
find_copy_in_blob(sb, e, parent, split, &file_p);
@@ -1027,14 +1004,14 @@ static struct blame_list *setup_blame_list(struct scoreboard *sb,
10271004

10281005
for (e = sb->ent, num_ents = 0; e; e = e->next)
10291006
if (!e->scanned && !e->guilty &&
1030-
same_suspect(e->suspect, target) &&
1007+
e->suspect == target &&
10311008
min_score < ent_score(sb, e))
10321009
num_ents++;
10331010
if (num_ents) {
10341011
blame_list = xcalloc(num_ents, sizeof(struct blame_list));
10351012
for (e = sb->ent, i = 0; e; e = e->next)
10361013
if (!e->scanned && !e->guilty &&
1037-
same_suspect(e->suspect, target) &&
1014+
e->suspect == target &&
10381015
min_score < ent_score(sb, e))
10391016
blame_list[i++].ent = e;
10401017
}
@@ -1178,7 +1155,7 @@ static void pass_whole_blame(struct scoreboard *sb,
11781155
origin->file.ptr = NULL;
11791156
}
11801157
for (e = sb->ent; e; e = e->next) {
1181-
if (!same_suspect(e->suspect, origin))
1158+
if (e->suspect != origin)
11821159
continue;
11831160
origin_incref(porigin);
11841161
origin_decref(e->suspect);
@@ -1567,7 +1544,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
15671544

15681545
/* Take responsibility for the remaining entries */
15691546
for (ent = sb->ent; ent; ent = ent->next)
1570-
if (same_suspect(ent->suspect, suspect))
1547+
if (ent->suspect == suspect)
15711548
found_guilty_entry(ent);
15721549
origin_decref(suspect);
15731550

@@ -1772,25 +1749,41 @@ static int prepare_lines(struct scoreboard *sb)
17721749
{
17731750
const char *buf = sb->final_buf;
17741751
unsigned long len = sb->final_buf_size;
1775-
int num = 0, incomplete = 0, bol = 1;
1752+
const char *end = buf + len;
1753+
const char *p;
1754+
int *lineno;
1755+
int num = 0, incomplete = 0;
17761756

1777-
if (len && buf[len-1] != '\n')
1778-
incomplete++; /* incomplete line at the end */
1779-
while (len--) {
1780-
if (bol) {
1781-
sb->lineno = xrealloc(sb->lineno,
1782-
sizeof(int *) * (num + 1));
1783-
sb->lineno[num] = buf - sb->final_buf;
1784-
bol = 0;
1785-
}
1786-
if (*buf++ == '\n') {
1757+
for (p = buf;;) {
1758+
p = memchr(p, '\n', end - p);
1759+
if (p) {
1760+
p++;
17871761
num++;
1788-
bol = 1;
1762+
continue;
17891763
}
1764+
break;
17901765
}
1791-
sb->lineno = xrealloc(sb->lineno,
1792-
sizeof(int *) * (num + incomplete + 1));
1793-
sb->lineno[num + incomplete] = buf - sb->final_buf;
1766+
1767+
if (len && end[-1] != '\n')
1768+
incomplete++; /* incomplete line at the end */
1769+
1770+
sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
1771+
lineno = sb->lineno;
1772+
1773+
*lineno++ = 0;
1774+
for (p = buf;;) {
1775+
p = memchr(p, '\n', end - p);
1776+
if (p) {
1777+
p++;
1778+
*lineno++ = p - buf;
1779+
continue;
1780+
}
1781+
break;
1782+
}
1783+
1784+
if (incomplete)
1785+
*lineno++ = len;
1786+
17941787
sb->num_lines = num + incomplete;
17951788
return sb->num_lines;
17961789
}
@@ -2502,8 +2495,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
25022495
ent->suspect = o;
25032496
ent->s_lno = bottom;
25042497
ent->next = next;
2505-
if (next)
2506-
next->prev = ent;
25072498
origin_incref(o);
25082499
}
25092500
origin_decref(o);

0 commit comments

Comments
 (0)