Commit cbc287dcbb29ad321dca5cd14c31998279205243

Stefan Sperling 2022-04-19T20:08:41

reimplement object-ID set data structure on top of a hash table Siphash suggested by jrick as a better alternative to murmurhash for this use case. with small fixes from and ok op@

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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
diff --git a/lib/got_lib_object_idset.h b/lib/got_lib_object_idset.h
index 5346f7f..750be49 100644
--- a/lib/got_lib_object_idset.h
+++ b/lib/got_lib_object_idset.h
@@ -31,13 +31,3 @@ const struct got_error *got_object_idset_for_each(struct got_object_idset *,
     const struct got_error *(*cb)(struct got_object_id *, void *, void *),
     void *);
 int got_object_idset_num_elements(struct got_object_idset *);
-
-struct got_object_idset_element;
-struct got_object_idset_element *got_object_idset_get_element(
-    struct got_object_idset *, struct got_object_id *);
-void *got_object_idset_get_element_data(struct got_object_idset_element *);
-const struct got_error *got_object_idset_for_each_element(struct got_object_idset *,
-    const struct got_error *(*cb)(struct got_object_idset_element *, void *), void *);
-void got_object_idset_remove_element(struct got_object_idset *,
-    struct got_object_idset_element *);
-
diff --git a/lib/object_idset.c b/lib/object_idset.c
index 06f7347..e27d30c 100644
--- a/lib/object_idset.c
+++ b/lib/object_idset.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -15,15 +15,17 @@
  */
 
 #include <sys/queue.h>
-#include <sys/tree.h>
 
 #include <stdlib.h>
+#include <stdint.h>
 #include <string.h>
 #include <sha1.h>
 #include <stdio.h>
 #include <zlib.h>
 #include <limits.h>
 #include <time.h>
+#include <errno.h>
+#include <siphash.h>
 
 #include "got_object.h"
 #include "got_error.h"
@@ -32,116 +34,185 @@
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
 #include "got_lib_object_idset.h"
+#include "got_lib_object_parse.h"
 
-struct got_object_idset_element {
-	RB_ENTRY(got_object_idset_element)	entry;
-	struct got_object_id id;
-	void *data;	/* API user data */
-};
-
-RB_HEAD(got_object_idset_tree, got_object_idset_element);
-
-static int
-cmp_elements(const struct got_object_idset_element *e1,
-    const struct got_object_idset_element *e2)
-{
-	return got_object_id_cmp(&e1->id, &e2->id);
-}
-
-RB_PROTOTYPE(got_object_idset_tree, got_object_idset_element, entry,
-    cmp_elements);
+#define GOT_OBJECT_IDSET_MIN_BUCKETS	64
 
 struct got_object_idset {
-	struct got_object_idset_tree entries;
-	int totelem;
-#define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
+	struct got_object_id_queue *ids;
+	size_t nbuckets;
+	unsigned int totelem;
+	unsigned int flags;
+#define GOT_OBJECT_IDSET_F_TRAVERSAL	0x01
+#define GOT_OBJECT_IDSET_F_NOMEM	0x02
+	SIPHASH_KEY key;
 };
 
 struct got_object_idset *
 got_object_idset_alloc(void)
 {
 	struct got_object_idset *set;
+	int i;
 
 	set = malloc(sizeof(*set));
 	if (set == NULL)
 		return NULL;
 
-	RB_INIT(&set->entries);
-	set->totelem = 0;
+	set->ids = calloc(sizeof(set->ids[0]), GOT_OBJECT_IDSET_MIN_BUCKETS);
+	if (set->ids == NULL) {
+		free(set);
+		return NULL;
+	}
+	for (i = 0; i < GOT_OBJECT_IDSET_MIN_BUCKETS; i++)
+		STAILQ_INIT(&set->ids[i]);
 
+	set->totelem = 0;
+	set->nbuckets = GOT_OBJECT_IDSET_MIN_BUCKETS;
+	set->flags = 0;
+	arc4random_buf(&set->key, sizeof(set->key));
 	return set;
 }
 
 void
 got_object_idset_free(struct got_object_idset *set)
 {
-	struct got_object_idset_element *entry;
+	size_t i;
+	struct got_object_qid *qid;
+
+	for (i = 0; i < set->nbuckets; i++) {
+		while (!STAILQ_EMPTY(&set->ids[i])) {
+			qid = STAILQ_FIRST(&set->ids[i]);
+			STAILQ_REMOVE(&set->ids[i], qid, got_object_qid, entry);
+			got_object_qid_free(qid);
+		}
+	}
+	/* User data should be freed by caller. */
+	free(set->ids);
+	free(set);
+}
+
+static uint64_t
+idset_hash(struct got_object_idset *set, struct got_object_id *id)
+{
+	return SipHash24(&set->key, id->sha1, sizeof(id->sha1));
+}
+
+static const struct got_error *
+idset_resize(struct got_object_idset *set, size_t nbuckets)
+{
+	struct got_object_id_queue *ids;
+	size_t i;
+
+	ids = calloc(nbuckets, sizeof(ids[0]));
+	if (ids == NULL) {
+		if (errno != ENOMEM)
+			return got_error_from_errno("calloc");
+		/* Proceed with our current amount of hash buckets. */
+		set->flags |= GOT_OBJECT_IDSET_F_NOMEM;
+		return NULL;
+	}
+
+	for (i = 0; i < nbuckets; i++)
+		STAILQ_INIT(&ids[i]);
+
+	arc4random_buf(&set->key, sizeof(set->key));
 
-	while ((entry = RB_MIN(got_object_idset_tree, &set->entries))) {
-		RB_REMOVE(got_object_idset_tree, &set->entries, entry);
-		/* User data should be freed by caller. */
-		free(entry);
+	for (i = 0; i < set->nbuckets; i++) {
+		while (!STAILQ_EMPTY(&set->ids[i])) {
+			struct got_object_qid *qid;
+			uint64_t idx;
+			qid = STAILQ_FIRST(&set->ids[i]);
+			STAILQ_REMOVE(&set->ids[i], qid, got_object_qid, entry);
+			idx = idset_hash(set, qid->id) % nbuckets;
+			STAILQ_INSERT_HEAD(&ids[idx], qid, entry);
+		}
 	}
 
-	free(set);
+	free(set->ids);
+	set->ids = ids;
+	set->nbuckets = nbuckets;
+	return NULL;
+}
+
+static const struct got_error *
+idset_grow(struct got_object_idset *set)
+{
+	size_t nbuckets;
+
+	if (set->flags & GOT_OBJECT_IDSET_F_NOMEM)
+		return NULL;
+
+	if (set->nbuckets >= UINT_MAX / 2)
+		nbuckets = UINT_MAX;
+	else
+		nbuckets = set->nbuckets * 2;
+
+	return idset_resize(set, nbuckets);
 }
 
 const struct got_error *
 got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
     void *data)
 {
-	struct got_object_idset_element *new;
-
-	if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
-		return got_error(GOT_ERR_NO_SPACE);
+	const struct got_error *err;
+	struct got_object_qid *qid;
+	uint64_t idx;
+	struct got_object_id_queue *head;
 
-	new = malloc(sizeof(*new));
-	if (new == NULL)
-		return got_error_from_errno("malloc");
+	/* This function may resize the set. */
+	if (set->flags & GOT_OBJECT_IDSET_F_TRAVERSAL)
+		return got_error_msg(GOT_ERR_NOT_IMPL,
+		    "cannot add elements to idset during traversal");
 
-	memcpy(&new->id, id, sizeof(new->id));
-	new->data = data;
+	if (set->totelem == UINT_MAX)
+		return got_error(GOT_ERR_NO_SPACE);
+		
+	err = got_object_qid_alloc_partial(&qid);
+	if (err)
+		return err;
+	memcpy(qid->id, id, sizeof(*qid->id));
+	qid->data = data;
+
+	idx = idset_hash(set, id) % set->nbuckets;
+	head = &set->ids[idx];
+	STAILQ_INSERT_HEAD(head, qid, entry);
+	set->totelem++;
 
-	if (RB_INSERT(got_object_idset_tree, &set->entries, new) != NULL) {
-		free(new);
-		return got_error(GOT_ERR_OBJ_EXISTS);
-	}
+	if (set->nbuckets < set->totelem)
+		err = idset_grow(set);
 
-	set->totelem++;
-	return NULL;
+	return err;
 }
 
-static struct got_object_idset_element *
+static struct got_object_qid *
 find_element(struct got_object_idset *set, struct got_object_id *id)
 {
-	struct got_object_idset_element *entry;
-
-	entry = RB_ROOT(&set->entries);
-	while (entry) {
-		int cmp = got_object_id_cmp(id, &entry->id);
-		if (cmp < 0)
-			entry = RB_LEFT(entry, entry);
-		else if (cmp > 0)
-			entry = RB_RIGHT(entry, entry);
-		else
-			break;
+	uint64_t idx = idset_hash(set, id) % set->nbuckets;
+	struct got_object_id_queue *head = &set->ids[idx];
+	struct got_object_qid *qid;
+
+	STAILQ_FOREACH(qid, head, entry) {
+		if (got_object_id_cmp(qid->id, id) == 0)
+			return qid;
 	}
 
-	return entry;
+	return NULL;
 }
 
 void *
 got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
 {
-	struct got_object_idset_element *entry = find_element(set, id);
-	return entry ? entry->data : NULL;
+	struct got_object_qid *qid = find_element(set, id);
+	return qid ? qid->data : NULL;
 }
 
 const struct got_error *
 got_object_idset_remove(void **data, struct got_object_idset *set,
     struct got_object_id *id)
 {
-	struct got_object_idset_element *entry;
+	uint64_t idx;
+	struct got_object_id_queue *head;
+	struct got_object_qid *qid;
 
 	if (data)
 		*data = NULL;
@@ -150,20 +221,30 @@ got_object_idset_remove(void **data, struct got_object_idset *set,
 		return got_error(GOT_ERR_NO_OBJ);
 
 	if (id == NULL) {
-		entry = RB_ROOT(&set->entries);
-		if (entry == NULL)
-			return got_error(GOT_ERR_NO_OBJ);
+		/* Remove a "random" element. */
+		for (idx = 0; idx < set->nbuckets; idx++) {
+			head = &set->ids[idx];
+			qid = STAILQ_FIRST(head);
+			if (qid)
+				break;
+		}
 	} else {
-		entry = find_element(set, id);
-		if (entry == NULL)
+		idx = idset_hash(set, id) % set->nbuckets;
+		head = &set->ids[idx];
+		STAILQ_FOREACH(qid, head, entry) {
+			if (got_object_id_cmp(qid->id, id) == 0)
+				break;
+		}
+		if (qid == NULL)
 			return got_error_no_obj(id);
 	}
 
-	RB_REMOVE(got_object_idset_tree, &set->entries, entry);
 	if (data)
-		*data = entry->data;
-	free(entry);
+		*data = qid->data;
+	STAILQ_REMOVE(head, qid, got_object_qid, entry);
+	got_object_qid_free(qid);
 	set->totelem--;
+
 	return NULL;
 }
 
@@ -171,8 +252,8 @@ int
 got_object_idset_contains(struct got_object_idset *set,
     struct got_object_id *id)
 {
-	struct got_object_idset_element *entry = find_element(set, id);
-	return entry ? 1 : 0;
+	struct got_object_qid *qid = find_element(set, id);
+	return qid ? 1 : 0;
 }
 
 const struct got_error *
@@ -180,15 +261,23 @@ got_object_idset_for_each(struct got_object_idset *set,
     const struct got_error *(*cb)(struct got_object_id *, void *, void *),
     void *arg)
 {
-	const struct got_error *err;
-	struct got_object_idset_element *entry, *tmp;
-
-	RB_FOREACH_SAFE(entry, got_object_idset_tree, &set->entries, tmp) {
-		err = (*cb)(&entry->id, entry->data, arg);
-		if (err)
-			return err;
+	const struct got_error *err = NULL;
+	struct got_object_id_queue *head;
+	struct got_object_qid *qid, *tmp;
+	size_t i;
+
+	set->flags |= GOT_OBJECT_IDSET_F_TRAVERSAL;
+	for (i = 0; i < set->nbuckets; i++) {
+		head = &set->ids[i];
+		STAILQ_FOREACH_SAFE(qid, head, entry, tmp) {
+			err = (*cb)(qid->id, qid->data, arg);
+			if (err)
+				goto done;
+		}
 	}
-	return NULL;
+done:
+	set->flags &= ~GOT_OBJECT_IDSET_F_TRAVERSAL;
+	return err;
 }
 
 int
@@ -196,43 +285,3 @@ got_object_idset_num_elements(struct got_object_idset *set)
 {
 	return set->totelem;
 }
-
-struct got_object_idset_element *
-got_object_idset_get_element(struct got_object_idset *set, struct got_object_id *id)
-{
-	return find_element(set, id);
-}
-
-void *
-got_object_idset_get_element_data(struct got_object_idset_element *entry)
-{
-	return entry->data;
-}
-
-const struct got_error *
-got_object_idset_for_each_element(struct got_object_idset *set,
-    const struct got_error *(*cb)(struct got_object_idset_element *, void *),
-    void *arg)
-{
-	const struct got_error *err;
-	struct got_object_idset_element *entry, *tmp;
-
-	RB_FOREACH_SAFE(entry, got_object_idset_tree, &set->entries, tmp) {
-		err = (*cb)(entry, arg);
-		if (err)
-			return err;
-	}
-	return NULL;
-}
-
-void
-got_object_idset_remove_element(struct got_object_idset *set,
-    struct got_object_idset_element *entry)
-{
-	RB_REMOVE(got_object_idset_tree, &set->entries, entry);
-	free(entry);
-	set->totelem--;
-}
-
-RB_GENERATE(got_object_idset_tree, got_object_idset_element, entry,
-    cmp_elements);
diff --git a/lib/pack_create.c b/lib/pack_create.c
index fa7ea77..81518d5 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -1777,25 +1777,24 @@ done:
 }
 
 static const struct got_error *
-remove_unused_object(struct got_object_idset_element *entry, void *arg)
+remove_unused_object(struct got_object_id *id, void *data, void *arg)
 {
 	struct got_object_idset *idset = arg;
 
-	if (got_object_idset_get_element_data(entry) == NULL)
-		got_object_idset_remove_element(idset, entry);
+	if (data == NULL)
+		got_object_idset_remove(NULL, idset, id);
 
 	return NULL;
 }
 
 static const struct got_error *
-remove_reused_object(struct got_object_idset_element *entry, void *arg)
+remove_reused_object(struct got_object_id *id, void *data, void *arg)
 {
 	struct got_object_idset *idset = arg;
-	struct got_pack_meta *m;
+	struct got_pack_meta *m = data;
 
-	m = got_object_idset_get_element_data(entry);
 	if (m->have_reused_delta)
-		got_object_idset_remove_element(idset, entry);
+		got_object_idset_remove(NULL, idset, id);
 
 	return NULL;
 }
@@ -1840,8 +1839,7 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 	if (err)
 		return err;
 
-	err = got_object_idset_for_each_element(idset,
-	    remove_unused_object, idset);
+	err = got_object_idset_for_each(idset, remove_unused_object, idset);
 	if (err)
 		goto done;
 
@@ -1877,7 +1875,7 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 	if (err)
 		goto done;
 	if (reuse.nmeta > 0) {
-		err = got_object_idset_for_each_element(idset,
+		err = got_object_idset_for_each(idset,
 		    remove_reused_object, idset);
 		if (err)
 			goto done;