From: George Hu <integral@archlinux.org>
To: kent.overstreet@linux.dev
Cc: integral@archlinux.org, linux-bcachefs@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] bcachefs: use union for bch_compression_opt to make encode & decode easier
Date: Sat, 31 May 2025 14:31:16 +0800 [thread overview]
Message-ID: <20250531063116.710718-1-integral@archlinux.org> (raw)
In-Reply-To: <zxcvscosad6yv2y6km3xsdhh26qse3qcx5u66a4nqybev4u64y@uzdekpsliv35>
Eliminate redundant encode & decode function by using union for
bch_compression_opt, which reduces code complexity.
Signed-off-by: George Hu <integral@archlinux.org>
---
fs/bcachefs/compress.c | 17 +++++++++--------
fs/bcachefs/compress.h | 36 +++++++++++-------------------------
fs/bcachefs/extents.c | 2 +-
3 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/fs/bcachefs/compress.c b/fs/bcachefs/compress.c
index b37b1f325f0a..5f74de920c92 100644
--- a/fs/bcachefs/compress.c
+++ b/fs/bcachefs/compress.c
@@ -336,7 +336,7 @@ static int attempt_compress(struct bch_fs *c,
void *workspace,
void *dst, size_t dst_len,
void *src, size_t src_len,
- struct bch_compression_opt compression)
+ union bch_compression_opt compression)
{
enum bch_compression_type compression_type =
__bch2_compression_opt_to_type[compression.type];
@@ -426,7 +426,7 @@ static int attempt_compress(struct bch_fs *c,
static unsigned __bio_compress(struct bch_fs *c,
struct bio *dst, size_t *dst_len,
struct bio *src, size_t *src_len,
- struct bch_compression_opt compression)
+ union bch_compression_opt compression)
{
struct bbuf src_data = { NULL }, dst_data = { NULL };
void *workspace;
@@ -553,7 +553,7 @@ unsigned bch2_bio_compress(struct bch_fs *c,
compression_type =
__bio_compress(c, dst, dst_len, src, src_len,
- bch2_compression_decode(compression_opt));
+ (union bch_compression_opt){ .value = compression_opt });
dst->bi_iter.bi_size = orig_dst;
src->bi_iter.bi_size = orig_src;
@@ -602,7 +602,8 @@ static int __bch2_check_set_has_compressed_data(struct bch_fs *c, u64 f)
int bch2_check_set_has_compressed_data(struct bch_fs *c,
unsigned compression_opt)
{
- unsigned compression_type = bch2_compression_decode(compression_opt).type;
+ unsigned int compression_type = ((union bch_compression_opt){ .value = compression_opt })
+ .type;
BUG_ON(compression_type >= ARRAY_SIZE(bch2_compression_opt_to_feature));
@@ -683,7 +684,7 @@ static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)
static u64 compression_opt_to_feature(unsigned v)
{
- unsigned type = bch2_compression_decode(v).type;
+ unsigned int type = ((union bch_compression_opt){ .value = v }).type;
return BIT_ULL(bch2_compression_opt_to_feature[type]);
}
@@ -703,7 +704,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
{
char *val = kstrdup(_val, GFP_KERNEL);
char *p = val, *type_str, *level_str;
- struct bch_compression_opt opt = { 0 };
+ union bch_compression_opt opt = { 0 };
int ret;
if (!val)
@@ -736,7 +737,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
opt.level = level;
}
- *res = bch2_compression_encode(opt);
+ *res = opt.value;
err:
kfree(val);
return ret;
@@ -744,7 +745,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
void bch2_compression_opt_to_text(struct printbuf *out, u64 v)
{
- struct bch_compression_opt opt = bch2_compression_decode(v);
+ union bch_compression_opt opt = { .value = v };
if (opt.type < BCH_COMPRESSION_OPT_NR)
prt_str(out, bch2_compression_opts[opt.type]);
diff --git a/fs/bcachefs/compress.h b/fs/bcachefs/compress.h
index bec2f05bfd52..667ddb91d47a 100644
--- a/fs/bcachefs/compress.h
+++ b/fs/bcachefs/compress.h
@@ -10,41 +10,27 @@ static const unsigned __bch2_compression_opt_to_type[] = {
#undef x
};
-struct bch_compression_opt {
- u8 type:4,
- level:4;
-};
-
-static inline struct bch_compression_opt __bch2_compression_decode(unsigned v)
-{
- return (struct bch_compression_opt) {
- .type = v & 15,
- .level = v >> 4,
+union bch_compression_opt {
+ u8 value;
+ struct {
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+ u8 type:4, level:4;
+#elif defined(__BIG_ENDIAN_BITFIELD)
+ u8 level:4, type:4;
+#endif
};
-}
+};
static inline bool bch2_compression_opt_valid(unsigned v)
{
- struct bch_compression_opt opt = __bch2_compression_decode(v);
+ union bch_compression_opt opt = { .value = v };
return opt.type < ARRAY_SIZE(__bch2_compression_opt_to_type) && !(!opt.type && opt.level);
}
-static inline struct bch_compression_opt bch2_compression_decode(unsigned v)
-{
- return bch2_compression_opt_valid(v)
- ? __bch2_compression_decode(v)
- : (struct bch_compression_opt) { 0 };
-}
-
-static inline unsigned bch2_compression_encode(struct bch_compression_opt opt)
-{
- return opt.type|(opt.level << 4);
-}
-
static inline enum bch_compression_type bch2_compression_opt_to_type(unsigned v)
{
- return __bch2_compression_opt_to_type[bch2_compression_decode(v).type];
+ return __bch2_compression_opt_to_type[((union bch_compression_opt){ .value = v }).type];
}
struct bch_write_op;
diff --git a/fs/bcachefs/extents.c b/fs/bcachefs/extents.c
index 036e4ad95987..8bdcb1551814 100644
--- a/fs/bcachefs/extents.c
+++ b/fs/bcachefs/extents.c
@@ -1512,7 +1512,7 @@ int bch2_bkey_ptrs_validate(struct bch_fs *c, struct bkey_s_c k,
const struct bch_extent_rebalance *r = &entry->rebalance;
if (!bch2_compression_opt_valid(r->compression)) {
- struct bch_compression_opt opt = __bch2_compression_decode(r->compression);
+ union bch_compression_opt opt = { .value = r->compression };
prt_printf(err, "invalid compression opt %u:%u",
opt.type, opt.level);
return bch_err_throw(c, invalid_bkey);
--
2.49.0
prev parent reply other threads:[~2025-05-31 6:31 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-30 14:52 [PATCH] bcachefs: use union for bch_compression_opt to make encode & decode easier George Hu
2025-05-30 22:00 ` Kent Overstreet
2025-05-31 6:31 ` George Hu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250531063116.710718-1-integral@archlinux.org \
--to=integral@archlinux.org \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox