*context safe* exceptions
davidl
davidl at 126.com
Mon Sep 10 03:34:51 PDT 2007
I read this :
http://dlang.group.javaeye.com/group/topic/2806
C++ code:
#include <stdio.h>
#include <exception>
volatile int turn=0;
volatile int count;
void fooFunc(void)
{
++ count;
};
typedef void (*foo_t)(void);
void test(int c1, int c2, foo_t foo)
{
for (int j=0;j<c1;j++)
{
++ turn;
count = 0;
for (int i=0;i<c2;i++)
{
try
{
foo();
}
catch (std::exception &e)
{
}
}
}
printf("\nturn %d, count %d", turn, count);
}
int main()
{
test(8*1024, 1048576, fooFunc);
return 0;
}
C++ code runs way faster than D, why??
Finally, the culprit comes out that D code doesn't optimize the args of i,
j to registers. also the function pointer func is not inlined, MSVC seems
totally make the args 'instanciated', so the func looks like a template
gened code.
And why we need to access i, j from memory?
Because the exception system is not context safe.
So we lose a big chance of optimzing those vars to registers(Even if
there's not exception get thrown, compiler can not assume the context
consistent).
--
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list