Skip to content

Commit

Permalink
Improve performance of xattr hashing
Browse files Browse the repository at this point in the history
Make check is really slow, and a lot of it is in
compute_erofs_shared_xattrs() when doing hash comparisons.

We improve performance by using a better estimate of the initial
hash-table size, and we compare the xattrn values before the xatt key
names (as the kay names are often the same).

This drops a mkcomposefs --from-file run from 5 seconds to 1 second.

Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Oct 10, 2023
1 parent 67c5c46 commit 51c5156
Showing 1 changed file with 8 additions and 5 deletions.
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

0 comments on commit 51c5156

Please sign in to comment.