Re: [PATCH 1/3] rust: maple_tree: add MapleTree

From: Alice Ryhl
Date: Sat Jul 26 2025 - 12:42:16 EST


On Sat, Jul 26, 2025 at 6:23 PM Matthew Wilcox <willy@xxxxxxxxxxxxx> wrote:
>
> On Sat, Jul 26, 2025 at 01:23:22PM +0000, Alice Ryhl wrote:
> > +struct ma_state rust_helper_MA_STATE(struct maple_tree *mt, unsigned long start, unsigned long end)
> > +{
> > + MA_STATE(mas, mt, start, end);
> > + return mas;
> > +}
>
> This seems very inefficient. Returning a struct larger than two words
> (on x86 anyway) means that the compiler implements this as:
>
> void rust_helper_MA_STATE(struct ma_state *masp, ...)
> {
> MA_STATE(mas, mt, start, end);
> *masp = mas;
> }
>
> so that's about 72 bytes being memcpy'd per access to the maple tree.
> Sure, it's stack, so it's cache hot, but surely we can implement
> the equivalent of MA_STATE in Rust and see a significant performance
> win, at least on read operations.

Some of the methods go through the mtree_* functions that create the
MA_STATE on the C side, so this isn't always the case.

Regardless, we definitely can avoid this helper. It has the
consequence that if MA_STATE is changed in the future, then the Rust
version of the method must also be changed. I can add a comment to
that effect to the header file to remind people of that.

Alice