Skip to content

Commit

Permalink
Merge pull request #217 from alexlarsson/performance
Browse files Browse the repository at this point in the history
Some small performance fixes
  • Loading branch information
cgwalters authored Oct 11, 2023
2 parents ce6abac + 8cd12a1 commit 35eb02e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions libcomposefs/lcfs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ struct lcfs_node_s {
struct lcfs_node_s *parent;

struct lcfs_node_s **children; /* Owns refs */
size_t children_capacity;
size_t children_size;

/* Used to create hard links. */
Expand Down
13 changes: 8 additions & 5 deletions libcomposefs/lcfs-writer-erofs.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ static bool xattrs_ht_comparator(const void *d1, const void *d2)
const struct hasher_xattr_s *v1 = d1;
const struct hasher_xattr_s *v2 = d2;

if (strcmp(v1->xattr->key, v2->xattr->key) != 0)
if (v1->xattr->value_len != v2->xattr->value_len)
return false;

if (v1->xattr->value_len != v2->xattr->value_len)
if (memcmp(v1->xattr->value, v2->xattr->value, v1->xattr->value_len) != 0)
return false;

return memcmp(v1->xattr->value, v2->xattr->value, v1->xattr->value_len) == 0;
return strcmp(v1->xattr->key, v2->xattr->key) == 0;
}

/* Sort alphabetically by key and value to get some canonical order */
Expand Down Expand Up @@ -328,9 +328,12 @@ static int compute_erofs_shared_xattrs(struct lcfs_ctx_s *ctx)
size_t n_xattrs;
uint64_t xattr_offset;

/* Find the use count for each xattr key/value in use */
size_t n_files = 0;
for (node = ctx->root; node != NULL; node = node->next)
n_files++;

xattr_hash = hash_initialize(0, NULL, xattrs_ht_hasher,
/* Find the use count for each xattr key/value in use */
xattr_hash = hash_initialize(n_files, NULL, xattrs_ht_hasher,
xattrs_ht_comparator, free);
if (xattr_hash == NULL) {
return -1;
Expand Down
32 changes: 19 additions & 13 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ size_t hash_memory(const char *string, size_t len, size_t n_buckets)
size_t i, value = 0;

for (i = 0; i < len; i++) {
value = (value * 31 + string[i]) % n_buckets;
value = (value * 31 + string[i]);
}
return value;
return value % n_buckets;
}

static struct lcfs_ctx_s *lcfs_new_ctx(struct lcfs_node_s *root,
Expand Down Expand Up @@ -906,7 +906,7 @@ int lcfs_node_add_child(struct lcfs_node_s *parent, struct lcfs_node_s *child,
const char *name)
{
struct lcfs_node_s **new_children;
size_t new_size;
size_t new_capacity;
char *name_copy;

if ((parent->inode.st_mode & S_IFMT) != S_IFDIR) {
Expand Down Expand Up @@ -936,20 +936,26 @@ int lcfs_node_add_child(struct lcfs_node_s *parent, struct lcfs_node_s *child,
return -1;
}

new_size = parent->children_size + 1;
if (parent->children_capacity == parent->children_size) {
if (parent->children_size == 0)
new_capacity = 16;
else
new_capacity = parent->children_capacity * 2;

new_children = reallocarray(parent->children, sizeof(*parent->children),
new_size);
if (new_children == NULL) {
errno = ENOMEM;
free(name_copy);
return -1;
}
new_children = reallocarray(parent->children,
sizeof(*parent->children), new_capacity);
if (new_children == NULL) {
errno = ENOMEM;
free(name_copy);
return -1;
}

parent->children = new_children;
parent->children = new_children;
parent->children_capacity = new_capacity;
}

parent->children[parent->children_size] = child;
parent->children_size = new_size;
parent->children_size += 1;
child->parent = parent;
child->name = name_copy;

Expand Down

0 comments on commit 35eb02e

Please sign in to comment.