Re: [PATCH] fs: btrfs: Optimize code execution process to save time
From: David Sterba
Date: Thu Jun 29 2023 - 11:40:08 EST
On Mon, Jun 26, 2023 at 06:07:16PM +0800, Lu Hongfei wrote:
> Originally, the btrfs_check_data_free_space used round_down twice when
> aligning the range, which to some extent increased the execution time of
> the code.
> After optimization, round_down only needs to be executed once, which can
> improve code efficiency and increase code readability.
But the code is not equivalent, because there is dependency on the value
of start that changes when the line is moved.
> Signed-off-by: Lu Hongfei <luhongfei@xxxxxxxx>
> ---
> fs/btrfs/delalloc-space.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
> index 427abaf608b8..fd33b1cf1954 100644
> --- a/fs/btrfs/delalloc-space.c
> +++ b/fs/btrfs/delalloc-space.c
> @@ -137,9 +137,8 @@ int btrfs_check_data_free_space(struct btrfs_inode *inode,
> int ret;
>
> /* align the range */
> - len = round_up(start + len, fs_info->sectorsize) -
> - round_down(start, fs_info->sectorsize);
Start is used unchanged, start + len
> start = round_down(start, fs_info->sectorsize);
> + len = round_up(start + len, fs_info->sectorsize) - start;
New code uses the rounded down start for round_up, then for "- start"
it's the same (that part is ok).
The changelog should explain why the code is still doing the same, not
just that it's reducing the number of round_down() calls.