Re: [[PATCH v2] 1/4] patch-ids: stop using a hand-rolled hashmap implementation
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jul 29, 2016, 20:47 UTC
- Message-ID
- <xmqqoa5gmas6.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <20160729161920.3792-2-kcwillford@gmail.com>
Kevin Willford <kcwillford@gmail.com> writes:
Show 11 quoted lines
> From: Kevin Willford <kewillf@microsoft.com> > > This change will use the hashmap from the hashmap.h to keep track of the > patch_ids that have been encountered instead of using an internal > implementation. This simplifies the implementation of the patch ids. > > Signed-off-by: Kevin Willford <kcwillford@gmail.com> > --- > patch-ids.c | 86 +++++++++++++++++++++---------------------------------------- > patch-ids.h | 7 +++-- > 2 files changed, 32 insertions(+), 61 deletions(-)
The patch text itself is almost unreadble because of a lot of verbose code it had to carry before this change, and the removal of that unreadable code of course is the point of this very welcome clean-up ;-). The resulting code is very readable.
Show 22 quoted lines
> struct patch_id *has_commit_patch_id(struct commit *commit,
> struct patch_ids *ids)
> {
> - return add_commit(commit, ids, 1);
> + struct patch_id patch;
> +
> + memset(&patch, 0, sizeof(patch));
> + if (init_patch_id_entry(&patch, commit, ids))
> + return NULL;
> + return hashmap_get(&ids->patches, &patch, NULL);
> }
>
> struct patch_id *add_commit_patch_id(struct commit *commit,
> struct patch_ids *ids)
> {
> - return add_commit(commit, ids, 0);
> + struct patch_id *key = xcalloc(1, sizeof(*key));
> +
> + if (init_patch_id_entry(key, commit, ids)) {
> + free(key);
> + return NULL;
> + }This is a tangent, but this made me wonder if it is safe to simply free(3) the result of calling hashmap_entry_init() which is called in init_patch_id_entry(). It would obviously become a resource leak, if a hashmap_entry (which the api documentation says is "an opaque structure") holds any allocated resource.
The fact that hashmap_entry_init() is there but there is no corresponding hashmap_entry_clear() hints that there is nothing to be worried about and I can see from the implementation of hashmap_entry_init() that no extra resource is held inside, but an API user should not have to guess. We may want to do one of the two things:
* document that an embedded hashmap_entry does not hold any resource that need to be released and it is safe to free the user structure that embeds one; or
* implement hashmap_entry_clear() that currently is a no-op.
If we anticipate that the hashmap implementation may gain more fields in this "opaque" structure, the latter might be a more future-proof approach, as all the callers of hashmap_entry_init() would already be calling hashmap_entry_clear() to clean it up when such a change to the hashmap implementation happens. On the other hand, a caller that does not call hashmap_entry_clear() would not be noticed by anybody as leaking resources until such a change happens, so the future-proofing may not have much practical value (iow, the existing callers of _init() would need to be audited anyway to make sure they also call _clear()).