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

Functionoids vs Member Function Pointer?

I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function pointer array
instead of switch.

They believe that ordinary function pointer is much faster than
switch. Think of one variable called selectFunction. selectFunction
variable can be the range of 0 through 1,000 or more.

Load selectFunction variable into a register. Load memory address of
ordinary function pointer array into a register. Both registers can
be added together to create indirection before one of these 1,000
functions can be called.

Member function pointer array is slower than ordinary function pointer
because first step is to load "this" pointer to locate memory address
of member function pointer array. It is likely that member function
pointer array needs to execute indirction twice before one of these
mmber function is called. Only ordinary function pionter needs one
indirection.

How can you find a way how member function pointer array can execute
one indirection instead of two indirection? A vtable has a memory
address where constructor function and deconstructor function are the
top. You can use selectFunction variable to be added to "this" pinter
in vtable memory address. Then, one of these 1,000 member functions
in the vtable can be called.

I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

I would prefer to use static memory where member function pointer
array is stored at compile-time. I may do not need virtual functions
becaus I choose not to use dynamic binding.

Please advise which I should choose to use ordinary function pointer,
member function pointerarray, or functionoids.

Short example of code looks like this below.

int selectFunction = 0;

.....
do something ....
.....
selectFnction may be modified to have a value of 100.

.....
.....
FuncPtr [ selectFuntion ] ();
.....
100th member function is called...

Nephi
Sep 13 '08 #1
4 4339
Im************@hotmail.com wrote:
They believe that ordinary function pointer is much faster than
switch.
Looking up a branch location can be faster but it is often much slower.
Specifically, when there are a small number of choices it is faster to use
nested branches to fixed locations.
I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?
The C++ Lite FAQ is wrong. They are describing rudimentary precursors to the
first-class functions now found in all modern languages because they are
the only workarounds available in C++.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Sep 13 '08 #2
On Sep 13, 5:24*am, Jon Harrop <j...@ffconsultancy.comwrote:
Immortal_Ne...@hotmail.com wrote:
They believe that ordinary function pointer is much faster than
switch.

Looking up a branch location can be faster but it is often much slower.
Specifically, when there are a small number of choices it is faster to use
nested branches to fixed locations.
I did read C++ Lite FAQ and it does mention functionoids. *They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

The C++ Lite FAQ is wrong. They are describing rudimentary precursors to the
first-class functions now found in all modern languages because they are
the only workarounds available in C++.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.http://www.ffconsultancy.com/?u
Dr Jon D Harrop,

I agree that nested branch like switch keyword, but sometimes, un-
nested branch with 1,000 branches in "JMP Table" is faster. It
depends on the processor specification. If you force all functions to
be inlined in un-nested branch and nested branch, then C++ Compiler
will fail to compile because it exceeds branch limit. Not all C++
compilers have exceeded limit support.

Branch misprediction is a major problems. I don't know if modern
machines like x86 32 or x86 64 at 3 GHz may solve it.

I believe that function pointer with "CALL Table" may be preferred.
You have to make sure that stack frame is not needed. Then all sub-
functions can be inlined in each function inside function pointer
table.

My question is -- do you recommend me to use ordinary function pointer
or member function pointer? Ordinary function pointer is faster
because it uses one indirection. I can use class as long as I want,
but I will define member function pointers to be static.

What do you think?

Nephi
Sep 13 '08 #3
On 2008-09-13 04:50, Im************@hotmail.com wrote:
I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function pointer array
instead of switch.

They believe that ordinary function pointer is much faster than
switch. Think of one variable called selectFunction. selectFunction
variable can be the range of 0 through 1,000 or more.

Load selectFunction variable into a register. Load memory address of
ordinary function pointer array into a register. Both registers can
be added together to create indirection before one of these 1,000
functions can be called.

Member function pointer array is slower than ordinary function pointer
because first step is to load "this" pointer to locate memory address
of member function pointer array. It is likely that member function
pointer array needs to execute indirction twice before one of these
mmber function is called. Only ordinary function pionter needs one
indirection.

How can you find a way how member function pointer array can execute
one indirection instead of two indirection? A vtable has a memory
address where constructor function and deconstructor function are the
top. You can use selectFunction variable to be added to "this" pinter
in vtable memory address. Then, one of these 1,000 member functions
in the vtable can be called.

I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

I would prefer to use static memory where member function pointer
array is stored at compile-time. I may do not need virtual functions
becaus I choose not to use dynamic binding.

Please advise which I should choose to use ordinary function pointer,
member function pointerarray, or functionoids.
Have you carefully profiled your code and analysed the results and from
that decided that the calling mechanism is the best place to optimise
your application? Unless you have you should do so before doing
something that will further obscure your code.
Short example of code looks like this below.

int selectFunction = 0;

....
do something ....
....
selectFnction may be modified to have a value of 100.

....
....
FuncPtr [ selectFuntion ] ();
....
Can you reorganise the code to call the correct function instead of
setting selectFunction to a value? If possible that should be the most
performant way to do things since the compiler knows which function to
call and can do stuff like inlining etc.

Another possibility is to replace selectFunction with a function-pointer
and assign the correct function to it where where you today assigns a
value to selectFunction.

--
Erik Wikström
Sep 13 '08 #4
On Sep 13, 4:50 am, Immortal_Ne...@hotmail.com wrote:
I had a lot of research to see how function pointer works.
Sometimes, programmers choose switch keyword and function in
each case block can be called. Sometimes, they choose
ordinary function pointer array instead of switch.
They believe that ordinary function pointer is much faster
than switch.
Who'd be stupid enough to believe something like that. If an
indirect jump is the fastest method on a given machine, that's
what the compiler will generate for a switch. In the distant
past (before extensive pipelining), it was usual for compilers
to break switches down into different categories: a dense switch
would be implemented by means of bounds checking and a jump
table, a scattered switch would use a lookup table to determine
the index, and then the jump table. And small switches would be
implemented as a hardwired binary search, using code more or
less equivalent to nested if's. The cut-off point where the use
of the jump table became a win depends on the processor; on
older machines, without extensive pipelining, it's often as
little as four cases. On a modern machine, with extensive
pipelining, branch prediction (which is often invalidated by an
indirect call), etc., the cut-off is often a couple of hundred,
or even a couple of thousand; some compilers probably don't even
bother with even considering the jump table, since the cut-off
is high enough that it will never be reached in reasonable code.
(Which is debattable: in hand written code, of course, you
probably will never have more than a couple of dozen cases, but
in machine generated code, switches with several hundred cases
or more are not unreasonable.)

At any rate, it's the compiler's problem to get this right, not
yours.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 14 '08 #5

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.