Skip to content

Reduce bloat from smallvec! in the inline case - #424

Open
bolshoytoster wants to merge 3 commits into
servo:v2from
bolshoytoster:v2
Open

Reduce bloat from smallvec! in the inline case#424
bolshoytoster wants to merge 3 commits into
servo:v2from
bolshoytoster:v2

Conversation

@bolshoytoster

Copy link
Copy Markdown

See #418 for details.

Comment thread src/lib.rs Outdated
@@ -2672,16 +2672,9 @@
$crate::from_elem($elem, $n)
});
($($x:expr),+$(,)?) => ({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be instead

($($x:expr),* $(,)?) => // ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though it wouldn't make sense calling smallvec[,], and it actually isn't a valid array, so it should probably be

($($($x:expr),+ $(,)?)?) => // ...

Comment thread src/lib.rs Outdated

@alejandro-vaz alejandro-vaz Aug 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then the () => // ... branch can disappear because the glob pattern of the one with elements it would cover nullable tokenstreams

Comment thread src/lib.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch can be removed if completely

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I missed that.

Comment thread src/lib.rs
}

#[macro_export]
macro_rules! smallvec_inline {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something similar should be done here, removing @one recursion

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that change the behavior of the macro? It currently outputs a SmallVec with the exact capacity for the items passed. If I removed the const N: usize = 0usize $(+ $crate::smallvec_inline!(@one $x))*; and just had it infer N, then it would be identical to the other macro.

Maybe something like

    ($($x:expr),+ $(,)?) => ({
        let buf = [$($x,)*];
        $crate::SmallVec::from_buf_and_len(buf, buf.len())
    });

would work?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually to me it how it is written now seems wrong

smallvec_inline! should basically be the parity of smallvec! but all being inline

so I honestly think it should be

    ($($x:expr),+ $(,)?) => ({
        $crate::SmallVec::from_buf([$($x),*])
        // from_buf::<S>([_; N]) lets S be inferred from the caller
        // so:
        // let s: SmallVec<usize, 4> = smallvec_inline![1, 2]
    });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently it is counting so it doesn't allow to get from context the desired maximum capacity, but that's not what smallvec! does

and they should probably behave equivalently

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that be a breaking change? And if we do that, we may as well just remove smallvec_inline! in favor of smallvec!.

Actually, looking at the blame, it looks like smallvec_inline! came from #265 (2021) and might just be obselete now.

It would still be a breaking change to remove it, so maybe it could just forward to smallvec!?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smallvec v2 is on alpha so breaking changes aren't an specially big deal if we get the architecture right

I mean, yeah, the breaking change would be that anyone who is using smallvec_inline! couldn't be relying on the macro for inferring the value of N

it doesn't seem intentional to me that they let constant fixed

but it is true that we could just remove the macro entirely then

but what use case does smallvec_inline! have?? basically none. just use smallvec! and annotate or (most of the time) the compiler will infer the value you want

I'd lean now towards removing the smallvec_inline! macro altogether

I'll request a second reviewer later, but I think this is the right direction for such a small macro whose use case is negligible and was meant for a hack 5 years ago

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove it (and if it has any uses in the codebase simply replace) and the pr is ready

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a use, actually. smallvec_inline can be used in const, but From<[T, M]> (and therefore smallvec!) isn't const.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this discussion will be continued on #432

Comment thread src/lib.rs Outdated
($($x:expr),+$(,)?) => ({
$crate::SmallVec::from([$($x,)+])
($($($x:expr),+$(,)?)?) => ({
$crate::SmallVec::from([$($($x,)+)?])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$crate::SmallVec::from([$($($x,)+)?])

should be

$crate::SmallVec::from([$($($x),+)?])

weird rust quirk in which the outside comma means there is no trailing comma

probably doesn't matter much but it matches the branch signature

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left the comma inside for consistency because the original did it in the heap path. smallvec_inline also has the comma inside. I can change it if you prefer it outside.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah remove it everywhere you see it

why would anyone want a trailing comma in macro_rules generation everywhere

hahahaha it's so tiny it's laughable but it doesn't make sense

like if it had been intentionally made that way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants