Compiler/JIT optimization causing problems

P

Prasad Dabak

Hello,

I am facing some serious problems in my C# application probably due to
compiler/JIT optimization. Let is give an hypothetical example that
demonstrates the problem I am running into


class class1 {
public static string func(string param) {
string processedvalue;
//Process the param and return the value
...
...
...
return processedvalue;
}
}

class class2 {
static string x=class1.func("testparam");

public string func() {
return x;
}


class MainClass {
[STAThread]
static void Main(string[] args) {
class2 x=new class2();
x.func();

class2 y=new class2();
y.func();
}
}


Now, class1.func() function is getting called only once. Although it
is passed same parameter the return value of the function may differ
depending upon some external factors.

I want to ensure, that, class1.func() is always called and not
optimized at all.

Is there any way to tell compiler/JIT not to do any optmization for
call to class1.func at all. I know that removing static for varible
class2.x solves this problem. However, I don't want to do that, as,
there are too many places II would need to do that in my application.

-Prasad
 
J

Jon Shemitz

Prasad said:
I am facing some serious problems in my C# application probably due to
compiler/JIT optimization. Let is give an hypothetical example that
demonstrates the problem I am running into
class class2 {
static string x=class1.func("testparam");
Now, class1.func() function is getting called only once. Although it
is passed same parameter the return value of the function may differ
depending upon some external factors.

I want to ensure, that, class1.func() is always called and not
optimized at all.

Is there any way to tell compiler/JIT not to do any optmization for
call to class1.func at all. I know that removing static for varible
class2.x solves this problem. However, I don't want to do that, as,
there are too many places II would need to do that in my application.

This has nothing to do with the jitter. The compiler is doing exactly
what you told it to do; the defined semantics of an initialized static
are that it will be initialized once, before it's first read.

If you want class1.func called every time you call class2.func, you'll
have to make x an instance variable, or explicitly reset it within
every call to class2.func.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top