From 51c515637945ab649535e4ef4e42b82d9eaa9f7a Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 10 Oct 2023 18:20:42 +0200 Subject: [PATCH] Improve performance of xattr hashing 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 --- libcomposefs/lcfs-writer-erofs.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libcomposefs/lcfs-writer-erofs.c b/libcomposefs/lcfs-writer-erofs.c index 2f4bfd34..62055338 100644 --- a/libcomposefs/lcfs-writer-erofs.c +++ b/libcomposefs/lcfs-writer-erofs.c @@ -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 */ @@ -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;