From: Hao Li <haoli.tcs@gmail.com>
To: Peng Zhang <zhangpeng.00@bytedance.com>
Cc: akpm@linux-foundation.org, maple-tree@lists.infradead.org,
linux-kernel@vger.kernel.org, Liam.Howlett@oracle.com
Subject: Re: [PATCH v2] maple_tree: simplify mas_wr_node_walk for improved readability
Date: Fri, 23 Aug 2024 09:38:24 +0000 [thread overview]
Message-ID: <20240823093824.GA566@systemsresearch.io> (raw)
In-Reply-To: <8f98e3e8-b6c4-4888-bfc7-204bea32004b@bytedance.com>
On Fri, Aug 23, 2024 at 05:07:31PM +0800, Peng Zhang wrote:
>
>
> 在 2024/8/23 16:17, Hao Li 写道:
> > Refactor mas_wr_node_walk to make the code more clear and easier to
> > understand. The main changes are:
> >
> > 1. Replace the forward-iterating loop with a backward-iterating loop.
> > This simplifies the logic for determining the correct range
> > containing mas->index.
> I don't think iterating in reverse is a good idea, it makes the code
> different from everywhere else.
I understand your concern about consistency in iteration direction.
While the original code correctly handled all cases, the process wasn't
as definitive or clear.
The new approach unifies the logic by eliminating the need to treat
`offset >= count` as a special case. This results in a more
straightforward and consistent flow throughout the function, which
provides a more deterministic and easy-to-follow path through the logic.
We can more clearly see and understand how we're determining the correct
range for `mas->index` without having to mentally parse complex
conditional logic.
> >
> > 2. Eliminate the ternary operator.
> >
> > The new implementation maintains the same functionality as before, but
> > with improved readability. The performance characteristics remain
> > essentially the same, as we cannot predict which interval mas->index
> > will fall into.
> >
> > Signed-off-by: Hao Li <haoli.tcs@gmail.com>
> > ---
> > lib/maple_tree.c | 18 ++++++++----------
> > 1 file changed, 8 insertions(+), 10 deletions(-)
> >
> > diff --git a/lib/maple_tree.c b/lib/maple_tree.c
> > index fe1b01b29..0b3eb55d8 100644
> > --- a/lib/maple_tree.c
> > +++ b/lib/maple_tree.c
> > @@ -2203,7 +2203,7 @@ static inline void mas_node_or_none(struct ma_state *mas,
> > static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
> > {
> > struct ma_state *mas = wr_mas->mas;
> > - unsigned char count, offset;
> > + int idx;
> > if (unlikely(ma_is_dense(wr_mas->type))) {
> > wr_mas->r_max = wr_mas->r_min = mas->index;
> > @@ -2213,16 +2213,14 @@ static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
> > wr_mas->node = mas_mn(wr_mas->mas);
> > wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
> > - count = mas->end = ma_data_end(wr_mas->node, wr_mas->type,
> > + mas->end = ma_data_end(wr_mas->node, wr_mas->type,
> > wr_mas->pivots, mas->max);
> > - offset = mas->offset;
> > -
> > - while (offset < count && mas->index > wr_mas->pivots[offset])
> > - offset++;
> > -
> > - wr_mas->r_max = offset < count ? wr_mas->pivots[offset] : mas->max;
> > - wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, offset);
> > - wr_mas->offset_end = mas->offset = offset;
> > + wr_mas->r_max = mas->max;
> > + idx = mas->end - 1;
> > + while (idx >= mas->offset && wr_mas->pivots[idx] >= mas->index)
> > + wr_mas->r_max = wr_mas->pivots[idx--];
> > + wr_mas->offset_end = mas->offset = idx + 1;
> > + wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, mas->offset);
> > }
> > /*
next prev parent reply other threads:[~2024-08-23 9:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-23 8:17 [PATCH v2] maple_tree: simplify mas_wr_node_walk for improved readability Hao Li
2024-08-23 9:07 ` Peng Zhang
2024-08-23 9:38 ` Hao Li [this message]
2024-08-23 17:51 ` Liam R. Howlett
2024-08-26 2:55 ` Hao Li
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=20240823093824.GA566@systemsresearch.io \
--to=haoli.tcs@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maple-tree@lists.infradead.org \
--cc=zhangpeng.00@bytedance.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