B
Bryan Parkoff
You may notice that switch (...) is much faster than function that can
gain a big improved performance because it only use JMP instruction however
function is required to use CALL, PUSH, and POP instruction that can be
slower.
I created three functions in an array. Array is called
pArray_Func[xx](). pArray_Func[xx]() contains three functions. "xx" can be
used to point each function.
Let say, I have over 1,000 functions. I would put "inline" to each
functions. It does not matter that each function may contain more than 50
lines. It is really BIG that C/C++ compiler tells "inline" is used for
short like 3-5 lines ONLY. I have decided to tell C/C++ compiler by forcing
"inline" there because I don't care if my code is getting bigger and bigger.
I allow 50 lines to be copied to each functions. CALL, PUSH, and POP
instructions can always be removed automatically by the C/C++ compiler
before JMP instruction can always be placed each function.
It is very neat according to my design that I wanted. If I care to keep
small code rather than big code, "inline" should not be used.
Right now, there is a big problem. I can't place "inline" into
pArray_Func[xx](). I can see why. JMP can show "JMP [EAX+EDX*4] that will
work fine. C/C++ compiler can only place "CALL [EAX+EDX*4] into
pArrayFunc[xx](). It is not designed to replace from "CALL [EAX+EDX*4]" to
"JMP [EAX+EDX*4] if "inline" is there. C/C++ compiler will always ignore
"inline".
Another option is to place 1,000 functions inside switch (...). Look
like below.
switch (xx)
{
case 1:
Func1();
case 2:
Func2();
case 3:
Func3();
}
C/C++ compiler can see this code above before it can place "inline" to
each functions inside switch (...). It is very annoying me because I have
to read switch (...) from line to line by scrolling up/down. It is very
long for me to read. It is not HARD to FIND BUG (off my point).
I would want to see only 3 or 4 lines there instead of over 20,000 lines
that contains switch (...). pArrayFunc() may be very useful. We should
tell the C/C++ authors to add keyword like __USE_JMP_INLINE instead of
__inline, _inline, or inline.
Newest version of C/C++ compiler like Intel Compiler can accept
__USE_JMP_INLINE. "__USE_JMP_INLINE" will be added to pArrayFunc[xx](), but
"inline" will always be same for 1,000 functions. After you compile your
C++ source code, it will recognize "__USE_JMP_INLINE", it will rename from
"CALL [EAX+EDX*4]" to "JMP [EAX+EDX*4]" so each functions' PUSH and POP
instructions will be removed. It will look like real JMP Table inside
pArrayFunc[xx](). It would be perfect for easy reading.
Please state your opinion what you think about my idea to modify C/C++
compiler's source code. ***Do not tell that there is no __USE_JMP_INLINE to
all C/C++ compiler. Please ask the authors to add __USE_JMP_INLINE keyword.
It would be perfect and neat C++ coding.***
I have put pArrayFunc[xx]() inside C++ class example below. It gives
you an idea however "__USE_JMP_INLINE" does not EXIST, but should be added
near the future.
#include <stdio.h>
class Test
{
public:
inline void Get_a() { printf("A: %d\n", _a); }
inline void Get_b() { printf("B: %d\n", _b); }
inline void Get_c() { printf("C: %d\n", _c); }
inline void Set_a(unsigned int a) { _a = a; }
inline void Set_b(unsigned int b) { _b = b; }
inline void Set_c(unsigned int c) { _c = c; }
typedef void (Test::*Action)();
static const Action Menu[3];
inline void Call();
private:
unsigned int _a;
unsigned int _b;
unsigned int _c;
};
const Test::Action Test::Menu[3] =
{
&Test::Get_a,
&Test::Get_b,
&Test::Get_c
};
inline void Test::Call()
{
for (int a = 0; a <= 2; a++)
(this->*Menu[a])();
}
void main(void)
{
Test test;
test.Set_a(1);
test.Set_b(2);
test.Set_c(3);
test.Call();
return;
}
gain a big improved performance because it only use JMP instruction however
function is required to use CALL, PUSH, and POP instruction that can be
slower.
I created three functions in an array. Array is called
pArray_Func[xx](). pArray_Func[xx]() contains three functions. "xx" can be
used to point each function.
Let say, I have over 1,000 functions. I would put "inline" to each
functions. It does not matter that each function may contain more than 50
lines. It is really BIG that C/C++ compiler tells "inline" is used for
short like 3-5 lines ONLY. I have decided to tell C/C++ compiler by forcing
"inline" there because I don't care if my code is getting bigger and bigger.
I allow 50 lines to be copied to each functions. CALL, PUSH, and POP
instructions can always be removed automatically by the C/C++ compiler
before JMP instruction can always be placed each function.
It is very neat according to my design that I wanted. If I care to keep
small code rather than big code, "inline" should not be used.
Right now, there is a big problem. I can't place "inline" into
pArray_Func[xx](). I can see why. JMP can show "JMP [EAX+EDX*4] that will
work fine. C/C++ compiler can only place "CALL [EAX+EDX*4] into
pArrayFunc[xx](). It is not designed to replace from "CALL [EAX+EDX*4]" to
"JMP [EAX+EDX*4] if "inline" is there. C/C++ compiler will always ignore
"inline".
Another option is to place 1,000 functions inside switch (...). Look
like below.
switch (xx)
{
case 1:
Func1();
case 2:
Func2();
case 3:
Func3();
}
C/C++ compiler can see this code above before it can place "inline" to
each functions inside switch (...). It is very annoying me because I have
to read switch (...) from line to line by scrolling up/down. It is very
long for me to read. It is not HARD to FIND BUG (off my point).
I would want to see only 3 or 4 lines there instead of over 20,000 lines
that contains switch (...). pArrayFunc() may be very useful. We should
tell the C/C++ authors to add keyword like __USE_JMP_INLINE instead of
__inline, _inline, or inline.
Newest version of C/C++ compiler like Intel Compiler can accept
__USE_JMP_INLINE. "__USE_JMP_INLINE" will be added to pArrayFunc[xx](), but
"inline" will always be same for 1,000 functions. After you compile your
C++ source code, it will recognize "__USE_JMP_INLINE", it will rename from
"CALL [EAX+EDX*4]" to "JMP [EAX+EDX*4]" so each functions' PUSH and POP
instructions will be removed. It will look like real JMP Table inside
pArrayFunc[xx](). It would be perfect for easy reading.
Please state your opinion what you think about my idea to modify C/C++
compiler's source code. ***Do not tell that there is no __USE_JMP_INLINE to
all C/C++ compiler. Please ask the authors to add __USE_JMP_INLINE keyword.
It would be perfect and neat C++ coding.***
I have put pArrayFunc[xx]() inside C++ class example below. It gives
you an idea however "__USE_JMP_INLINE" does not EXIST, but should be added
near the future.
#include <stdio.h>
class Test
{
public:
inline void Get_a() { printf("A: %d\n", _a); }
inline void Get_b() { printf("B: %d\n", _b); }
inline void Get_c() { printf("C: %d\n", _c); }
inline void Set_a(unsigned int a) { _a = a; }
inline void Set_b(unsigned int b) { _b = b; }
inline void Set_c(unsigned int c) { _c = c; }
typedef void (Test::*Action)();
static const Action Menu[3];
inline void Call();
private:
unsigned int _a;
unsigned int _b;
unsigned int _c;
};
const Test::Action Test::Menu[3] =
{
&Test::Get_a,
&Test::Get_b,
&Test::Get_c
};
inline void Test::Call()
{
for (int a = 0; a <= 2; a++)
(this->*Menu[a])();
}
void main(void)
{
Test test;
test.Set_a(1);
test.Set_b(2);
test.Set_c(3);
test.Call();
return;
}