reduce the amount of memcmp() calls via got_object_idset_add()
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
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index da78aa1..d0e80bc 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -289,11 +289,8 @@ advance_branch(struct got_commit_graph *graph,
qid = SIMPLEQ_FIRST(&commit->parent_ids);
if (qid == NULL)
return NULL;
- err = got_object_idset_add(NULL, graph->open_branches, qid->id,
- node);
- if (err && err->code != GOT_ERR_OBJ_EXISTS)
- return err;
- return NULL;
+ err = got_object_idset_add(graph->open_branches, qid->id, node);
+ return err;
}
/*
@@ -335,11 +332,9 @@ advance_branch(struct got_commit_graph *graph,
* skip any other branches.
*/
if (got_object_id_cmp(merged_id, id) == 0) {
- err = got_object_idset_add(NULL,
- graph->open_branches, qid->id, node);
- if (err && err->code != GOT_ERR_OBJ_EXISTS)
- return err;
- return NULL;
+ err = got_object_idset_add(graph->open_branches,
+ qid->id, node);
+ return err;
}
}
@@ -353,20 +348,20 @@ advance_branch(struct got_commit_graph *graph,
return NULL;
if (got_object_idset_get(graph->node_ids, qid->id))
return NULL; /* parent already traversed */
- err = got_object_idset_add(NULL,
- graph->open_branches, qid->id, node);
- if (err && err->code != GOT_ERR_OBJ_EXISTS)
- return err;
- return NULL;
+ if (got_object_idset_get(graph->open_branches, qid->id))
+ return NULL;
+ return got_object_idset_add(graph->open_branches,
+ qid->id, node);
}
}
SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
if (got_object_idset_get(graph->node_ids, qid->id))
continue; /* parent already traversed */
- err = got_object_idset_add(NULL, graph->open_branches,
- qid->id, node);
- if (err && err->code != GOT_ERR_OBJ_EXISTS)
+ if (got_object_idset_get(graph->open_branches, qid->id))
+ continue;
+ err = got_object_idset_add(graph->open_branches, qid->id, node);
+ if (err)
return err;
}
@@ -412,10 +407,8 @@ add_node(struct got_commit_graph_node **new_node,
node->nparents++;
}
- err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
+ err = got_object_idset_add(graph->node_ids, &node->id, node);
if (err) {
- if (err->code == GOT_ERR_OBJ_EXISTS)
- err = NULL;
free_node(node);
return err;
}
diff --git a/lib/got_lib_object_idset.h b/lib/got_lib_object_idset.h
index a64e2b7..aad9204 100644
--- a/lib/got_lib_object_idset.h
+++ b/lib/got_lib_object_idset.h
@@ -19,8 +19,8 @@ struct got_object_idset;
struct got_object_idset *got_object_idset_alloc(void);
void got_object_idset_free(struct got_object_idset *);
-const struct got_error *got_object_idset_add(void **,
- struct got_object_idset *, struct got_object_id *, void *);
+const struct got_error *got_object_idset_add(struct got_object_idset *,
+ struct got_object_id *, void *);
void *got_object_idset_get(struct got_object_idset *, struct got_object_id *);
const struct got_error *got_object_idset_remove(void **,
struct got_object_idset *, struct got_object_id *);
diff --git a/lib/object_idset.c b/lib/object_idset.c
index a2cbecc..d611952 100644
--- a/lib/object_idset.c
+++ b/lib/object_idset.c
@@ -88,15 +88,12 @@ got_object_idset_free(struct got_object_idset *set)
}
const struct got_error *
-got_object_idset_add(void **existing_data,
- struct got_object_idset *set, struct got_object_id *id, void *data)
+got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
+ void *data)
{
- struct got_object_idset_element *new, *entry;
+ struct got_object_idset_element *new;
uint8_t i = id->sha1[0];
- if (existing_data)
- *existing_data = NULL;
-
if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
return got_error(GOT_ERR_NO_SPACE);
@@ -107,43 +104,10 @@ got_object_idset_add(void **existing_data,
memcpy(&new->id, id, sizeof(new->id));
new->data = data;
- if (TAILQ_EMPTY(&set->entries[i])) {
- TAILQ_INSERT_HEAD(&set->entries[i], new, entry);
- set->nelem[i]++;
- set->totelem++;
- return NULL;
- }
-
- /*
- * Keep the list sorted by ID so that iterations of
- * the set occur in a predictable order.
- */
- TAILQ_FOREACH(entry, &set->entries[i], entry) {
- int cmp = got_object_id_cmp(&new->id, &entry->id);
- struct got_object_idset_element *next;
-
- if (cmp == 0) {
- free(new);
- if (existing_data)
- *existing_data = entry->data;
- return got_error(GOT_ERR_OBJ_EXISTS);
- } else if (cmp < 0) {
- TAILQ_INSERT_BEFORE(entry, new, entry);
- set->nelem[i]++;
- set->totelem++;
- return NULL;
- }
-
- next = TAILQ_NEXT(entry, entry);
- if (next == NULL) {
- TAILQ_INSERT_AFTER(&set->entries[i], entry, new, entry);
- set->nelem[i]++;
- set->totelem++;
- return NULL;
- }
- }
-
- return got_error(GOT_ERR_BAD_OBJ_ID); /* should not get here */
+ TAILQ_INSERT_HEAD(&set->entries[i], new, entry);
+ set->nelem[i]++;
+ set->totelem++;
+ return NULL;
}
void *
diff --git a/regress/idset/idset_test.c b/regress/idset/idset_test.c
index 7d0f1de..a3971bd 100644
--- a/regress/idset/idset_test.c
+++ b/regress/idset/idset_test.c
@@ -54,17 +54,13 @@ static const char *id_str2 = "2222222222222222222222222222222222222222";
static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
static struct got_object_id id1, id2, id3;
static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
-static int iter_count;
static void
idset_cb(struct got_object_id *id, void *data, void *arg) {
- if (iter_count == 0 &&
- (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
- abort();
- if (iter_count == 1 &&
- (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
- abort();
- iter_count++;
+ if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
+ (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
+ return;
+ abort();
}
static int
@@ -72,7 +68,6 @@ idset_add_remove_iter(void)
{
const struct got_error *err = NULL;
struct got_object_idset *set;
- void *existing_data;
set = got_object_idset_alloc();
if (set == NULL) {
@@ -97,13 +92,9 @@ idset_add_remove_iter(void)
goto done;
}
- err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
+ err = got_object_idset_add(set, &id1, (void *)data1);
if (err)
goto done;
- if (existing_data != NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
- }
if (got_object_idset_num_elements(set) != 1) {
err = got_error(GOT_ERR_BAD_OBJ_DATA);
goto done;
@@ -114,24 +105,9 @@ idset_add_remove_iter(void)
goto done;
}
- err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
+ err = got_object_idset_add(set, &id2, (void *)data2);
if (err)
goto done;
- if (existing_data != NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
- }
- err = got_object_idset_add(&existing_data, set, &id2, NULL);
- if (existing_data == NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
- }
- if (err->code != GOT_ERR_OBJ_EXISTS)
- goto done;
- err = got_object_idset_add(NULL, set, &id2, NULL);
- if (err->code != GOT_ERR_OBJ_EXISTS)
- goto done;
- err = NULL;
if (!got_object_idset_contains(set, &id1)) {
err = got_error(GOT_ERR_BAD_OBJ_DATA);
@@ -146,7 +122,7 @@ idset_add_remove_iter(void)
goto done;
}
- err = got_object_idset_add(NULL, set, &id3, (void *)data3);
+ err = got_object_idset_add(set, &id3, (void *)data3);
if (err)
goto done;