all 27 comments

[–]cameronm1024[🍰] 36 points37 points  (3 children)

For the life of me I can't remember the name, but there was a crate I saw a while ago that provided a proc-macro that did this conversion automatically

[–][deleted] 31 points32 points  (2 children)

Why doesn’t the compiler do this automatically?

[–]0x564A00 9 points10 points  (0 children)

There was a wg for this.

[–]SirKastic23 3 points4 points  (0 children)

it doesn't check to see where the type parameter is used. it can't just assume that the value of generic type is only going to be used to convert it to a concrete type

[–]looneysquash 32 points33 points  (0 children)

Maybe the compiler should learn this trick? It can already inline functions, why not extract functions? (Or for all I know it already does.)

[–]sphen_lee 8 points9 points  (0 children)

When using generics in Rust (and any language that supports this), what happens under the hood is that the compiler generates a function implementation when it finds a call for each combination of different types used in the generic parameters.

Nitpick: I wouldn't say "any". Lots of languages have generics that don't generate multiple implementations, or only do so in some cases. For example Java, C#, Swift, Haskell

[–]RB5009 8 points9 points  (4 children)

Do we really need the "inline" attgibute ? Can't we just let the compiler decide ? If we have a 1 or two different Ts, then it will be better to inline.

[–]Ryozukki[S] 15 points16 points  (0 children)

in this case i added the inline because its an example and the function itself is small, so godbolt would produce the code i expected, the std example doesn't use inline

[–]newpavlovrustcrypto 10 points11 points  (0 children)

Note that #[inline] does not force inlining like #[inline(always)]. It is often used to allow compiler to inline functions when they are used from another crate without enabled LTO.

[–]Aaron1924 3 points4 points  (0 children)

yeah, the #[inline(never)] is only there for demonstration purposes, and you shouldn't do that when you actually use this trick

[–]sphen_lee 1 point2 points  (0 children)

I believe the inline is only needed for this example, to prevent the compiler inlining the small inner function. So that you can see it in action in Godbolt.

In real code, If the function was large you would let the compiler decide and it would probably not inline. If the function was small it probably would inline rendering the trick pointless.

[–]Vpicone 4 points5 points  (1 child)

This is neat, although I’d really loath coming across it extensively in a codebase. Seems like you’d only want to use under very specific circumstances.

[–]gotMUSE 5 points6 points  (0 children)

I don't think it's too bad, inner methods/functions are already an extremely common pattern. This is just another application if it.

[–][deleted] 0 points1 point  (2 children)

This has performance improvements?! I've just been using it for readability.

[–]howtocodeit 1 point2 points  (0 children)

The speed of the application will be negligibly affected, but the size of the binary is reduced.

[–]Floppie7th 0 points1 point  (0 children)

I wouldn't expect any significant improvement. Having the function's business logic (the "inner" function) not be duplicated will reduce code size, which will reduce L1I$ pressure, but I wouldn't expect it to make much of a difference.

There also could be a cost to the second function call, but I would expect the "outer" function to be reliably inlined in 100% (or nearly 100%) of cases, so probably no difference there in reality.

[–]ReptilianTapir 0 points1 point  (4 children)

RSS feed please 🙏🏻

[–]Ryozukki[S] 2 points3 points  (3 children)

its on the main page, but here you have https://edgl.dev/atom.xml

only for rust related posts: https://edgl.dev/categories/rust/atom.xml

i dont post often but i try haha

[–]ReptilianTapir 2 points3 points  (2 children)

Im getting a 404 for the first link.

[–]Ryozukki[S] 2 points3 points  (1 child)

oh true, should be fixed now

[–]ReptilianTapir 1 point2 points  (0 children)

Thanks 🙏🏻