Commit d9dff0e5c658f1bef8647522dcb5b851b3f4734e

Stefan Sperling 2020-12-26T21:32:01

switch reflist to TAILQ; insert elements more efficiently for sorted input ok naddy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
diff --git a/got/got.c b/got/got.c
index 5ae8df4..56ec3c8 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1881,7 +1881,7 @@ delete_missing_refs(struct got_pathlist_head *their_refs,
 	char *remote_namespace = NULL;
 	char *local_refname = NULL;
 
-	SIMPLEQ_INIT(&my_refs);
+	TAILQ_INIT(&my_refs);
 
 	if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
 	    == -1)
@@ -1891,7 +1891,7 @@ delete_missing_refs(struct got_pathlist_head *their_refs,
 	if (err)
 		goto done;
 
-	SIMPLEQ_FOREACH(re, &my_refs, entry) {
+	TAILQ_FOREACH(re, &my_refs, entry) {
 		const char *refname = got_ref_get_name(re->ref);
 
 		if (!remote->mirror_references) {
@@ -2744,7 +2744,7 @@ cmd_checkout(int argc, char *argv[])
 	if (commit_id_str) {
 		struct got_object_id *commit_id;
 		struct got_reflist_head refs;
-		SIMPLEQ_INIT(&refs);
+		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
 		    NULL);
 		if (error)
@@ -3058,7 +3058,7 @@ cmd_update(int argc, char *argv[])
 			goto done;
 	} else {
 		struct got_reflist_head refs;
-		SIMPLEQ_INIT(&refs);
+		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
 		    NULL);
 		if (error)
@@ -3416,7 +3416,7 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 	char *refs_str = NULL;
 	struct got_reflist_entry *re;
 
-	SIMPLEQ_FOREACH(re, refs, entry) {
+	TAILQ_FOREACH(re, refs, entry) {
 		char *s;
 		const char *name;
 		struct got_tag_object *tag = NULL;
@@ -3716,7 +3716,7 @@ cmd_log(int argc, char *argv[])
 	const char *errstr;
 	struct got_reflist_head refs;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 #ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
@@ -4130,7 +4130,7 @@ cmd_diff(int argc, char *argv[])
 	char *path = NULL;
 	struct got_reflist_head refs;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 #ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
@@ -4582,7 +4582,7 @@ cmd_blame(int argc, char *argv[])
 			goto done;
 	} else {
 		struct got_reflist_head refs;
-		SIMPLEQ_INIT(&refs);
+		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
 		    NULL);
 		if (error)
@@ -4925,7 +4925,7 @@ cmd_tree(int argc, char *argv[])
 			goto done;
 	} else {
 		struct got_reflist_head refs;
-		SIMPLEQ_INIT(&refs);
+		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
 		    NULL);
 		if (error)
@@ -5090,12 +5090,12 @@ list_refs(struct got_repository *repo, const char *refname)
 	struct got_reflist_head refs;
 	struct got_reflist_entry *re;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 	err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
 	if (err)
 		return err;
 
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	TAILQ_FOREACH(re, &refs, entry) {
 		char *refstr;
 		refstr = got_ref_to_str(re->ref);
 		if (refstr == NULL)
@@ -5421,7 +5421,7 @@ list_branches(struct got_repository *repo, struct got_worktree *worktree)
 	struct got_reference *temp_ref = NULL;
 	int rebase_in_progress, histedit_in_progress;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	if (worktree) {
 		err = got_worktree_rebase_in_progress(&rebase_in_progress,
@@ -5449,7 +5449,7 @@ list_branches(struct got_repository *repo, struct got_worktree *worktree)
 	if (err)
 		return err;
 
-	SIMPLEQ_FOREACH(re, &refs, entry)
+	TAILQ_FOREACH(re, &refs, entry)
 		list_branch(repo, worktree, re->ref);
 
 	got_ref_list_free(&refs);
@@ -5646,7 +5646,7 @@ cmd_branch(int argc, char *argv[])
 		error = delete_branch(repo, worktree, delref);
 	else {
 		struct got_reflist_head refs;
-		SIMPLEQ_INIT(&refs);
+		TAILQ_INIT(&refs);
 		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
 		    NULL);
 		if (error)
@@ -5793,13 +5793,13 @@ list_tags(struct got_repository *repo, struct got_worktree *worktree)
 	struct got_reflist_head refs;
 	struct got_reflist_entry *re;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
 	if (err)
 		return err;
 
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	TAILQ_FOREACH(re, &refs, entry) {
 		const char *refname;
 		char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
 		char datebuf[26];
@@ -5976,7 +5976,7 @@ add_tag(struct got_repository *repo, struct got_worktree *worktree,
 	int preserve_tagmsg = 0;
 	struct got_reflist_head refs;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	/*
 	 * Don't let the user create a tag name with a leading '-'.
@@ -9624,7 +9624,7 @@ cmd_cat(int argc, char *argv[])
 	int ch, obj_type, i, force_path = 0;
 	struct got_reflist_head refs;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 #ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index 53ce046..b657ec5 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -2780,7 +2780,7 @@ gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
 	time_t committer_time = 0, cmp_time = 0;
 
 	*repo_age = NULL;
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	if (gw_trans->gw_conf->got_show_repo_age == 0)
 		return NULL;
@@ -2802,7 +2802,7 @@ gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
 	 * Find the youngest branch tip in the repository, or the age of
 	 * the a specific branch tip if a name was provided by the caller.
 	 */
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	TAILQ_FOREACH(re, &refs, entry) {
 		struct got_object_id *id = NULL;
 
 		if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
@@ -3015,14 +3015,14 @@ gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
 	int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
 	int prev_set = 0, tag_count = 0;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
 	    got_ref_cmp_tags, gw_trans->repo);
 	if (error)
 		goto done;
 
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	TAILQ_FOREACH(re, &refs, entry) {
 		const char *refname;
 		const char *tagger;
 		const char *tag_commit;
@@ -3420,7 +3420,7 @@ gw_init_header()
 		return NULL;
 
 	header->path = NULL;
-	SIMPLEQ_INIT(&header->refs);
+	TAILQ_INIT(&header->refs);
 
 	header->refs_str = NULL;
 	header->commit_id = NULL;
@@ -3546,7 +3546,7 @@ gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
 	char *commit_msg = NULL, *commit_msg0;
 
 	/*print commit*/
-	SIMPLEQ_FOREACH(re, &header->refs, entry) {
+	TAILQ_FOREACH(re, &header->refs, entry) {
 		char *s;
 		const char *name;
 		struct got_tag_object *tag = NULL;
@@ -4402,7 +4402,7 @@ gw_output_repo_heads(struct gw_trans *gw_trans)
 	char *href_commits = NULL;
 	enum kcgi_err kerr = KCGI_OK;
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
 	    got_ref_cmp_by_name, NULL);
@@ -4428,7 +4428,7 @@ gw_output_repo_heads(struct gw_trans *gw_trans)
 	if (kerr != KCGI_OK)
 		goto done;
 
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	TAILQ_FOREACH(re, &refs, entry) {
 		const char *refname;
 
 		if (got_ref_is_symbolic(re->ref))
diff --git a/include/got_reference.h b/include/got_reference.h
index 8a7ddd1..eeeea20 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -77,10 +77,10 @@ char *got_ref_to_str(struct got_reference *);
 
 /* List of references. */
 struct got_reflist_entry {
-	SIMPLEQ_ENTRY(got_reflist_entry) entry;
+	TAILQ_ENTRY(got_reflist_entry) entry;
 	struct got_reference *ref;
 };
-SIMPLEQ_HEAD(got_reflist_head, got_reflist_entry);
+TAILQ_HEAD(got_reflist_head, got_reflist_entry);
 
 /* Duplicate a reference list entry. Caller must dispose of it with free(3). */
 const struct got_error *got_reflist_entry_dup(struct got_reflist_entry **,
diff --git a/lib/fetch.c b/lib/fetch.c
index 0aab76f..134a933 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -408,7 +408,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		tmpfds[i] = -1;
 
 	TAILQ_INIT(&have_refs);
-	SIMPLEQ_INIT(&my_refs);
+	TAILQ_INIT(&my_refs);
 
 	if (!mirror_references) {
 		if (asprintf(&ref_prefix, "refs/remotes/%s/",
@@ -424,7 +424,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 			goto done;
 	}
 
-	SIMPLEQ_FOREACH(re, &my_refs, entry) {
+	TAILQ_FOREACH(re, &my_refs, entry) {
 		struct got_object_id *id;
 		const char *refname;
 
diff --git a/lib/reference.c b/lib/reference.c
index d9f1596..6dba390 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -746,7 +746,7 @@ insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
     got_ref_cmp_cb cmp_cb, void *cmp_arg)
 {
 	const struct got_error *err;
-	struct got_reflist_entry *new, *re, *prev = NULL;
+	struct got_reflist_entry *new, *re;
 	int cmp;
 
 	*newp = NULL;
@@ -762,8 +762,13 @@ insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
 	 * contain redundant entries. On-disk refs take precedence.
 	 * This code assumes that on-disk revs are read before packed-refs.
 	 * We're iterating the list anyway, so insert elements sorted by name.
+	 *
+	 * Many callers will provide paths in a somewhat sorted order.
+	 * Iterating backwards from the tail of the list should be more
+	 * efficient than traversing through the entire list each time
+	 * an element is inserted.
 	 */
-	re = SIMPLEQ_FIRST(refs);
+	re = TAILQ_LAST(refs, got_reflist_head);
 	while (re) {
 		err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
 		if (err)
@@ -773,19 +778,14 @@ insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
 			free(new);
 			*newp = NULL;
 			return NULL;
-		} else if (cmp > 0) {
-			if (prev)
-				SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
-			else
-				SIMPLEQ_INSERT_HEAD(refs, new, entry);
+		} else if (cmp < 0) {
+			TAILQ_INSERT_AFTER(refs, re, new, entry);
 			return NULL;
-		} else {
-			prev = re;
-			re = SIMPLEQ_NEXT(re, entry);
 		}
+		re = TAILQ_PREV(re, got_reflist_head, entry);
 	}
 
-	SIMPLEQ_INSERT_TAIL(refs, new, entry);
+	TAILQ_INSERT_HEAD(refs, new, entry);
 	return NULL;
 }
 
@@ -1012,10 +1012,8 @@ got_ref_list_free(struct got_reflist_head *refs)
 {
 	struct got_reflist_entry *re;
 
-	while (!SIMPLEQ_EMPTY(refs)) {
-		re = SIMPLEQ_FIRST(refs);
-		SIMPLEQ_REMOVE_HEAD(refs, entry);
-		got_ref_close(re->ref);
+	while ((re = TAILQ_FIRST(refs))) {
+		TAILQ_REMOVE(refs, re, entry);
 		free(re);
 	}
 
@@ -1184,7 +1182,7 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
 	if (delref->flags & GOT_REF_IS_SYMBOLIC)
 		return got_error(GOT_ERR_BAD_REF_DATA);
 
-	SIMPLEQ_INIT(&refs);
+	TAILQ_INIT(&refs);
 
 	packed_refs_path = got_repo_get_path_packed_refs(repo);
 	if (packed_refs_path == NULL)
@@ -1253,7 +1251,7 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
 			goto done;
 		}
 
-		SIMPLEQ_FOREACH(re, &refs, entry) {
+		TAILQ_FOREACH(re, &refs, entry) {
 			uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
 
 			if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
@@ -1430,7 +1428,7 @@ got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
 	}
 	(*map)->idset = idset;
 
-	SIMPLEQ_FOREACH(re, refs, entry) {
+	TAILQ_FOREACH(re, refs, entry) {
 		struct got_reflist_entry *new;
 		struct got_reflist_object_id_map_entry *ent;
 
@@ -1445,7 +1443,7 @@ got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
 				err = got_error_from_errno("malloc");
 				goto done;
 			}
-			SIMPLEQ_INIT(&ent->refs);
+			TAILQ_INIT(&ent->refs);
 			err = got_object_idset_add(idset, id, ent);
 			if (err)
 				goto done;
@@ -1454,7 +1452,7 @@ got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
 		err = got_reflist_entry_dup(&new, re);
 		if (err)
 			goto done;
-		SIMPLEQ_INSERT_TAIL(&ent->refs, new, entry);
+		TAILQ_INSERT_TAIL(&ent->refs, new, entry);
 		free(id);
 		id = NULL;
 	}
diff --git a/lib/repository.c b/lib/repository.c
index bfbd39a..4a6e13f 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1538,7 +1538,7 @@ got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
 
 	*tag = NULL;
 
-	SIMPLEQ_FOREACH(re, refs, entry) {
+	TAILQ_FOREACH(re, refs, entry) {
 		const char *refname;
 		refname = got_ref_get_name(re->ref);
 		if (got_ref_is_symbolic(re->ref))
diff --git a/tog/tog.c b/tog/tog.c
index 34ef259..2e23ae5 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -125,7 +125,7 @@ struct tog_color {
 };
 SIMPLEQ_HEAD(tog_colors, tog_color);
 
-static struct got_reflist_head tog_refs = SIMPLEQ_HEAD_INITIALIZER(tog_refs);
+static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
 static struct got_reflist_object_id_map *tog_refs_idmap;
 
 static const struct got_error *
@@ -1248,7 +1248,7 @@ build_refs_str(char **refs_str, struct got_reflist_head *refs,
 
 	*refs_str = NULL;
 
-	SIMPLEQ_FOREACH(re, refs, entry) {
+	TAILQ_FOREACH(re, refs, entry) {
 		struct got_tag_object *tag = NULL;
 		struct got_object_id *ref_id;
 		int cmp;
@@ -2757,7 +2757,7 @@ cmd_log(int argc, char *argv[])
 		goto done;
 
 	/* already loaded by tog_log_with_path()? */
-	if (SIMPLEQ_EMPTY(&tog_refs)) {
+	if (TAILQ_EMPTY(&tog_refs)) {
 		error = tog_load_refs(repo);
 		if (error)
 			goto done;
@@ -5621,7 +5621,7 @@ ref_view_load_refs(struct tog_ref_view_state *s)
 	struct tog_reflist_entry *re;
 
 	s->nrefs = 0;
-	SIMPLEQ_FOREACH(sre, &tog_refs, entry) {
+	TAILQ_FOREACH(sre, &tog_refs, entry) {
 		if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
 			continue;