Skip to contentSkip to conversation

erofs-utils: lib: get rid of `len` in z_erofs_compress_one()

PATCH New Gao Xiang 1 participant 2 messages Dec 20, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v2 1/2 erofs-utils: lib: get rid of `len` in z_erofs_compress_one()

More than one extent could be emitted in __z_erofs_compress_one() later.

---
 lib/compress.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
lib/compress.c +9 -14
index 65edd00..6a409de 100644
@@ -278,8 +278,7 @@ static void z_erofs_commit_extent(struct z_erofs_compress_sctx *ctx,
 			  (erofs_blksiz(ctx->ictx->inode->sbi) - 1);
 }
 
-static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx,
-				   unsigned int *len)
+static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx)
 {
 	struct erofs_inode *inode = ctx->ictx->inode;
 	const unsigned int lclustermask = (1 << inode->z_logical_clusterbits) - 1;
@@ -306,7 +305,7 @@ static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx,
 				else
 					rc = ei->e.length - erofs_blksiz(sbi);
 				rc; }),
-			.end = ctx->queue + ctx->head + *len,
+			.end = ctx->queue + ctx->tail,
 			.cur = ctx->queue + ctx->head,
 		};
 		int delta;
@@ -366,12 +365,11 @@ static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx,
 		ei->e = dctx.e;
 
 		ctx->head += dctx.e.length - delta;
-		DBG_BUGON(*len < dctx.e.length - delta);
-		*len -= dctx.e.length - delta;
+		DBG_BUGON(ctx->head > ctx->tail);
 
 		if (z_erofs_need_refill(ctx))
 			return 1;
-	} while (*len);
+	} while (ctx->tail > ctx->head);
 out:
 	z_erofs_commit_extent(ctx, ei);
 	ctx->pivot = NULL;
@@ -484,13 +482,12 @@ out:
 	return 0;
 }
 
-static bool z_erofs_fixup_deduped_fragment(struct z_erofs_compress_sctx *ctx,
-					   unsigned int len)
+static bool z_erofs_fixup_deduped_fragment(struct z_erofs_compress_sctx *ctx)
 {
 	struct z_erofs_compress_ictx *ictx = ctx->ictx;
 	struct erofs_inode *inode = ictx->inode;
 	struct erofs_sb_info *sbi = inode->sbi;
-	const unsigned int newsize = ctx->remaining + len;
+	const unsigned int newsize = ctx->remaining + ctx->tail - ctx->head;
 
 	DBG_BUGON(!inode->fragment_size);
 
@@ -700,11 +697,10 @@ fix_dedupedfrag:
 static int z_erofs_compress_one(struct z_erofs_compress_sctx *ctx)
 {
 	struct z_erofs_compress_ictx *ictx = ctx->ictx;
-	unsigned int len = ctx->tail - ctx->head;
 	struct z_erofs_extent_item *ei;
 
-	while (len) {
-		int ret = z_erofs_compress_dedupe(ctx, &len);
+	while (ctx->tail > ctx->head) {
+		int ret = z_erofs_compress_dedupe(ctx);
 
 		if (ret > 0)
 			break;
@@ -725,10 +721,9 @@ static int z_erofs_compress_one(struct z_erofs_compress_sctx *ctx)
 			return ret;
 		}
 
-		len -= ei->e.length;
 		ctx->pivot = ei;
 		if (ictx->fix_dedupedfrag && !ictx->fragemitted &&
-		    z_erofs_fixup_deduped_fragment(ctx, len))
+		    z_erofs_fixup_deduped_fragment(ctx))
 			break;
 
 		if (z_erofs_need_refill(ctx))
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 2/2 erofs-utils: mkfs: speed up uncompressed data handling

Currently, it only writes one uncompressed block (typically 4 KiB) and then tries to recompress the remaining part if the entire data was compressed and proven to be incompressible.

This is wasteful of CPU resources and slow, as such incompressible data could be skipped entirely.

The LZMA build time for large pclusters (e.g., 512K, 1M) can be greatly reduced if there is a significant amount of uncompressed data.

---
 lib/compress.c | 58 +++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 8 deletions(-)
lib/compress.c +50 -8
index 6a409de..07a6e57 100644
@@ -376,8 +376,8 @@ out:
 	return 0;
 }
 
-static int write_uncompressed_extent(struct z_erofs_compress_sctx *ctx,
-				     unsigned int len, char *dst)
+static int write_uncompressed_block(struct z_erofs_compress_sctx *ctx,
+				    unsigned int len, char *dst)
 {
 	struct erofs_inode *inode = ctx->ictx->inode;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -412,6 +412,44 @@ static int write_uncompressed_extent(struct z_erofs_compress_sctx *ctx,
 	return count;
 }
 
+static int write_uncompressed_extents(struct z_erofs_compress_sctx *ctx,
+				      unsigned int size, unsigned int processed,
+				      char *dst)
+{
+	struct erofs_inode *inode = ctx->ictx->inode;
+	unsigned int lclustersize = 1 << inode->z_logical_clusterbits;
+	struct z_erofs_extent_item *ei;
+	int count;
+
+	while (1) {
+		count = write_uncompressed_block(ctx, size, dst);
+		if (count < 0)
+			return count;
+
+		size -= count;
+		if (processed < lclustersize + count)
+			break;
+		processed -= count;
+
+		ei = malloc(sizeof(*ei));
+		if (!ei)
+			return -ENOMEM;
+		init_list_head(&ei->list);
+
+		ei->e = (struct z_erofs_inmem_extent) {
+			.length = count,
+			.compressedblks = BLK_ROUND_UP(inode->sbi, count),
+			.raw = true,
+			.blkaddr = ctx->blkaddr,
+		};
+		if (ctx->blkaddr != EROFS_NULL_ADDR)
+			ctx->blkaddr += ei->e.compressedblks;
+		z_erofs_commit_extent(ctx, ei);
+		ctx->head += count;
+	}
+	return count;
+}
+
 static unsigned int z_erofs_get_max_pclustersize(struct erofs_inode *inode)
 {
 	if (erofs_is_packed_inode(inode)) {
@@ -533,6 +571,7 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
 	unsigned int compressedsize;
 	int ret;
 
+	DBG_BUGON(ctx->pivot);
 	*e = (struct z_erofs_inmem_extent){};
 	if (len <= ctx->pclustersize) {
 		if (!final || !len)
@@ -545,8 +584,10 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
 			e->length = len;
 			goto frag_packing;
 		}
-		if (!may_inline && len <= blksz)
+		if (!may_inline && len <= blksz) {
+			e->length = len;
 			goto nocompression;
+		}
 	}
 
 	e->length = min(len, cfg.c_max_decompressed_extent_bytes);
@@ -574,9 +615,11 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
 		} else {
 			may_inline = false;
 			may_packing = false;
+			e->length = min_t(u32, e->length, ret);
 nocompression:
 			/* TODO: reset clusterofs to 0 if permitted */
-			ret = write_uncompressed_extent(ctx, len, dst);
+			ret = write_uncompressed_extents(ctx, len,
+							 e->length, dst);
 			if (ret < 0)
 				return ret;
 		}
@@ -702,17 +745,16 @@ static int z_erofs_compress_one(struct z_erofs_compress_sctx *ctx)
 	while (ctx->tail > ctx->head) {
 		int ret = z_erofs_compress_dedupe(ctx);
 
+		if (ret < 0)
+			return ret;
 		if (ret > 0)
 			break;
-		else if (ret < 0)
-			return ret;
 
-		DBG_BUGON(ctx->pivot);
 		ei = malloc(sizeof(*ei));
 		if (!ei)
 			return -ENOMEM;
-
 		init_list_head(&ei->list);
+
 		ret = __z_erofs_compress_one(ctx, &ei->e);
 		if (ret) {
 			free(ei);
Signature · 1 line
2.43.5