Advertisement Jump to content
Sign in to follow this  
Alundra

Inline for clone() function

This topic is 1742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

We all know it's good to inline Get function :

inline float GetData() const
{
  return m_Data;
}

Now my question is : Is it good to inline clone function ?

inline IBase* Clone() const
{
  return new ThisClass;
}

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement

There is nothing inherently wrong with it, but returning a raw pointer can lead to memory leaks.  Would be safer to return a unique_ptr or shared_ptr (assuming C++).

Share this post


Link to post
Share on other sites

Both of those functions look like they would be declared within a class definition in an example that compiled - they do not look written to compile without being inside of a class - and if they are inside of a class definition, they are inline functions by default.

Share this post


Link to post
Share on other sites


Now my question is : Is it good to inline clone function ?

inline IBase* Clone() const
{
return new ThisClass;
}

 

I'm going on a stretch by saying that this won't give you any advantage here in 99% of the cases really. Think about one possible usage, like in such a method:

void DoSomethingWithBase(const IBase& base)
{
       // do some other stuff
       auto pNew = base.Clone();
       // do something with pNew
}

The compiler won't be able to inline this function at all, since you could pass in every possible class that overrides the clone method in every way imaginable, so that method might differ here based on the type of object passed in here. The compiler, in most cases, can only inline the function if it knows the type of the object for sure, and this is valid for most/all virtual methods.

Share this post


Link to post
Share on other sites

There is nothing inherently wrong with it, but returning a raw pointer can lead to memory leaks.  Would be safer to return a unique_ptr or shared_ptr (assuming C++).

The clone is used for load/save from file based on a factory, the factory is global and cleared on shutdown of the engine.

But you right that it's one thing to be safe when using it outside this scope.

I'm going on a stretch by saying that this won't give you any advantage here in 99% of the cases really. 

That mean that should be avoided and have the Clone() inside a .cpp ?

Edited by Alundra

Share this post


Link to post
Share on other sites

That mean that should be avoided and have the Clone() inside a .cpp ?

 

It means that it is not worth doing so for reasons of performance and (especially permature) inlining optimization. Its as Apoch has pointed out. In that regard, yes, you should define Clone() in the cpp, because this gives better code readability, and unless it proves by profiling that 1.) the Clone()-function call itself is a major bottleneck and 2.) inlining it manually by putting it into the .h file and adding the "inline" keyword/compiler hint solves that, always prefer the code that is more readable. This does not only apply here, but for the rest of your code also.

Edited by Juliean

Share this post


Link to post
Share on other sites

Hi all,
We all know it's good to inline Get function :


I argue otherwise. The inline keyword doesn't even do anything in most modern compilers. In debug builds, inlining is off. In release builds, the compiler has its own set of heuristics and often can be built with LTO (link-time optimization), meaning that putting a function in a header is unnecessary.

I'd sooner go for faster build times which is often helped by making very light headers and never put a function definition in a header (except for some very general templates where extern'd specializations aren't feasible). Then I rely on the optimizer to inline when most appropriate until and unless profiling and hard data shows that I have to help the compiler out.

Share this post


Link to post
Share on other sites

Every sane version of clone() would be a virtual method (or there would be no point having a clone() method) in which case, assuming you are calling through a base pointer, it can't be inlined anyway.

Share this post


Link to post
Share on other sites

Every sane version of clone() would be a virtual method (or there would be no point having a clone() method) in which case, assuming you are calling through a base pointer, it can't be inlined anyway.

Ok if a function is always called from base class (virtual) so it can't be inlined and must be in .cpp.

What's about destructor ? Has it a problem about inline ?

Edited by Alundra

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!