Can compiler do a default implementation's inlining of the trait's method?

Can compiler do a default implementation's inlining of the trait's method?



I understand that the trait's method doesn't have a body, so there is nothing to inline. But is there any sense to mark its default implementation like this?


trait Magnitude
fn square_magnitude( &self ) -> f64;

#[inline]
fn magnitude( &self ) -> f64
self.square_magnitude().sqrt()




Do I need to rewrite whole method body and mark this impl's method with #[inline] when implementing the trait for type rather than marking just trait's method as above?


#[inline]





"I understand that the trait's method doesn't have a body, so there is nothing to inline." ???
– Stargateur
Sep 3 at 5:51





Possible duplicates: stackoverflow.com/questions/41733424/… stackoverflow.com/questions/51698722/inline-a-common-method
– hellow
Sep 3 at 6:29




1 Answer
1



If I understand the question correctly, you are asking two things:


magnitude


square_magnitude


magnitude


square_magnitude


inline


square_magnitude



As to the first, there is no reason why it couldn't. As to the second the answer is yes, the compiler will be able to inline both functions because by the time it generates the code, the source for both functions is available. This can be seen in the disassembly:


trait Magnitude
fn square_magnitude( &self ) -> f64;

#[inline]
fn magnitude( &self ) -> f64
self.square_magnitude().sqrt()



struct Vector x: f64, y: f64

impl Magnitude for Vector
#[inline]
fn square_magnitude (&self) -> f64
self.x*self.x + self.y*self.y



pub fn test (x: f64, y: f64) -> f64
let v = Vector x: x, y: y ;
v.magnitude()



Compiled with rustc v1.28.0 and option -O:


-O


example::test:
mulsd xmm0, xmm0
mulsd xmm1, xmm1
addsd xmm1, xmm0
xorps xmm0, xmm0
sqrtsd xmm0, xmm1
ret



Note however that the compiler will not inline square_magnitude inside magnitude if square_magnitude is not declared inline itself:


square_magnitude


magnitude


square_magnitude


inline


impl Magnitude for Vector
fn square_magnitude (&self) -> f64
self.x*self.x + self.y*self.y




Generates:


<example::Vector as example::Magnitude>::square_magnitude:
movsd xmm1, qword ptr [rdi]
movsd xmm0, qword ptr [rdi + 8]
mulsd xmm1, xmm1
mulsd xmm0, xmm0
addsd xmm0, xmm1
ret

example::test:
mulsd xmm0, xmm0
mulsd xmm1, xmm1
addsd xmm1, xmm0
xorps xmm0, xmm0
sqrtsd xmm0, xmm1
ret





This is exactly what I meant, thanks.
– kbec
Sep 3 at 11:27



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế