473,397 Members | 2,116 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,397 software developers and data experts.

tweaking the virtual function table pointer

Case:

-- class X has occupies tiny amount of memory:

sizeof(X) is only a little greater than sizeof(void*).

-- X instantiates thousands of objects and memory does matter.

-- The class has a virtual destructor, and therefore, a pointer
to a virtual function table.

-- This class, now, needs a boolean variable 'boolF' which would
cost at the very least 1 byte, which is a lot under the
circumstances described above.

--> idea: (1) copy the virtual function table at another place.

(2) let the virtual function pointer point to either
one of the virtual function tables (which are
identical), but:

The place where the pointer points to indicates the
state of the 'implicitly represented' variable
'boolF'.

Such a setup is profitable, if

N * sizeof(bool) > sizeof(virtual function table),

where N = estimated number of instantiated objects.

Does the standard impose the management of virtual function tables
strictly enough to elaborate on such a solution? Is such a
solution practical?

Thanks

Frank

Sep 23 '05 #1
7 1756
Frank-René Schäfer wrote:
[...]
Does the standard impose the management of virtual function tables
strictly enough to elaborate on such a solution? Is such a
solution practical?


The Standard says nothing about "management of virtual function tables"
since it's outside of the realm of its concern. It's an implementation
detail.

V
Sep 23 '05 #2

"Frank-René Schäfer" <fr**************@gmx.net> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Case:

-- class X has occupies tiny amount of memory:

sizeof(X) is only a little greater than sizeof(void*).

-- X instantiates thousands of objects and memory does matter.

-- The class has a virtual destructor, and therefore, a pointer
to a virtual function table.

-- This class, now, needs a boolean variable 'boolF' which would
cost at the very least 1 byte, which is a lot under the
circumstances described above.

--> idea: (1) copy the virtual function table at another place.

(2) let the virtual function pointer point to either
one of the virtual function tables (which are
identical), but:

The place where the pointer points to indicates the
state of the 'implicitly represented' variable
'boolF'.

Such a setup is profitable, if

N * sizeof(bool) > sizeof(virtual function table),

where N = estimated number of instantiated objects.

Does the standard impose the management of virtual function tables
No. The standard does not at all dictate how virtual function
behavior is implemented, only that virtual functions behave in
a particular manner. There's no requirement to use a mechanism
such as a 'vtable' (although afaik many/ most compilers do use them).
strictly enough to elaborate on such a solution? Is such a
solution practical?


That depends upon your needs, but I doubt it.

-Mike
Sep 23 '05 #3
* =?iso-8859-1?B?RnJhbmstUmVu6SBTY2jkZmVy?=:
Case:

-- class X has occupies tiny amount of memory:

sizeof(X) is only a little greater than sizeof(void*).

-- X instantiates thousands of objects and memory does matter.

-- The class has a virtual destructor, and therefore, a pointer
to a virtual function table.

-- This class, now, needs a boolean variable 'boolF' which would
cost at the very least 1 byte, which is a lot under the
circumstances described above.

--> idea: (1) copy the virtual function table at another place.

(2) let the virtual function pointer point to either
one of the virtual function tables (which are
identical), but:

The place where the pointer points to indicates the
state of the 'implicitly represented' variable
'boolF'.

Such a setup is profitable, if

N * sizeof(bool) > sizeof(virtual function table),

where N = estimated number of instantiated objects.

Does the standard impose the management of virtual function tables
strictly enough to elaborate on such a solution?

Is such a solution practical?


No, the standard does not specify whether there _is_ a vtable or not.

However, if your objects are not handled polymorphically, then simply get rid
of that virtual destructor, and you've made room for your boolean.

If on the other hand the objects are handled polymorphically you might
implement the essentials of your idea by using two classes derived from a
common base class, one representing 'false' and the other 'true'.

Another idea can be to store the booleans externally to the objects, as a
bitset. For that idea you need some way to easily associate each object with
a unique index, without more memory overhead. That of course depends on your
objects.

And a third idea, to use the flyweight pattern, in essence to store pure data
objects and bring the functionality to the data (or vice versa, depending on
your point of view) when required.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 23 '05 #4
Frank-René Schäfer wrote:
Case:

-- class X has occupies tiny amount of memory:

sizeof(X) is only a little greater than sizeof(void*).

-- X instantiates thousands of objects and memory does matter.

-- The class has a virtual destructor, and therefore, a pointer
to a virtual function table.

-- This class, now, needs a boolean variable 'boolF' which would
cost at the very least 1 byte, which is a lot under the
circumstances described above.

--> idea: (1) copy the virtual function table at another place.

(2) let the virtual function pointer point to either
one of the virtual function tables (which are
identical), but:

The place where the pointer points to indicates the
state of the 'implicitly represented' variable
'boolF'.

Such a setup is profitable, if

N * sizeof(bool) > sizeof(virtual function table),

where N = estimated number of instantiated objects.

Does the standard impose the management of virtual function tables
strictly enough to elaborate on such a solution? Is such a
solution practical?

Thanks

Frank


Flyweight (mentioned by A.Steinbach) is a good solution.

Is there any reason why you actually need these objects instantiated
simultaneously? I mean if you had (say) a vector<char> v, you could
create a temporary

myfn(ExpensiveChar(v[i]))

That is, only create the object when you need it, and store it
efficiently when you don't.

Calum
Sep 23 '05 #5
Actually, I was considering this 'design' as a kind of 'flyweight'
pattern,
where the key is the pointer to the vtable.

-- What about the typeid()-operator? Considering your second proposal,
with twol classes, let's say "X_false", the other "X_true". How
could I
turn the switch for an existing object, i.e. that it's virtual
pointer points
to it's counterpart?

Objects of type X_false and X_true will have the same size and
structure,
but is it sure, that if the tweaking works, that the deallocation is
handled
100% propperly?

Sep 23 '05 #6
* =?iso-8859-1?B?RnJhbmstUmVu6SBTY2jkZmVy?=:
Actually, I was considering this 'design' as a kind of 'flyweight'
pattern,
It isn't. See the posting by Calum Grant in this thread. Or, google. ;-)

where the key is the pointer to the vtable.

-- What about the typeid()-operator?
It reports the dynamic type of an object, which with your proposed solution
would vary.

Considering your second proposal,
with twol classes, let's say "X_false", the other "X_true". How
could I turn the switch for an existing object, i.e. that it's
virtual pointer points to it's counterpart?
The implementation I sketched assumed, without mention, that the boolean would
be constant for each object. However, with only polymorphic usage you can do
dirty tricks. Not that I recommend this, and I'm not even sure whether it's
well-defined or perhaps undefined behavior, and since you're counting _bytes_
this solution requires a really efficient small object allocator (otherwise
you can easily have, say, 24 bytes "invisible" overhead per object):

#include <iostream>
#include <stdexcept>
#include <memory>

class Data {};
class SomeInterface{ public: virtual ~SomeInterface() {} };

class Base: public SomeInterface
{
public:
virtual bool boolValue() const = 0;
virtual void setBoolValue( bool newValue ) = 0;

Base& operator=( Base const& other )
{
myData = other.myData; // Could be optimized.
setBoolValue( other.boolValue() );
}

static Base* Base::instanceAt( void* storage, Data const& data, bool
aBoolValue );
static std::auto_ptr<Base> newInstance( Data const& data, bool aBoolValue
);

private:
class DerivedFalse; friend class DerivedFalse;
class DerivedTrue; friend class DerivedTrue;

Data myData;

Base( Data const& data ): myData( data ) {};
Base( Base const& ); // None.
};

class Base::DerivedFalse: public Base
{
public:
DerivedFalse( Data const& data ): Base( data ) {}
virtual bool boolValue() const { return false; }
virtual void setBoolValue( bool newValue );
};

class Base::DerivedTrue: public Base
{
public:
DerivedTrue( Data const& data ): Base( data ) {}
virtual bool boolValue() const { return true; }
virtual void setBoolValue( bool newValue );
};

Base* Base::instanceAt( void* storage, Data const& data, bool aBoolValue )
{
if( aBoolValue )
{
return new( storage ) DerivedTrue( data );
}
else
{
return new( storage ) DerivedFalse( data );
}
}

std::auto_ptr<Base> Base::newInstance( Data const& data, bool aBoolValue )
{
// ASSERT sizeof(Base) == sizeof(Base::DerivedFalse)
// ASSERT sizeof(Base) == sizeof(Base::DerivedTrue)
return std::auto_ptr<Base>(
instanceAt( new char[sizeof(Base)], data, aBoolValue )
);
}

void Base::DerivedFalse::setBoolValue( bool newValue )
{
if( newValue != boolValue() )
{
Data data = myData;
this->~DerivedFalse();
new( this ) DerivedTrue( data );
}
}

void Base::DerivedTrue::setBoolValue( bool newValue )
{
if( newValue != boolValue() )
{
Data data = myData;
this->~DerivedTrue();
new( this ) DerivedFalse( data );
}
}

int main()
{
try
{
std::auto_ptr<Base> p( Base::newInstance( Data(), true ) );

std::cout << sizeof( Base ) << std::endl;
std::cout << std::boolalpha;

std::cout << p->boolValue() << std::endl;
p->setBoolValue( false );
std::cout << p->boolValue() << std::endl;
p->setBoolValue( true );
std::cout << p->boolValue() << std::endl;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
}

Objects of type X_false and X_true will have the same size and
structure, but is it sure, that if the tweaking works, that the
deallocation is handled 100% propperly?


I think so.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 23 '05 #7
* Alf P. Steinbach:
std::auto_ptr<Base> Base::newInstance( Data const& data, bool aBoolValue )
{
// ASSERT sizeof(Base) == sizeof(Base::DerivedFalse)
// ASSERT sizeof(Base) == sizeof(Base::DerivedTrue)
return std::auto_ptr<Base>(
instanceAt( new char[sizeof(Base)], data, aBoolValue )
);
}


Forgot to fix that before posting, t'was just a raw hack. But I think you get
the idea in spite of that formally very incorrect code -- the problem with
this code is allocation as char array and deallocation as Base object, which
is Not Good (TM). That must be fixed for real code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 23 '05 #8

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

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
8
by: siddhu | last post by:
Like other virtual functions does the entry of virtual destructor exist in VTABLE?
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.