Global Variable vs Struct Variable Question

  • Follow


    I have read Pentium IV Optimization manual that it explains to avoid 
global variable because it is slow.  MOV instruction has to direct memory 
address to retrieve global variable, but it does not have pointer.  It makes 
sense that global variable has its own memory address individually. 
Individual memory address does not store in the cache.
    Now, some local variables are stored in struct that struct always have 
pointer.  MOV instruction has to direct pointer once before it begins to 
indirect memory address to retrieve local variable through pointer like 
struct.  Pointer does store in the cache because pointer is used again and 
again.
    My question is -- struct and class in C++ are useful for local 
variables, but it is a very bad idea to bind member functions through 
pointer.  Member functions inside struct or class can slow and degrade the 
performance on Pentium IV.  It is preferable to use global functions outside 
of struct or class while global functions can still access local variable 
inside struct or class.
    My test with timing stamp shows that member function inside struct and 
class are slower than global functions.  How can you please explain why it 
happens?  Why do programmers encourage to use member functions through 
pointer because they claim that member functions can be stored in the cache 
for better performance?
    It is very important to me that I need to know.

Bryan Parkoff 

0
Reply Bryan 3/28/2005 7:39:08 AM

Bryan Parkoff wrote:
>     I have read Pentium IV Optimization manual that it explains to avoid 
> global variable because it is slow.  MOV instruction has to direct memory 
> address to retrieve global variable, but it does not have pointer.  It makes 
> sense that global variable has its own memory address individually. 
> Individual memory address does not store in the cache.
>     Now, some local variables are stored in struct that struct always have 
> pointer.  MOV instruction has to direct pointer once before it begins to 
> indirect memory address to retrieve local variable through pointer like 
> struct.  Pointer does store in the cache because pointer is used again and 
> again.
>     My question is -- struct and class in C++ are useful for local 
> variables, but it is a very bad idea to bind member functions through 
> pointer.  Member functions inside struct or class can slow and degrade the 
> performance on Pentium IV.  It is preferable to use global functions outside 
> of struct or class while global functions can still access local variable 
> inside struct or class.
>     My test with timing stamp shows that member function inside struct and 
> class are slower than global functions.  How can you please explain why it 
> happens?  Why do programmers encourage to use member functions through 
> pointer because they claim that member functions can be stored in the cache 
> for better performance?
>     It is very important to me that I need to know.
> 
> Bryan Parkoff 


Calling a function by a pointer to the address to that function is a 
slower than calling it by having the address of that function as a 
literal in the instruction stream.
The reason you would use pointers to functions within a structure (or 
class) would usually be because different actions would need to be taken 
for different instances of that structure and the different functions 
that were pointed to within it would do the right thing.  You could have 
a master function that could determine the "type" of each instance and 
then use a jump table (switch/case) to do the right action, but this 
would require that all subtypes and their associated actions be known at 
the time that this function was written.  By using the pointers to the 
functions within the struct (or having a pointer to another struct that 
represents that type and has static members and pointers to methods) you 
could link in a new type that implements the interface of another type 
that the program knows about and it could use it, even if this new type 
were not even dreamed of when the main program was compiled.
I suspect that in many benchmarks the penalty for these types of calls 
is heavier than in actual use.  In actual use if you used the master 
method that knew what actions to take for all sub-types, the jump table 
that would be used for the different actions would probably eat up more 
than enough time to make up for the call to a function pointer.

Nathan Moore

0
Reply Nathan 3/28/2005 6:01:27 PM


"Nathan Moore" <spamtrap@crayne.org> wrote in message 
news:aeP1e.552$WM6.77@okepread07...
> Calling a function by a pointer to the address to that function is a 
> slower than calling it by having the address of that function as a literal 
> in the instruction stream.
> The reason you would use pointers to functions within a structure (or 
> class) would usually be because different actions would need to be taken 
> for different instances of that structure and the different functions that 
> were pointed to within it would do the right thing.  You could have a 
> master function that could determine the "type" of each instance and then 
> use a jump table (switch/case) to do the right action, but this would 
> require that all subtypes and their associated actions be known at the 
> time that this function was written.  By using the pointers to the 
> functions within the struct (or having a pointer to another struct that 
> represents that type and has static members and pointers to methods) you 
> could link in a new type that implements the interface of another type 
> that the program knows about and it could use it, even if this new type 
> were not even dreamed of when the main program was compiled.
> I suspect that in many benchmarks the penalty for these types of calls is 
> heavier than in actual use.  In actual use if you used the master method 
> that knew what actions to take for all sub-types, the jump table that 
> would be used for the different actions would probably eat up more than 
> enough time to make up for the call to a function pointer.
    Thank you for the advice so you recommend to use global function and 
member variables inside struct, but not member functions inside struct. 
Correct?  I tried to construct large Jump Table that requires to have large 
one global function body.  Let says Jump Table has 10,000 blocks in one 
large global functions.  I used __forceinline keyword to all small 10,000 
functions so they can be in one large function with Jump Table, but C++ 
Compiler crashed because of large blocks in one function.
    It is why programmers recommend to use function array under pointer as 
the global functions so they do not need to be bound inside struct or class. 
It is the same situation when they tried to write large Jump Table using 
MASM 6.15 and it crashed too.  It is recommended to have small global 
functions through function array under pointer.
    Do you know what I mean?

Bryan Parkoff

0
Reply Bryan 3/28/2005 8:22:54 PM

Bryan Parkoff wrote:
>     Thank you for the advice so you recommend to use global function and 
> member variables inside struct, but not member functions inside struct. 

What?  I don't even know what you are asking.

> Correct?  I tried to construct large Jump Table that requires to have large 
> one global function body.  Let says Jump Table has 10,000 blocks in one 
> large global functions.

What?

>  I used __forceinline keyword to all small 10,000 

You are not using assembly if you use things like __forceinline.

> functions so they can be in one large function with Jump Table, but C++ 
> Compiler crashed because of large blocks in one function.

My explination dealt with implementing certain things that are in C++ 
with asm in an effecient and flexible way.  I did not tell you which way 
you should do anything -- that is for you to decide based on your needs 
and design parameters.

>     It is why programmers recommend to use function array under pointer as 
> the global functions so they do not need to be bound inside struct or class. 
> It is the same situation when they tried to write large Jump Table using 
> MASM 6.15 and it crashed too.  It is recommended to have small global 
> functions through function array under pointer.
>     Do you know what I mean?

No.

I said that if you want to implement something like C++ virtual methods, 
you will either have to have a pointer to the correct method/function 
within the structure or have a master function that can figure out what 
the type of that instance is and then do the right thing.  The first is 
more flexible.  The real world performance of each is probably about the 
same, though.

An array to function pointers is an efficient way to implement a type of 
jump table b/c calls are special jumps.

0
Reply Nathan 3/28/2005 11:21:24 PM

"Bryan Parkoff"  <spamtrap@crayne.org> wrote:
..
>    I have read Pentium IV Optimization manual that it explains to avoid 
>global variable because it is slow.

No.  The processor cannot tell the difference between a global variable, a
local variable, a stack variable, or a class member.  All of them are just
bytes in memory.  It's true that memory access is slower than register
acccess, but you can't avoid memory.  There aren't enough registers.

>    My test with timing stamp shows that member function inside struct and 
>class are slower than global functions.  How can you please explain why it 
>happens?  Why do programmers encourage to use member functions through 
>pointer because they claim that member functions can be stored in the cache 
>for better performance?

Because the difference is ABSOLUTELY unimportant.  You're talking about 2
cycles of additional overhead for a function that might take thousands of
cycles to execute.  The member function concept allows programmers to write
better programs in less time -- programs that are easier to maintain.

You have the concepts all backwards, Bryan.  That computer is there to make
MY life easier.  It is not MY job to make the processor's life easier.
Optimization has its place, but nonsense optimization at the cost of
programmer productivity is a completely ridiculous notion.

>    It is very important to me that I need to know.

No, it isn't.
-- 
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

0
Reply Tim 3/30/2005 8:19:52 AM

4 Replies
207 Views

(page loaded in 0.08 seconds)

Similiar Articles:













7/29/2012 6:46:27 PM


Reply: