From: Jeff King <peff@peff.net> To: Jonathan Nieder <jrnieder@gmail.com> Cc: git@vger.kernel.org, "Junio C Hamano" <gitster@pobox.com>, "Jakub Narebski" <jnareb@gmail.com>, "Ted Ts\'o" <tytso@mit.edu>, "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>, "Clemens Buchacher" <drizzd@aon.at>, "Shawn O. Pearce" <spearce@spearce.org>, "David Barr" <davidbarr@google.com> Subject: [PATCH 3/3] decorate: use "map" for the underlying implementation Date: Thu, 14 Jul 2011 13:53:48 -0400 Message-ID: <20110714175348.GC21771@sigill.intra.peff.net> (raw) In-Reply-To: <20110714173454.GA21657@sigill.intra.peff.net> The decoration API maps objects to void pointers. This is a subset of what the map API is capable of, so let's get rid of the duplicate implementation. We could just fix all callers of decorate to call the map API directly. However, the map API is very generic since it is meant to handle any type. In particular, it can't use sentinel values like "NULL" to indicate "entry not found" (since it doesn't know whether the type can represent such a sentinel value). Instead, the decorate API just becomes a set of wrappers, and no callers need to be updated at all. Signed-off-by: Jeff King <peff@peff.net> --- The result should perform identically to the existing decorate code with the exception of the extra "used" field, which makes each hash entry bigger (see the comments in patch 1/3). decorate.c | 105 ++++++++++-------------------------------------------------- decorate.h | 10 ++---- map.c | 1 + map.h | 1 + 4 files changed, 22 insertions(+), 95 deletions(-) rewrite decorate.c (89%) diff --git a/decorate.c b/decorate.c dissimilarity index 89% index 2f8a63e..31e9656 100644 --- a/decorate.c +++ b/decorate.c @@ -1,88 +1,17 @@ -/* - * decorate.c - decorate a git object with some arbitrary - * data. - */ -#include "cache.h" -#include "object.h" -#include "decorate.h" - -static unsigned int hash_obj(const struct object *obj, unsigned int n) -{ - unsigned int hash; - - memcpy(&hash, obj->sha1, sizeof(unsigned int)); - return hash % n; -} - -static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration) -{ - int size = n->size; - struct object_decoration *hash = n->hash; - unsigned int j = hash_obj(base, size); - - while (hash[j].base) { - if (hash[j].base == base) { - void *old = hash[j].decoration; - hash[j].decoration = decoration; - return old; - } - if (++j >= size) - j = 0; - } - hash[j].base = base; - hash[j].decoration = decoration; - n->nr++; - return NULL; -} - -static void grow_decoration(struct decoration *n) -{ - int i; - int old_size = n->size; - struct object_decoration *old_hash = n->hash; - - n->size = (old_size + 1000) * 3 / 2; - n->hash = xcalloc(n->size, sizeof(struct object_decoration)); - n->nr = 0; - - for (i = 0; i < old_size; i++) { - const struct object *base = old_hash[i].base; - void *decoration = old_hash[i].decoration; - - if (!base) - continue; - insert_decoration(n, base, decoration); - } - free(old_hash); -} - -/* Add a decoration pointer, return any old one */ -void *add_decoration(struct decoration *n, const struct object *obj, - void *decoration) -{ - int nr = n->nr + 1; - - if (nr > n->size * 2 / 3) - grow_decoration(n); - return insert_decoration(n, obj, decoration); -} - -/* Lookup a decoration pointer */ -void *lookup_decoration(struct decoration *n, const struct object *obj) -{ - unsigned int j; - - /* nothing to lookup */ - if (!n->size) - return NULL; - j = hash_obj(obj, n->size); - for (;;) { - struct object_decoration *ref = n->hash + j; - if (ref->base == obj) - return ref->decoration; - if (!ref->base) - return NULL; - if (++j == n->size) - j = 0; - } -} +#include "cache.h" +#include "decorate.h" + +void *add_decoration(struct decoration *n, const struct object *obj, + void *decoration) +{ + void *ret = NULL; + map_set_object_void(&n->map, obj, decoration, &ret); + return ret; +} + +void *lookup_decoration(struct decoration *n, const struct object *obj) +{ + void *ret = NULL; + map_get_object_void(&n->map, obj, &ret); + return ret; +} diff --git a/decorate.h b/decorate.h index e732804..6a3adcd 100644 --- a/decorate.h +++ b/decorate.h @@ -1,15 +1,11 @@ #ifndef DECORATE_H #define DECORATE_H -struct object_decoration { - const struct object *base; - void *decoration; -}; +#include "map.h" struct decoration { - const char *name; - unsigned int size, nr; - struct object_decoration *hash; + char *name; + struct map_object_void map; }; extern void *add_decoration(struct decoration *n, const struct object *obj, void *decoration); diff --git a/map.c b/map.c index 28f885e..93e0364 100644 --- a/map.c +++ b/map.c @@ -86,3 +86,4 @@ int map_get_##name(struct map_##name *m, \ } MAP_IMPLEMENT(object_uint32, struct object *, uint32_t, cmp_obj, hash_obj) +MAP_IMPLEMENT(object_void, struct object *, void *, cmp_obj, hash_obj) diff --git a/map.h b/map.h index e80d85d..737054e 100644 --- a/map.h +++ b/map.h @@ -22,5 +22,6 @@ extern int map_set_##name(struct map_##name *, \ vtype *old); \ DECLARE_MAP(object_uint32, struct object *, uint32_t) +DECLARE_MAP(object_void, struct object *, void *) #endif /* MAP_H */ -- 1.7.6.38.ge5b33
next prev parent reply index Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top 2011-07-13 6:47 [RFC/PATCHv2 0/6] generation numbers for faster traversals Jeff King 2011-07-13 6:57 ` [RFC/PATCHv2 1/6] decorate: allow storing values instead of pointers Jeff King 2011-07-13 17:52 ` Jonathan Nieder 2011-07-13 20:08 ` Jeff King 2011-07-14 17:34 ` Jeff King 2011-07-14 17:51 ` [PATCH 1/3] implement generic key/value map Jeff King 2011-07-14 18:52 ` Bert Wesarg 2011-07-14 18:54 ` Bert Wesarg 2011-07-14 18:55 ` Jeff King 2011-07-14 19:07 ` Bert Wesarg 2011-07-14 19:14 ` Jeff King 2011-07-14 19:18 ` Bert Wesarg 2011-07-14 17:52 ` [PATCH 2/3] fast-export: use object to uint32 map instead of "decorate" Jeff King 2011-07-15 9:40 ` Sverre Rabbelier 2011-07-15 20:00 ` Jeff King 2011-07-14 17:53 ` Jeff King [this message] 2011-07-14 21:06 ` [RFC/PATCHv2 1/6] decorate: allow storing values instead of pointers Junio C Hamano 2011-08-04 22:43 ` [RFC/PATCH 0/5] macro-based key/value maps Jeff King 2011-08-04 22:45 ` [PATCH 1/5] implement generic key/value map Jeff King 2011-08-04 22:46 ` [PATCH 2/5] fast-export: use object to uint32 map instead of "decorate" Jeff King 2011-08-04 22:46 ` [PATCH 3/5] decorate: use "map" for the underlying implementation Jeff King 2011-08-04 22:46 ` [PATCH 4/5] map: implement persistent maps Jeff King 2011-08-04 22:46 ` [PATCH 5/5] implement metadata cache subsystem Jeff King 2011-08-05 11:03 ` [RFC/PATCH 0/5] macro-based key/value maps Jeff King 2011-08-05 15:31 ` René Scharfe 2011-08-06 6:30 ` Jeff King 2011-07-13 7:04 ` [RFC/PATCHv2 2/6] add metadata-cache infrastructure Jeff King 2011-07-13 8:18 ` Bert Wesarg 2011-07-13 8:31 ` Jeff King 2011-07-13 8:45 ` Bert Wesarg 2011-07-13 19:18 ` Jeff King 2011-07-13 19:40 ` Junio C Hamano 2011-07-13 19:33 ` Junio C Hamano 2011-07-13 20:25 ` Jeff King 2011-07-13 7:05 ` [RFC/PATCHv2 3/6] commit: add commit_generation function Jeff King 2011-07-13 14:26 ` Eric Sunshine 2011-07-13 7:05 ` [RFC/PATCHv2 4/6] pretty: support %G to show the generation number of a commit Jeff King 2011-07-13 7:06 ` [RFC/PATCHv2 5/6] check commit generation cache validity against grafts Jeff King 2011-07-13 14:26 ` Eric Sunshine 2011-07-13 19:35 ` Jeff King 2011-07-13 7:06 ` [RFC/PATCHv2 6/6] limit "contains" traversals based on commit generation Jeff King 2011-07-13 7:23 ` Jeff King 2011-07-13 20:33 ` Junio C Hamano 2011-07-13 20:58 ` Jeff King 2011-07-13 21:12 ` Junio C Hamano 2011-07-13 21:18 ` Jeff King 2011-07-15 18:22 ` Junio C Hamano 2011-07-15 20:40 ` Jeff King 2011-07-15 21:04 ` Junio C Hamano 2011-07-15 21:14 ` Jeff King 2011-07-15 21:01 ` Generation numbers and replacement objects Jakub Narebski 2011-07-15 21:10 ` Jeff King 2011-07-16 21:10 ` Jakub Narebski 2011-08-04 22:48 [RFC/PATCH 0/2] patch-id caching Jeff King 2011-08-04 22:49 ` [PATCH 1/2] cherry: read default config Jeff King 2011-08-04 22:49 ` [PATCH 2/2] cache patch ids on disk Jeff King 2011-08-04 22:52 ` Jeff King
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20110714175348.GC21771@sigill.intra.peff.net \ --to=peff@peff.net \ --cc=avarab@gmail.com \ --cc=davidbarr@google.com \ --cc=drizzd@aon.at \ --cc=git@vger.kernel.org \ --cc=gitster@pobox.com \ --cc=jnareb@gmail.com \ --cc=jrnieder@gmail.com \ --cc=spearce@spearce.org \ --cc=tytso@mit.edu \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Mailing List Archive on lore.kernel.org Archives are clonable: git clone --mirror https://lore.kernel.org/git/0 git/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 git git/ https://lore.kernel.org/git \ git@vger.kernel.org public-inbox-index git Example config snippet for mirrors Newsgroup available over NNTP: nntp://nntp.lore.kernel.org/org.kernel.vger.git AGPL code for this site: git clone https://public-inbox.org/public-inbox.git