减少虚函数表大小...... [英] Reducing the virtual function table size...
问题描述
考虑一个可以具有7或8个函数的对象。如果为接口创建一个
抽象基类。对象,嗯,这意味着7或
8个纯虚函数对吗?那么,恕我直言,这太过分了......我想知道以下技术是否皱起了眉头。 on:
<伪代码>
_____________
typedef struct ... vztimespec_t;
class condmutex_base {
public:
enum wait_e {
WAITLOCK,TRYLOCK,WAITCOND
};
enum wake_e {
UNLOCK,SIGNAL,BROADCAST
};
protected :
condmutex_base(){}
public:
virtual~condmutex_base(){}
>
私人:
虚拟布尔等待(wait_e,vztimespec_t const *)= 0;
虚拟布尔唤醒(wake_e)= 0;
public:
inline void unlock(){wake(wake_e :: UNLOCK); } $ / $
inline void waitlock(){wait(wait_e :: WAITLOCK,0); } $ / $
inline void waitcond(){wait(wait_e :: WAITCOND,0); } $ / $
inline bool trylock(){return wait(wait_e :: TRYLOCK,0); } $ / $
inline bool signal(){return wake(wake_e :: SIGNAL); }
inline bool broadcast(){return wake(wake_e :: BROADCAST); }
内联bool timedwaitcond(vztimespec_t const * tspec){
返回等待(wait_e :: WAITCOND,tspec);
}
};
_____________
我现在只有2个虚拟功能,不包括dtor等。 ..好吧,2是
优于7,或8?
有什么想法吗?
:^)
Consider an an object that that can has 7 or 8 functions. If you create an
abstract base class for the "interface" of the object, well, that means 7 or
8 pure virtual functions right? Well, IMHO, that''s way too much... I was
wondering if the following technique is "frowned" upon:
<pseudo-code>
_____________
typedef struct ... vztimespec_t;
class condmutex_base {
public:
enum wait_e {
WAITLOCK, TRYLOCK, WAITCOND
};
enum wake_e {
UNLOCK, SIGNAL, BROADCAST
};
protected:
condmutex_base() {}
public:
virtual ~condmutex_base() {}
private:
virtual bool wait(wait_e, vztimespec_t const*) = 0;
virtual bool wake(wake_e) = 0;
public:
inline void unlock() { wake(wake_e::UNLOCK); }
inline void waitlock() { wait(wait_e::WAITLOCK, 0); }
inline void waitcond() { wait(wait_e::WAITCOND, 0); }
inline bool trylock() { return wait(wait_e::TRYLOCK, 0); }
inline bool signal() { return wake(wake_e::SIGNAL); }
inline bool broadcast() { return wake(wake_e::BROADCAST); }
inline bool timedwaitcond(vztimespec_t const *tspec) {
return wait(wait_e::WAITCOND, tspec);
}
};
_____________
I only have 2 virtual functions now, not counting dtor, ect... Well, 2 is
better than 7, or 8?
Any thoughts?
:^)
推荐答案
Chris Thomasson写道:
Chris Thomasson wrote:
考虑一个可以有7或8个函数的对象。如果你创建了
,那么interface的抽象基类就是对象,那么,
意味着7或8个纯虚函数对吗?嗯,恕我直言,那太好了
很多...
Consider an an object that that can has 7 or 8 functions. If you create
an abstract base class for the "interface" of the object, well, that
means 7 or 8 pure virtual functions right? Well, IMHO, that''s way too
much...
为什么,它会给你带来麻烦吗?
Why, is it causing you problems?
我想知道以下技术是否皱眉 on:
I was wondering if the following technique is "frowned" upon:
你所做的只是在派遣中添加一个额外的间接层,
你希望从中获得什么?
All you are doing is adding an extra layer of indirection in dispatch,
what do you hope to gain from this?
>
<伪代码>
_____________
typedef struct ... vztimespec_t;
class condmutex_base {
public:
enum wait_e {
WAITLOCK ,TRYLOCK,WAITCOND
};
enum wake_e {
UNLOCK,SIGNAL,BROADCAST
} b / b受保护:
condmutex_base(){}
b virtual~condmutex_base(){}
private:
虚拟布尔等待(wait_e,vztimespec_t const *)= 0;
virtual bool wake(wake_e)= 0;
public:
inline void unlock(){wake(wake_e :: UNLOCK); }
>
<pseudo-code>
_____________
typedef struct ... vztimespec_t;
class condmutex_base {
public:
enum wait_e {
WAITLOCK, TRYLOCK, WAITCOND
};
enum wake_e {
UNLOCK, SIGNAL, BROADCAST
};
protected:
condmutex_base() {}
public:
virtual ~condmutex_base() {}
private:
virtual bool wait(wait_e, vztimespec_t const*) = 0;
virtual bool wake(wake_e) = 0;
public:
inline void unlock() { wake(wake_e::UNLOCK); }
删除内联。
drop the inline.
inline void waitlock(){wait(wait_e :: WAITLOCK, 0); } $ / $
inline void waitcond(){wait(wait_e :: WAITCOND,0); } $ / $
inline bool trylock(){return wait(wait_e :: TRYLOCK,0); } $ / $
inline bool signal(){return wake(wake_e :: SIGNAL); }
inline bool broadcast(){return wake(wake_e :: BROADCAST); }
内联bool timedwaitcond(vztimespec_t const * tspec){
返回等待(wait_e :: WAITCOND,tspec);
}
};
_____________
我现在只有2个虚拟功能,不包括dtor等。 ..好吧,2
优于7,还是8?
有什么想法吗?
:^ )
inline void waitlock() { wait(wait_e::WAITLOCK, 0); }
inline void waitcond() { wait(wait_e::WAITCOND, 0); }
inline bool trylock() { return wait(wait_e::TRYLOCK, 0); }
inline bool signal() { return wake(wake_e::SIGNAL); }
inline bool broadcast() { return wake(wake_e::BROADCAST); }
inline bool timedwaitcond(vztimespec_t const *tspec) {
return wait(wait_e::WAITCOND, tspec);
}
};
_____________
I only have 2 virtual functions now, not counting dtor, ect... Well, 2
is better than 7, or 8?
Any thoughts?
:^)
-
Ian Collins。
--
Ian Collins.
" ;伊恩柯林斯 < ia ****** @ hotmail.comwrote in message
news:58 ************** @ mid.individual.net ...
"Ian Collins" <ia******@hotmail.comwrote in message
news:58**************@mid.individual.net...
Chris Thomasson写道:
Chris Thomasson wrote:
>考虑一个可以有7或8个函数的对象。如果你为接口创建了一个抽象基类。对象,那,
意味着7或8个纯虚函数对吗?那么,恕我直言,那太多了......很多...
>Consider an an object that that can has 7 or 8 functions. If you create
an abstract base class for the "interface" of the object, well, that
means 7 or 8 pure virtual functions right? Well, IMHO, that''s way too
much...
为什么,它会给你带来麻烦吗?
Why, is it causing you problems?
没问题。我认为它是个人的东西..; ^)
我认为当它的所有功能都可以分摊到7美元时,我不需要7到8个函数指针2或3个低级纯虚函数...您可以添加额外的功能
不增加虚拟表大小。我是
愿意在实际的:: wake(...)或:: wait(...)函数中获取命中
在枚举值上分支。
No problems. Its a personal thing I guess.. ;^)
I see no need for 7 to 8 function pointers when all of its functionality can
be amortized into 2 or 3 low-level pure virtual functions... You can add
extra "functionality" without increasing the virtual table size. I am
willing to take the hit in the actual ::wake(...) or ::wait(...) functions
wrt branching on the enum value.
>我想知道以下技术是否皱眉了on:
>I was wondering if the following technique is "frowned" upon:
你所做的只是在派遣中添加一个额外的间接层,
你希望从中获得什么?
All you are doing is adding an extra layer of indirection in dispatch,
what do you hope to gain from this?
摊销虚拟功能表大小...恕我直言,你不想传递
" interface"具有大量函数指针的对象...尝试将7
压缩为整个对象的8个函数,分为2或3个低级纯虚拟
函数...
[...]
Amortized virtual function table size... IMHO, you don''t want to pass around
"interface" objects that have tons of function pointers... Try to condense 7
to 8 functions of the object as a whole, into 2 or 3 low-level pure virtual
functions...
[...]
4月17日上午5:16,Chris Thomasson < cris ... @ comcast.netwrote:
On Apr 17, 5:16 am, "Chris Thomasson" <cris...@comcast.netwrote:
>
我认为当所有的时候都不需要7到8个函数指针它的功能可以分为2或3个低级纯虚函数......你可以添加额外的功能。不增加虚拟表大小。我是
愿意在实际的:: wake(...)或:: wait(...)函数中获取命中
在枚举值上分支。
摊销的虚拟功能表大小...恕我直言,你不想传递
" interface"具有大量函数指针的对象...尝试将7
压缩为整个对象的8个函数,分为2或3个低级纯虚拟
函数...
>
I see no need for 7 to 8 function pointers when all of its functionality can
be amortized into 2 or 3 low-level pure virtual functions... You can add
extra "functionality" without increasing the virtual table size. I am
willing to take the hit in the actual ::wake(...) or ::wait(...) functions
wrt branching on the enum value.
Amortized virtual function table size... IMHO, you don''t want to pass around
"interface" objects that have tons of function pointers... Try to condense 7
to 8 functions of the object as a whole, into 2 or 3 low-level pure virtual
functions...
(启用关于实现的常见警告,不一定使用
vtables和4字节指针......)
嗯那里有过早的优化,然后就是疯狂。
首先,你知道7或8个函数指针。每个类别存在一次
,而不是每个实例,对吧?
其次,为什么你认为传递
的界面对象大量的函数指针有问题吗?你知道那里
没有复制vtable,对吗?
所以你的问题归结为我不喜欢单一的想法在我的程序中,每个condmutex派生类的32个字节的
,我真的希望
将其降低到8个字节。我想大家都同意,除非
你有大量的子类并且处于一个非常有限的内存环境中,这有点傻。
(Enable usual caveats about implementations not necessarily using
vtables and 4-byte pointers ...)
Well there''s premature optimisation, and then there''s just madness.
Firstly, you do know that the "7 or 8 function pointers" exist once
per class, not per instance, right ?
Secondly, why do you think that passing around "interface objects that
have tons of function pointers" is a problem? You do know that there
is no copying of vtables going on, right ?
So your problem comes down to "I don''t like the idea of a single block
of 32 bytes per condmutex-derived class in my program, I really want
to push that down to 8 bytes". I think we can all agree that, unless
you have a vast number of subclasses and are in a very contrained
memory environment, this is a bit silly.
这篇关于减少虚函数表大小......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!