erofs-utils: improve performance of mkfs with dedupe
cover erofs-utils: improve performance of mkfs with dedupe
I got a report from AOSP user that performance of mkfs.erofs with dedupe option that mkfs.erofs time increased to very high number. For example creation of 8GB uncompressed erofs image increased from 36seconds to 27minutes when dedupe was enabled. After profiling mkfs.erofs for sample data, I observed that the actual increased in time was coming from erofs_blob_exit() and debugging further it showed that real inefficiency was coming from hashmap_iter_first() which starts scanning for the first element from tablepos = 0 always.
The following patches solve this by
- creating a helper function to disable hashmap shrinking
- using hashmap_iter_next() to avoid scanning from 0 and as rehashing is disabled it is guaranteed to go through all the elements even while doing hashmap_remove().
Test results now show order of magnitude improvements for larger filesystem size.
You can verify the improvements with below steps
$ mkdir fs_data $ dd if=/dev/urandom of=fs_data/random_file.bin bs=1M count=8192 $ time mkfs.erofs --chunksize=4096 erofs_dedupe.img fs_data
fs_size Before After Improvement 1G 23s 7s 3.2x 2G 81s 15s 5.4x 4G 272s 31s 8.77x 8G 1252s 61s 20.52x
Thanks,
Sandeep
Sandeep Dhavale (2):
erofs-utils: lib: provide helper to disable hashmap shrinking
erofs-utils: lib: improve freeing hashmap in erofs_blob_exit()
include/erofs/hashmap.h | 4 ++++ lib/blobchunk.c | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-)
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
PATCH 1/2 erofs-utils: lib: provide helper to disable hashmap shrinking
This helper sets hasmap.shrink_at to 0. This is helpful to iterate over hashmap using hashmap_iter_next() and use hashmap_remove() in single pass efficeintly.
--- include/erofs/hashmap.h | 4 ++++ 1 file changed, 4 insertions(+)
include/erofs/hashmap.h +4 -0
@@ -97,6 +97,10 @@ static inline void *hashmap_iter_first(struct hashmap *map,return hashmap_iter_next (iter); } +static inline void hashmap_disable_shrink (struct hashmap * map) +{ + map->shrink_at =0 ; +}/* string interning */ const void *memintern (const void *data,size_t len);static inline const char *strintern (const char *string)
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH 1/2] erofs-utils: lib: provide helper to disable hashmap shrinking
Quoted 5 lines · Sandeep Dhavale
This helper sets hasmap.shrink_at to 0. This is helpful to iterate over hashmap using
hashmap_iter_next()and usehashmap_remove()in single pass efficeintly.Signed-off-by Sandeep Dhavale <[email protected]>
Thanks,
Gao Xiang
PATCH 2/2 erofs-utils: lib: improve freeing hashmap in erofs_blob_exit()
In reply to Sandeep Dhavale via Linux-erofsDepending on size of the filesystem being built there can be huge number of elements in the hashmap. Currently we call hashmap_iter_first() in while loop to iterate and free the elements. However technically correct, this is inefficient in 2 aspects.
- As we are iterating the elements for removal, we do not need overhead of rehashing.
- Second part which contributes hugely to the performance is using hashmap_iter_first() as it starts scanning from index 0 throwing away the previous successful scan. For sparsely populated hashmap this becomes O(n^2) in worst case.
Lets fix this by disabling hashmap shrink which avoids rehashing and use hashmap_iter_next() which is now guaranteed to iterate over all the elements while removing while avoiding the performance pitfalls of using hashmap_iter_first().
Test with random data shows performance improvement as:
fs_size Before After
1G 23s 7s
2G 81s 15s
4G 272s 31s
8G 1252s 61s
--- lib/blobchunk.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
lib/blobchunk.c +7 -1
@@ -548,11 +548,17 @@ void erofs_blob_exit(void)if (blobfile)fclose (blobfile); -while ((e =hashmap_iter_first (&blob_hashmap, &iter))) { +/* Disable hashmap shrink, effectively disabling rehash. +* This way we can iterate over entire hashmap efficiently +* and safely by using hashmap_iter_next() */ +hashmap_disable_shrink (&blob_hashmap); + e =hashmap_iter_first (&blob_hashmap, &iter); +while (e) { bc =container_of ((struct hashmap_entry *)e, struct erofs_blobchunk, ent);DBG_BUGON (hashmap_remove (&blob_hashmap, e) != e);free (bc); + e =hashmap_iter_next (&iter); }DBG_BUGON (hashmap_free (&blob_hashmap));
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH 2/2] erofs-utils: lib: improve freeing hashmap in erofs_blob_exit()
Hi Sandeep,
Quoted 17 lines · Sandeep Dhavale
Depending on size of the filesystem being built there can be huge number of elements in the hashmap. Currently we call
hashmap_iter_first()in while loop to iterate and free the elements. However technically correct, this is inefficient in 2 aspects.- As we are iterating the elements for removal, we do not need overhead of rehashing.
- Second part which contributes hugely to the performance is usinghashmap_iter_first()as it starts scanning from index 0 throwing away the previous successful scan. For sparsely populated hashmap this becomes O(n^2) in worst case.Lets fix this by disabling hashmap shrink which avoids rehashing and use
hashmap_iter_next()which is now guaranteed to iterate over all the elements while removing while avoiding the performance pitfalls of usinghashmap_iter_first().
Quoted 7 lines
Test with random data shows performance improvement as:
fs_sizeBefore After
1G 23s 7s
2G 81s 15s
4G 272s 31s
8G 1252s 61s
Sigh.. BTW, in the long term, I guess we might need to find a better hashmap implementation (with MIT or BSD license) instead of this one.
Quoted 10 lines
Signed-off-by Sandeep Dhavale <[email protected]>> ---lib/blobchunk.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)lib/blobchunk.c +0
-0
lib/blobchunk.c void erofs_blob_exit(void) · line 553
Quoted 7 lines
lib/blobchunk.c +3
-1@@ -548,11 +548,17 @@ void erofs_blob_exit(void)if (blobfile)fclose (blobfile); -while ((e =hashmap_iter_first (&blob_hashmap, &iter))) { +/* Disable hashmap shrink, effectively disabling rehash. +* This way we can iterate over entire hashmap efficiently +* and safely by using hashmap_iter_next() */
I will fix up the comment style manually, otherwise it looks good to me...
Thanks,
Gao Xiang
Re: [PATCH 2/2] erofs-utils: lib: improve freeing hashmap in erofs_blob_exit()
Quoted 3 lines · Gao Xiang
Hi Sandeep,
Hi Gao,
Quoted 26 lines · Sandeep Dhavale
Quoted 17 lines
Depending on size of the filesystem being built there can be huge number of elements in the hashmap. Currently we call
hashmap_iter_first()in while loop to iterate and free the elements. However technically correct, this is inefficient in 2 aspects.- As we are iterating the elements for removal, we do not need overhead of rehashing.
- Second part which contributes hugely to the performance is usinghashmap_iter_first()as it starts scanning from index 0 throwing away the previous successful scan. For sparsely populated hashmap this becomes O(n^2) in worst case.Lets fix this by disabling hashmap shrink which avoids rehashing and use
hashmap_iter_next()which is now guaranteed to iterate over all the elements while removing while avoiding the performance pitfalls of usinghashmap_iter_first().Quoted 7 lines
Test with random data shows performance improvement as:
fs_sizeBefore After1G 23s 7s 2G 81s 15s 4G 272s 31s 8G 1252s 61s
Quoted 4 lines
Sigh.. BTW, in the long term, I guess we might need to find a better hashmap implementation (with MIT or BSD license) instead of this one.
Ok, sounds good. I have not seen any more problems (yet). But will be good to see if something better is available.
Quoted 13 lines
Quoted 13 lines
Signed-off-by Sandeep Dhavale <[email protected]>> ---lib/blobchunk.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)lib/blobchunk.c +0
-0@@ -548,11 +548,17 @@ void erofs_blob_exit(void)if (blobfile)fclose (blobfile);
Quoted 8 lines
lib/blobchunk.c void erofs_blob_exit(void) · line 553
Quoted 4 lines
lib/blobchunk.c +3
-1-while ((e =hashmap_iter_first (&blob_hashmap, &iter))) { +/* Disable hashmap shrink, effectively disabling rehash. +* This way we can iterate over entire hashmap efficiently +* and safely by using hashmap_iter_next() */ I will fix up the comment style manually, otherwise it looks good to me...
Ok, thanks!
Quoted 4 lines
Reviewed-by Gao Xiang <[email protected]>Thanks,
Gao Xiang