473,387 Members | 1,785 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,387 software developers and data experts.

Property accessors and Optimization

Given a property define like this:

public decimal ControlAmount
{
get { return mControlAmount; }
set { mControlAmount = value; }
}

I expected (under release mode) that when used as a variable in normal code
(ie, nothing that is enumerating properties or requiring metadata) eg:

ControlAmount += MyValue;

That the get accessor would be "optimized out", and replaced with direct
access to the variable (like an "inline function"). This is what would have
happened in C++ or Delphi.

However, I am finding that this is not the case in my code, even in release
mode. I profile my application, and find that the get_ControlAmount function
not only gets called *all the time*, but that it has significant overhead -
much more so than a simple member variable access.

Is there any way to optimize simple properties like this, or ensure that the
optimizer is able to remove the call?
Apr 26 '06 #1
7 1935
Did you compile with optimizations turned on (command line argument
/optimize+)?
If you didn't, the JIT will not optimize the getter body.
Note also that the getter is inlined, what's not inlined is the code
generated by:
return mControlAmount;
guess this is done in order to reduce the code size.

Note that System.Decimal is a complex type, and the JIT will try to optimize
it's behavior by using MMX instructions when available on the platform.

Willy.

"Jaimi McEntire" <Ja***********@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
| Given a property define like this:
|
| public decimal ControlAmount
| {
| get { return mControlAmount; }
| set { mControlAmount = value; }
| }
|
| I expected (under release mode) that when used as a variable in normal
code
| (ie, nothing that is enumerating properties or requiring metadata) eg:
|
| ControlAmount += MyValue;
|
| That the get accessor would be "optimized out", and replaced with direct
| access to the variable (like an "inline function"). This is what would
have
| happened in C++ or Delphi.
|
| However, I am finding that this is not the case in my code, even in
release
| mode. I profile my application, and find that the get_ControlAmount
function
| not only gets called *all the time*, but that it has significant
overhead -
| much more so than a simple member variable access.
|
| Is there any way to optimize simple properties like this, or ensure that
the
| optimizer is able to remove the call?
|
|
Apr 26 '06 #2
Jaimi,
Is there any way to optimize simple properties like this, or ensure that the
optimizer is able to remove the call?


Which profiler are you using? It's possible that it prevents certain
jit optimizations from happening. Simple property accessors like these
should definitely be inlined.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 26 '06 #3
Thank you for your response. I'm using the profiler that is built into
"Visual Studio 2005 Team Edition for Software Developers", and I configured
it to use Instrumentation instead of sampling. Are there any known issues
with this profiler or configuration? It appears that it instruments the
module after compiling, and I would have thought that these kind of
optimizations would be done by the compiler (and not by the JIT). Is this not
the case?

"Mattias Sjögren" wrote:
Jaimi,
Is there any way to optimize simple properties like this, or ensure that the
optimizer is able to remove the call?


Which profiler are you using? It's possible that it prevents certain
jit optimizations from happening. Simple property accessors like these
should definitely be inlined.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 26 '06 #4
Yes, definitely optimizations are on, and in release mode. I was dealing with
a performance problem (testing with the VSTE profiler in release mode), when
I saw all of the getters on the classes taking a considerable amount of time.
I solved the original issue, but just that fact that the getters existed was
disturbing - on every property, even ints.
Mattias suggested that perhaps this is caused by the profiler itself an is
not indicative of normal behavior (which leads me to question the usefulness
of the profiler, if the very use of it will remove such basic optimization).

"Willy Denoyette [MVP]" wrote:
Did you compile with optimizations turned on (command line argument
/optimize+)?
If you didn't, the JIT will not optimize the getter body.
Note also that the getter is inlined, what's not inlined is the code
generated by:
return mControlAmount;
guess this is done in order to reduce the code size.

Note that System.Decimal is a complex type, and the JIT will try to optimize
it's behavior by using MMX instructions when available on the platform.

Willy.

"Jaimi McEntire" <Ja***********@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
| Given a property define like this:
|
| public decimal ControlAmount
| {
| get { return mControlAmount; }
| set { mControlAmount = value; }
| }
|
| I expected (under release mode) that when used as a variable in normal
code
| (ie, nothing that is enumerating properties or requiring metadata) eg:
|
| ControlAmount += MyValue;
|
| That the get accessor would be "optimized out", and replaced with direct
| access to the variable (like an "inline function"). This is what would
have
| happened in C++ or Delphi.
|
| However, I am finding that this is not the case in my code, even in
release
| mode. I profile my application, and find that the get_ControlAmount
function
| not only gets called *all the time*, but that it has significant
overhead -
| much more so than a simple member variable access.
|
| Is there any way to optimize simple properties like this, or ensure that
the
| optimizer is able to remove the call?
|
|

Apr 26 '06 #5
Jaimi McEntire <Ja***********@discussions.microsoft.com> wrote:
Thank you for your response. I'm using the profiler that is built into
"Visual Studio 2005 Team Edition for Software Developers", and I configured
it to use Instrumentation instead of sampling. Are there any known issues
with this profiler or configuration? It appears that it instruments the
module after compiling, and I would have thought that these kind of
optimizations would be done by the compiler (and not by the JIT). Is this not
the case?


No, inlining is done by the JIT. This is absolutely necessary, as
properties are used by classes which wouldn't have access in the IL to
the backing fields. The compiler has to emit valid IL, which can't
access the field, so the JIT has to do the inlining.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 26 '06 #6
Ok - a better use of Google found the following post from Microsoft. It
appears that the JIT doesn't optimize release builds when run from Visual
Studio. I'm still under the opinion that these sorts of optimization would be
better handled at the compiler instead of relying on the JIT, but oh well. I
suppose the profiler isn't completely worthless, though it now seems
considerably less handy. Thanks for the responses.
James,
The Energy function should be inlined. Were you running your test from
within Visual Studio? If so, be aware that the JIT generates non-optimized
code if you start the application from with VS, even if it is a release
build. I suggest that you use the cordbg debugger and look at the
disassembly. You will have to set the JitOptimization mode on before the
containing method is JITted.

Gregor
CLR Performance PM
Microsoft

"Jaimi McEntire" wrote:
Given a property define like this:

public decimal ControlAmount
{
get { return mControlAmount; }
set { mControlAmount = value; }
}

I expected (under release mode) that when used as a variable in normal code
(ie, nothing that is enumerating properties or requiring metadata) eg:

ControlAmount += MyValue;

That the get accessor would be "optimized out", and replaced with direct
access to the variable (like an "inline function"). This is what would have
happened in C++ or Delphi.

However, I am finding that this is not the case in my code, even in release
mode. I profile my application, and find that the get_ControlAmount function
not only gets called *all the time*, but that it has significant overhead -
much more so than a simple member variable access.

Is there any way to optimize simple properties like this, or ensure that the
optimizer is able to remove the call?

Apr 26 '06 #7

Note that in this case (a Decimal property) the JIT doesn't inline the
property accessor.
My guess is that the JIT chooses to 'not inline' in favor of working set
size (a speed/space tradeoff).

Take a look at the disassembly of the getter:
00000000 57 push edi

00000001 56 push esi

00000002 8B FA mov edi,edx

00000004 83 C1 0C add ecx,0Ch

00000007 3B 09 cmp ecx,dword ptr [ecx]

00000009 8B F1 mov esi,ecx

0000000b F3 0F 7E 06 movq xmm0,mmword ptr [esi]

0000000f 66 0F D6 07 movq mmword ptr [edi],xmm0

00000013 F3 0F 7E 46 08 movq xmm0,mmword ptr [esi+8]

00000018 66 0F D6 47 08 movq mmword ptr [edi+8],xmm0

0000001d 5E pop esi

0000001e 5F pop edi

0000001f C3 ret

and compare it to the code generated for the direct field access.

00000113 8D 7D E4 lea edi,[ebp-1Ch]

00000116 8B 45 D0 mov eax,dword ptr [ebp-30h]

00000119 3A 40 0C cmp al,byte ptr [eax+0Ch]

0000011c 8D 70 0C lea esi,[eax+0Ch]

0000011f F3 0F 7E 06 movq xmm0,mmword ptr [esi]

00000123 66 0F D6 07 movq mmword ptr [edi],xmm0

00000127 F3 0F 7E 46 08 movq xmm0,mmword ptr [esi+8]

0000012c 66 0F D6 47 08 movq mmword ptr [edi+8],xmm0

The first has 13 instructions, the second 8. That means that the latter must
be something like 35% faster, but at the expense of 30 bytes injected at
each call site.

Willy.


"Jaimi McEntire" <Ja***********@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
| Ok - a better use of Google found the following post from Microsoft. It
| appears that the JIT doesn't optimize release builds when run from Visual
| Studio. I'm still under the opinion that these sorts of optimization would
be
| better handled at the compiler instead of relying on the JIT, but oh well.
I
| suppose the profiler isn't completely worthless, though it now seems
| considerably less handy. Thanks for the responses.
|
| >James,
| >The Energy function should be inlined. Were you running your test from
| >within Visual Studio? If so, be aware that the JIT generates
non-optimized
| >code if you start the application from with VS, even if it is a release
| >build. I suggest that you use the cordbg debugger and look at the
| >disassembly. You will have to set the JitOptimization mode on before the
| >containing method is JITted.
| >
| >Gregor
| >CLR Performance PM
| >Microsoft
|
|
| "Jaimi McEntire" wrote:
|
| > Given a property define like this:
| >
| > public decimal ControlAmount
| > {
| > get { return mControlAmount; }
| > set { mControlAmount = value; }
| > }
| >
| > I expected (under release mode) that when used as a variable in normal
code
| > (ie, nothing that is enumerating properties or requiring metadata) eg:
| >
| > ControlAmount += MyValue;
| >
| > That the get accessor would be "optimized out", and replaced with direct
| > access to the variable (like an "inline function"). This is what would
have
| > happened in C++ or Delphi.
| >
| > However, I am finding that this is not the case in my code, even in
release
| > mode. I profile my application, and find that the get_ControlAmount
function
| > not only gets called *all the time*, but that it has significant
overhead -
| > much more so than a simple member variable access.
| >
| > Is there any way to optimize simple properties like this, or ensure that
the
| > optimizer is able to remove the call?
| >
| >
Apr 26 '06 #8

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

Similar topics

2
by: cody | last post by:
Does the clr allow more than one set and one get method for a property? Is it possible to use overloading for example set_Color(int c), set_Color(Color c)? from msdn: ...
10
by: crjunk | last post by:
I have a script that I want to run only when my input box IS NOT disabled. Can someone tell me if something is wrong with my script? Is "disabled" the correct property to use? function...
13
by: Peter Kirk | last post by:
Hi there, can someone tell me what exactly a "property" is in a C# class? As far as I can see it is "two methods" - ie a getter and a setter for an instance variable. What is the difference...
11
by: wASP | last post by:
Hi, I've got a pair of int properties in a class. The properties in question are indexing values - but that's not relevant to my problem - or it's just symptomatic ... sort of. They are...
62
by: djake | last post by:
Someone can explain me why to use property get and property set to access a value in a class, insted of access the value directly? What's the usefulness of property statements in VB.NET? Thanks
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
2
by: Ben Voigt | last post by:
The C# Language Specification says: A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write...
19
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string?...
2
by: Tony Johansson | last post by:
Hello! I just wonder what this mean it is written in a book called "Visual C# 2005" " The accessibilities that are permitted for accessors depends on the accessibility of the property, and it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.