Skip to content

Commit 6232086

Browse files
committed
config_entries: only keep track of a single entry list
Whenever adding a configuration entry to the config entries structure, we allocate two list heads: - The first list head is added to the global list of config entries in order to be able to iterate over configuration entries in the order they were originally added. - The second list head is added to the map of entries in order to efficiently look up an entry by its name. If no entry with the same name exists in the map, then we add the new entry to the map directly. Otherwise, we append the new entry's list head to the pre-existing entry's list in order to keep track of multivars. While the former usecase is perfectly sound, the second usecase can be optimized. The only reason why we keep track of multivar entries in another separate list is to be able to determine whether an entry is unique or not by seeing whether its `next` pointer is set. So we keep track of a complete list of multivar entries just to have a single bit of information of whether it has other multivar entries with the same entry name. We can completely get rid of this secondary list by just adding a `first` field to the list structure itself. When executing `git_config_entries_append`, we will then simply check whether the configuration map already has an entry with the same name -- if so, we will set the `first` to zero to indicate that it is not the initial entry anymore. Instead of a second list head in the map, we can thus now directly store the list head of the first global list inside of the map and just refer to that bit. Note that the more obvious solution would be to store a `unique` field instead of a `first` field. But as we will only ever inspect the `first` field of the _last_ entry that has been moved into the map, these are semantically equivalent in that case. Having a `first` field also allows for a minor optimization: for multivar values, we can free the `name` field of all entries that are _not_ first and have them point to the name of the first entry instead.
1 parent 8a88701 commit 6232086

File tree

1 file changed

+22
-73
lines changed

1 file changed

+22
-73
lines changed

src/config_entries.c

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct config_entry_list {
1111
struct config_entry_list *next;
1212
struct config_entry_list *last;
1313
git_config_entry *entry;
14+
bool first;
1415
} config_entry_list;
1516

1617
typedef struct config_entries_iterator {
@@ -25,31 +26,6 @@ struct git_config_entries {
2526
config_entry_list *list;
2627
};
2728

28-
static void config_entry_list_free(config_entry_list *list)
29-
{
30-
config_entry_list *next;
31-
32-
while (list != NULL) {
33-
next = list->next;
34-
35-
git__free((char*) list->entry->name);
36-
git__free((char *) list->entry->value);
37-
git__free(list->entry);
38-
git__free(list);
39-
40-
list = next;
41-
};
42-
}
43-
44-
static void config_entry_list_append(config_entry_list **list, config_entry_list *entry)
45-
{
46-
if (*list)
47-
(*list)->last->next = entry;
48-
else
49-
*list = entry;
50-
(*list)->last = entry;
51-
}
52-
5329
int git_config_entries_new(git_config_entries **out)
5430
{
5531
git_config_entries *entries;
@@ -127,12 +103,14 @@ static void config_entries_free(git_config_entries *entries)
127103
{
128104
config_entry_list *list = NULL, *next;
129105

130-
git_strmap_foreach_value(entries->map, list, config_entry_list_free(list));
131106
git_strmap_free(entries->map);
132107

133108
list = entries->list;
134109
while (list != NULL) {
135110
next = list->next;
111+
git__free((char *) list->entry->name);
112+
git__free((char *) list->entry->value);
113+
git__free(list->entry);
136114
git__free(list);
137115
list = next;
138116
}
@@ -148,71 +126,42 @@ void git_config_entries_free(git_config_entries *entries)
148126

149127
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
150128
{
151-
config_entry_list *existing, *var;
152-
int error = 0;
153-
154-
var = git__calloc(1, sizeof(config_entry_list));
155-
GIT_ERROR_CHECK_ALLOC(var);
156-
var->entry = entry;
157-
158-
if ((existing = git_strmap_get(entries->map, entry->name)) == NULL) {
159-
/*
160-
* We only ever inspect `last` from the first config
161-
* entry in a multivar. In case where this new entry is
162-
* the first one in the entry map, it will also be the
163-
* last one at the time of adding it, which is
164-
* why we set `last` here to itself. Otherwise we
165-
* do not have to set `last` and leave it set to
166-
* `NULL`.
167-
*/
168-
var->last = var;
169-
170-
error = git_strmap_set(entries->map, entry->name, var);
171-
} else {
172-
config_entry_list_append(&existing, var);
173-
}
174-
175-
var = git__calloc(1, sizeof(config_entry_list));
176-
GIT_ERROR_CHECK_ALLOC(var);
177-
var->entry = entry;
178-
config_entry_list_append(&entries->list, var);
179-
180-
return error;
181-
}
129+
config_entry_list *head;
182130

183-
static int config_entry_get(config_entry_list **out, git_config_entries *entries, const char *key)
184-
{
185-
config_entry_list *list;
131+
head = git__calloc(1, sizeof(config_entry_list));
132+
GIT_ERROR_CHECK_ALLOC(head);
133+
head->entry = entry;
134+
head->first = (git_strmap_get(entries->map, entry->name) == NULL);
186135

187-
if ((list = git_strmap_get(entries->map, key)) == NULL)
188-
return GIT_ENOTFOUND;
136+
if (entries->list)
137+
entries->list->last->next = head;
138+
else
139+
entries->list = head;
140+
entries->list->last = head;
189141

190-
*out = list;
142+
if (git_strmap_set(entries->map, entry->name, head) < 0)
143+
return -1;
191144

192145
return 0;
193146
}
194147

195148
int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key)
196149
{
197150
config_entry_list *entry;
198-
int error;
199-
200-
if ((error = config_entry_get(&entry, entries, key)) < 0)
201-
return error;
202-
*out = entry->last->entry;
203-
151+
if ((entry = git_strmap_get(entries->map, key)) == NULL)
152+
return GIT_ENOTFOUND;
153+
*out = entry->entry;
204154
return 0;
205155
}
206156

207157
int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key)
208158
{
209159
config_entry_list *entry;
210-
int error;
211160

212-
if ((error = config_entry_get(&entry, entries, key)) < 0)
213-
return error;
161+
if ((entry = git_strmap_get(entries->map, key)) == NULL)
162+
return GIT_ENOTFOUND;
214163

215-
if (entry->next != NULL) {
164+
if (!entry->first) {
216165
git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar");
217166
return -1;
218167
}

0 commit comments

Comments
 (0)