473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pimpl optimization

Hi group,
suppose having this code using PIMPL:

// a.hh
class A
{
struct Impl;
public:
A();

int Member1() const
{ return mrMember; }
int Member2() const;
private:
Impl* mpImpl;
int& mrMember;
};

// a.cc
struct A::Impl
{
int mMember;
};

A()
: mpImpl(new Impl)
, mrMember(mpImpl->mMember)
{}

int A::Member2() const
{ return mpImpl->mMember; }
// end of code

The member functions Member1() and Member2() should return the same
value at all times, except for the Member1() operation can be inlined
while Member2() cannot.
Does it make sense for certain members, which are hidden in sturct
Impl, to expose references in the header file so the accessor
operations can be inlined?
Is it better, from the design point of view, to have the member
attribute in class A and have its reference in struct Impl so Impl
functions can access it? (I am assuming from performance point of
view it should be the same as the code above).
Thanks
m.

Feb 5 '07 #1
7 2296
On 5 Feb 2007 03:47:32 -0800, "mliptak" <Me*****@gmail.comwrote:
>Hi group,
suppose having this code using PIMPL:

// a.hh
class A
{
struct Impl;
public:
A();

int Member1() const
{ return mrMember; }
int Member2() const;
private:
Impl* mpImpl;
int& mrMember;
};

// a.cc
struct A::Impl
{
int mMember;
};

A()
: mpImpl(new Impl)
, mrMember(mpImpl->mMember)
{}

int A::Member2() const
{ return mpImpl->mMember; }
// end of code

The member functions Member1() and Member2() should return the same
value at all times, except for the Member1() operation can be inlined
while Member2() cannot.
Does it make sense for certain members, which are hidden in sturct
Impl, to expose references in the header file so the accessor
operations can be inlined?
Is it better, from the design point of view, to have the member
attribute in class A and have its reference in struct Impl so Impl
functions can access it? (I am assuming from performance point of
view it should be the same as the code above).
Thanks
m.
Since struct A::Impl is a private data structure used only by A, you can do
whatever you want with it, including the trick that you're using right now.
No-one else can use the data structure, so no-one else would care what it
looks like.

-dr
Feb 5 '07 #2

mliptak wrote:
>
int Member1() const { return mrMember; }
int Member2() const;

int A::Member2() const { return mpImpl->mMember; }

The member functions Member1() and Member2() should return the same
value at all times, except for the Member1() operation can be inlined
while Member2() cannot.
Member2() can be inlined, if you are writing "inline" keyword

int Member1() const { return mrMember; }
inline int Member2() const;

inline int A::Member2() const { return mpImpl->mMember; }
Does it make sense for certain members, which are hidden in sturct
Impl, to expose references in the header file so the accessor
operations can be inlined?
Is it better, from the design point of view, to have the member
attribute in class A and have its reference in struct Impl so Impl
functions can access it? (I am assuming from performance point of
view it should be the same as the code above).
Not sure wah are speaking about, becasue "sturct A::Impl" is undefined.

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 5 '07 #3
Grizlyk wrote:
mliptak wrote:
>int Member1() const { return mrMember; }
int Member2() const;

int A::Member2() const { return mpImpl->mMember; }

The member functions Member1() and Member2() should return the same
value at all times, except for the Member1() operation can be inlined
while Member2() cannot.

Member2() can be inlined, if you are writing "inline" keyword

int Member1() const { return mrMember; }
inline int Member2() const;

inline int A::Member2() const { return mpImpl->mMember; }
You don't want to do this, as it defeats the purpose of the pImpl,
namely reducing coupling. Otherwise, you need to have the definition of
the Impl class available. A good pImpl implementation should not inline
because you can then hide the implementation of the pImpl...

e.g.:

//MyClass.h

class MyImpl;
class MyClass
{
private:
MyImpl* pImpl;
public:
MyClass();
~MyClass();
void Method1();
// etc.
};

With this, clients don't need access to the definition of MyImpl. By
inlining, you will need to include "MyImpl.h" as well. Bad design, as
it increases unnecessary coupling (clients of MyClass don't *need* to
know what MyImpl looks like).
Feb 6 '07 #4
On Tue, 6 Feb 2007 00:22:57 +0300, "Grizlyk" <gr******@yandex.ruwrote:
>Member2() can be inlined, if you are writing "inline" keyword

int Member1() const { return mrMember; }
inline int Member2() const;

inline int A::Member2() const { return mpImpl->mMember; }
No, it can't. In the OP's post, the definition of A::Member2() const is in the
..cpp file, not in the .h file.
Feb 6 '07 #5
On Tue, 06 Feb 2007 02:12:58 GMT, red floyd <no*****@here.dudewrote:
>Grizlyk wrote:
>mliptak wrote:
>>int Member1() const { return mrMember; }
int Member2() const;

int A::Member2() const { return mpImpl->mMember; }

The member functions Member1() and Member2() should return the same
value at all times, except for the Member1() operation can be inlined
while Member2() cannot.

Member2() can be inlined, if you are writing "inline" keyword

int Member1() const { return mrMember; }
inline int Member2() const;

inline int A::Member2() const { return mpImpl->mMember; }
You don't want to do this, as it defeats the purpose of the pImpl,
namely reducing coupling. Otherwise, you need to have the definition of
the Impl class available. A good pImpl implementation should not inline
because you can then hide the implementation of the pImpl...
The OP has already done exactly what you suggested; check the original post.
struct A::impl was declared but not defined in the header file. It is defined
as a local structure in the source file.

-dr
Feb 6 '07 #6
Member2() can be inlined, if you are writing "inline" keyword
>
int Member1() const { return mrMember; }
inline int Member2() const;

inline int A::Member2() const { return mpImpl->mMember; }
No it can't.
Either you mentioned to implement the Member2() in the .h file which I
can't and won't do because Impl is only declared in .h file. Or you
mentioned to implement the Member2() in the .cpp file. However when
calling Member2 from a different compilation unit, you get 'undefined
reference' error by linker, since the inlined function must be in
the .h file (check c++ FAQ lite [9.7]).
>
Does it make sense for certain members, which are hidden in sturct
Impl, to expose references in the header file so the accessor
operations can be inlined?
Is it better, from the design point of view, to have the member
attribute in class A and have its reference in struct Impl so Impl
functions can access it? (I am assuming from performance point of
view it should be the same as the code above).

Not sure wah are speaking about, becasue "sturct A::Impl" is undefined.
Ok, what I meant is that you expose some of the Impl members in the
parent class (class A in my example) via references.

Feb 6 '07 #7
You don't want to do this, as it defeats the purpose of the pImpl,
namely reducing coupling. Otherwise, you need to have the definition of
the Impl class available. A good pImpl implementation should not inline
because you can then hide the implementation of the pImpl...

The OP has already done exactly what you suggested; check the original post.
struct A::impl was declared but not defined in the header file. It is defined
as a local structure in the source file.
Exactly. There's no question about whether the Impl should be visible
to "outside" world, because that's what pimpl is about. The question
is about the style when some of the members of Impl - especially those
which don't present additional compilation dependencies and are
accessed many times - are exposed in A's declaration in order for some
operations to get inlined.

Feb 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
4
by: Simon Elliott | last post by:
I want to design a class which will encapsulate some system specific representation, in this case time structs: #if unix_implementation #include <time.h> class Cfoo { private:
9
by: Edward Diener | last post by:
Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++ classes seems nearly impossible. Normally a pImpl class is a 'friend' to the class for which it supplies the private...
10
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
2
by: Graham Reitz | last post by:
What are good strategies for selecting, either at run-time or compile time, various pimpl'ed implementations? While retaining the ability to switch implementations without recompiling. Boost...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.