Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance/memory usage impact in non-polymorphic inheritance?

I'm curious about the impact of inheritance in non-polymorphic classes. Specifically, I am writing two smart pointer classes, neither with virtual methods, and both for very unique purposes. Since the basic operator overloads and some standard functions are identical, and they only need one member variable, I figured I could use a base class to reuse code. Here's a simple mockup of what I mean:

The base class:

template <class T>
class Pointer_Impl
{
public:
    T & operator*() { return this->*m_pointer; }
    // etc.
protected:
    T *m_pointer;
    //protected to prevent instantiation without using = delete
    Pointer_Impl() {}
    Pointer_Impl(T *) {}
    //other constructors, assignment and move operators, destructor here
};

And then:

template <class T>
class PointerA : public Pointer_Impl<T>
{
public:
    PointerA() { m_pointer = nullptr; }
    PointerA(T * obj) { m_pointer = obj; }
    // other constructors, assignment and move operators, destructor, and any other class-specific functions here
};

The question: Is there any overhead in memory or performance in deriving from a base class lacking any virtual methods? Since it's a smart pointer implementation, lean and mean is what I'm looking for.

My gut says no, there is no concern, but I'd like to be certain from wiser minds.

like image 345
jonspaceharper Avatar asked Jun 21 '16 10:06

jonspaceharper


People also ask

Will flash-based memory become stable enough to challenge volatile memory?

Flash-based memory used in mobile devices, which usually have a shelf life of a couple of years, will one day become stable and inexpensive enough to challenge volatile memory — just not quite yet.

What is non-volatile memory?

Non-volatile memory (NVM) is a type of memory storage that holds data when the power is turned off on a computer or device. Unlike volatile memory (VM), it does not need to refresh data periodically to function. This type of memory is typically used for long-term, secondary storage.

How can we improve the performance of computer memory?

“The realization of very large, flattened memory address spaces and very fast I/O devices will greatly improve speeds on practical applications, presumably greatly reducing the gap between peak and sustained performance. Also, the simple predictable behavior of SCM, compared to disks, will simplify performance tuning.


1 Answers

Performance impact

None. Without virtual (either virtual inheritance or virtual functions) you won't incur any runtime penalties for using a base class. The name resolution is performed entirely at compile time using the static types.

Memory impact

None. In the absence of virtual, there's no meaningful difference between a class that inherits and one that has the same fields directly as members. This all changes if you have multiple base classes or virtual (inheritance or functions).

Compilation impact

There may be some impact on the time taken to compile; the compiler will have to keep track of more names and template instantiations. This is sometimes a concern for large, template-heavy codebases.

like image 169
Andrew Avatar answered Oct 15 '22 10:10

Andrew