From: Sun YangKai <sunk67188@gmail.com>
To: Rosen Penev <rosenp@gmail.com>, linux-btrfs@vger.kernel.org
Cc: Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] btrfs: compression: allocate buckets with workspace
Date: Mon, 29 Jun 2026 09:54:11 +0800 [thread overview]
Message-ID: <c6f9a5f5-0aaf-4f89-9e88-fb7eaf6aa3d2@gmail.com> (raw)
In-Reply-To: <20260629002505.1552238-1-rosenp@gmail.com>
On 2026/6/29 08:25, Rosen Penev wrote:
> Convert 3 allocations into one. Simplifies code slightly.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
> fs/btrfs/compression.c | 18 +++++-------------
> 1 file changed, 5 insertions(+), 13 deletions(-)
>
> diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
> index ffb6b52863a7..da6749ff5924 100644
> --- a/fs/btrfs/compression.c
> +++ b/fs/btrfs/compression.c
> @@ -650,11 +650,11 @@ struct heuristic_ws {
> /* Partial copy of input data */
> u8 *sample;
> u32 sample_size;
> - /* Buckets store counters for each byte value */
> - struct bucket_item *bucket;
> /* Sorting buffer */
> struct bucket_item *bucket_b;
> struct list_head list;
> + /* Buckets store counters for each byte value */
> + struct bucket_item bucket[];
> };
>
> static void free_heuristic_ws(struct list_head *ws)
> @@ -664,8 +664,6 @@ static void free_heuristic_ws(struct list_head *ws)
> workspace = list_entry(ws, struct heuristic_ws, list);
>
> kvfree(workspace->sample);
> - kfree(workspace->bucket);
> - kfree(workspace->bucket_b);
> kfree(workspace);
> }
>
> @@ -673,22 +671,16 @@ static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info)
> {
> struct heuristic_ws *ws;
>
> - ws = kzalloc_obj(*ws);
> + ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL);
It seems that size is fixed and known at compile time, and since we want
to inline the buckets in the struct, I think we can have something like
this to save one pointer:
struct heuristic_ws {
...
struct bucket_item bucket[BUCKET_SIZE];
struct bucket_item bucket_b[BUCKET_SIZE];
}
However, each bucket array takes exactly 1024B memory. Currently we have
a 48B allocation for the struct it self, 2 * 1024B allocation for bucket
array, and 8192B allocation for sample. With these 2 arrays inlined, the
struct will need 32 + 2048B allocation if my calculation is correct,
which will goes into kmalloc-4k. Seems not good.
Thanks,
Sun YangKai
> if (!ws)
> return ERR_PTR(-ENOMEM);
>
> + ws->bucket_b = ws->bucket + BUCKET_SIZE;
> +
> ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
> if (!ws->sample)
> goto fail;
>
> - ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE);
> - if (!ws->bucket)
> - goto fail;
> -
> - ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE);
> - if (!ws->bucket_b)
> - goto fail;
> -
> INIT_LIST_HEAD(&ws->list);
> return &ws->list;
> fail:
next prev parent reply other threads:[~2026-06-29 1:54 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-29 0:25 [PATCH] btrfs: compression: allocate buckets with workspace Rosen Penev
2026-06-29 1:54 ` Sun YangKai [this message]
2026-06-29 23:09 ` David Sterba
2026-06-30 1:51 ` Sun YangKai
2026-06-29 10:14 ` Qu Wenruo
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=c6f9a5f5-0aaf-4f89-9e88-fb7eaf6aa3d2@gmail.com \
--to=sunk67188@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rosenp@gmail.com \
/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