Skip to content

Commit

Permalink
Merge branch 'pr/remotes-in-hashmap'
Browse files Browse the repository at this point in the history
Optimize remotes configuration look-up in a repository with very
many remotes defined.

* pr/remotes-in-hashmap:
  use a hashmap to make remotes faster
  • Loading branch information
Junio C Hamano committed Sep 2, 2014
2 parents 44ceb79 + d0da003 commit 0e28161
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
63 changes: 46 additions & 17 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct rewrites {
static struct remote **remotes;
static int remotes_alloc;
static int remotes_nr;
static struct hashmap remotes_hash;

static struct branch **branches;
static int branches_alloc;
Expand Down Expand Up @@ -136,26 +137,51 @@ static void add_url_alias(struct remote *remote, const char *url)
add_pushurl_alias(remote, url);
}

struct remotes_hash_key {
const char *str;
int len;
};

static int remotes_hash_cmp(const struct remote *a, const struct remote *b, const struct remotes_hash_key *key)
{
if (key)
return strncmp(a->name, key->str, key->len) || a->name[key->len];
else
return strcmp(a->name, b->name);
}

static inline void init_remotes_hash(void)
{
if (!remotes_hash.cmpfn)
hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, 0);
}

static struct remote *make_remote(const char *name, int len)
{
struct remote *ret;
int i;
struct remote *ret, *replaced;
struct remotes_hash_key lookup;
struct hashmap_entry lookup_entry;

for (i = 0; i < remotes_nr; i++) {
if (len ? (!strncmp(name, remotes[i]->name, len) &&
!remotes[i]->name[len]) :
!strcmp(name, remotes[i]->name))
return remotes[i];
}
if (!len)
len = strlen(name);

init_remotes_hash();
lookup.str = name;
lookup.len = len;
hashmap_entry_init(&lookup_entry, memhash(name, len));

if ((ret = hashmap_get(&remotes_hash, &lookup_entry, &lookup)) != NULL)
return ret;

ret = xcalloc(1, sizeof(struct remote));
ret->prune = -1; /* unspecified */
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
remotes[remotes_nr++] = ret;
if (len)
ret->name = xstrndup(name, len);
else
ret->name = xstrdup(name);
ret->name = xstrndup(name, len);

hashmap_entry_init(ret, lookup_entry.hash);
replaced = hashmap_put(&remotes_hash, ret);
assert(replaced == NULL); /* no previous entry overwritten */
return ret;
}

Expand Down Expand Up @@ -717,13 +743,16 @@ struct remote *pushremote_get(const char *name)

int remote_is_configured(const char *name)
{
int i;
struct remotes_hash_key lookup;
struct hashmap_entry lookup_entry;
read_config();

for (i = 0; i < remotes_nr; i++)
if (!strcmp(name, remotes[i]->name))
return 1;
return 0;
init_remotes_hash();
lookup.str = name;
lookup.len = strlen(name);
hashmap_entry_init(&lookup_entry, memhash(name, lookup.len));

return hashmap_get(&remotes_hash, &lookup_entry, &lookup) != NULL;
}

int for_each_remote(each_remote_fn fn, void *priv)
Expand Down
3 changes: 3 additions & 0 deletions remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define REMOTE_H

#include "parse-options.h"
#include "hashmap.h"

enum {
REMOTE_CONFIG,
Expand All @@ -10,6 +11,8 @@ enum {
};

struct remote {
struct hashmap_entry ent; /* must be first */

const char *name;
int origin;

Expand Down

0 comments on commit 0e28161

Please sign in to comment.