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

No overhead for a "get" accessor...

This is probably common knowledge to .Net gurus, but please bear with me
while I share a discovery with the group...

I needed to create a public lock on a class, as follows...

class Locked
{
.....
public object lockObject = new object();
}

Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on it,...

class Locked
{
.....
private object lockObject = new object();
public object LockObject
{
get {return lockObject;}
}
}

However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really is
an overhead, so ran ildasm on both versions of the exe. The dissasembled code
for the client was identical in both cases!

ie.

Locked l = new l;

lock (l.lockObject) {..}; // public member, no accessor

lock (l.LockObject()) {...}; // private member, "get" accessor

both compile to the same code. In other words, the compiler removes the
method call associated with the "get", and does it "in-line" (in C++
parlance).

The lesson... don't avoid accessors because of any perceived performance
issues.

Regards,

Javaman
Nov 17 '05 #1
7 3104

"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
This is probably common knowledge to .Net gurus, but please bear with me
while I share a discovery with the group...

I needed to create a public lock on a class, as follows...

class Locked
{
....
public object lockObject = new object();
}

Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on
it,...

class Locked
{
....
private object lockObject = new object();
public object LockObject
{
get {return lockObject;}
}
}

However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really
is
an overhead, so ran ildasm on both versions of the exe. The dissasembled
code
for the client was identical in both cases!

ie.

Locked l = new l;

lock (l.lockObject) {..}; // public member, no accessor

lock (l.LockObject()) {...}; // private member, "get" accessor

both compile to the same code. In other words, the compiler removes the
method call associated with the "get", and does it "in-line" (in C++
parlance).

The lesson... don't avoid accessors because of any perceived performance
issues.


It is a good lesson. I don't recall the compiler doing this myself, but the
JIT will inline simple properties pretty often if the compiler doesn't as
well. The only caveat is that the property cannot be virtual.
Nov 17 '05 #2
Javaman59 <Ja*******@discussions.microsoft.com> wrote:
This is probably common knowledge to .Net gurus, but please bear with me
while I share a discovery with the group...

I needed to create a public lock on a class, as follows...

class Locked
{
....
public object lockObject = new object();
}

Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on it,...
<snip>
However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really is
an overhead, so ran ildasm on both versions of the exe. The dissasembled code
for the client was identical in both cases!
<snip>
both compile to the same code.


Hang on - what exactly are you talking about? They won't compile to the
same IL, but they *will* be JITted (in release mode, anyway) to the
same assembler code. Is that what you meant?

--
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
Nov 17 '05 #3
Apologies. I was wrong.

I've had another look at the IL code and found that in fact their is a
difference. I was looking at the wrong section of the code before.

The code with the accessor has this line, which I assume is calling the
accessor...

IL_0009: callvirt instance object Locked::get_LockObject()

and the code without the accessor has the following, which I assume is
loading the value of the "lockObject" variable.

IL_0009: ldfld object Locked::lockObject

Sorry to mislead you,

Javaman

"Javaman59" wrote:
This is probably common knowledge to .Net gurus, but please bear with me
while I share a discovery with the group...

I needed to create a public lock on a class, as follows...

class Locked
{
....
public object lockObject = new object();
}

Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on it,...

class Locked
{
....
private object lockObject = new object();
public object LockObject
{
get {return lockObject;}
}
}

However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really is
an overhead, so ran ildasm on both versions of the exe. The dissasembled code
for the client was identical in both cases!

ie.

Locked l = new l;

lock (l.lockObject) {..}; // public member, no accessor

lock (l.LockObject()) {...}; // private member, "get" accessor

both compile to the same code. In other words, the compiler removes the
method call associated with the "get", and does it "in-line" (in C++
parlance).

The lesson... don't avoid accessors because of any perceived performance
issues.

Regards,

Javaman

Nov 17 '05 #4
Thanks Jon,
but they *will* be JITted (in release mode, anyway) to the
same assembler code. Is that what you meant?


No, that's not what I meant, but i was wrong. So, if we get back to the
original purpose of my little investigation, which was to determine whether
there is an overhead for using accessors, can I take it that the answer is
"no", for release mode?

Javaman
Nov 17 '05 #5
Javaman59 <Ja*******@discussions.microsoft.com> wrote:
but they *will* be JITted (in release mode, anyway) to the
same assembler code. Is that what you meant?


No, that's not what I meant, but i was wrong. So, if we get back to the
original purpose of my little investigation, which was to determine whether
there is an overhead for using accessors, can I take it that the answer is
"no", for release mode?


Indeed. You can easily check with cordbg though. Basically, the JIT is
free to inline method calls (and a property is basically a method or
two) below a certain size, and which don't do certain things.

--
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
Nov 17 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Javaman59 <Ja*******@discussions.microsoft.com> wrote:
> but they *will* be JITted (in release mode, anyway) to the
> same assembler code. Is that what you meant?


No, that's not what I meant, but i was wrong. So, if we get back to the
original purpose of my little investigation, which was to determine
whether
there is an overhead for using accessors, can I take it that the answer
is
"no", for release mode?


Indeed. You can easily check with cordbg though. Basically, the JIT is
free to inline method calls (and a property is basically a method or
two) below a certain size, and which don't do certain things.

--
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


It also won't be inlined if the class derives from MarshalByRefObject

Regards

Richard Blewett -DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #7

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:O7**************@TK2MSFTNGP14.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Javaman59 <Ja*******@discussions.microsoft.com> wrote:
> but they *will* be JITted (in release mode, anyway) to the
> same assembler code. Is that what you meant?

No, that's not what I meant, but i was wrong. So, if we get back to the
original purpose of my little investigation, which was to determine
whether
there is an overhead for using accessors, can I take it that the answer
is
"no", for release mode?


Indeed. You can easily check with cordbg though. Basically, the JIT is
free to inline method calls (and a property is basically a method or
two) below a certain size, and which don't do certain things.

--
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


It also won't be inlined if the class derives from MarshalByRefObject

Regards

Richard Blewett -DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


And if it contains exception blocks (such as locked code blocks).
Willy.
Nov 17 '05 #8

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

Similar topics

1
by: Pete Mahoney | last post by:
Ok I use a textarea to store data input by the user, and then upon them clicking the submit button I store this data to a database. The problem is once the user inputs too much data (about 3...
12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
6
by: Eitan | last post by:
Hello, I would like to know about the methods : post and get of ASP. What is the difference between them. Need some samples, and explanation, please. Thanks :)
4
by: Bill Borg | last post by:
Hello, I've got a simple shared property, e.g. Public Class dbObject Private Const m_ID As String = "ID" Public Shared ReadOnly Property ID() As String Get Return m_ID End Get End Property
5
by: Jeff | last post by:
Visual Studio 2003 DotNet framework 1.1 Windows 2000 Pro I create two pages in an Asp.net application, one is html page with a form in it: .... <form id="testForm" method="post"...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
7
by: vvkl | last post by:
I have readed a example code from MSDN about FormsAuthenticationTicket calss, but there's a line I can't understand : 'strRedirect = Request;' What's the mean in which square brackets? Thank...
3
by: Bsr | last post by:
What is the difference between for the following methods. "GET", "HEAD", "PUT" or "POST". Ex:my $req =HTTP::Request->new(GET =>$url1); Bhuvan.
3
by: ElBeardo | last post by:
Hello, I´m new to Python.. so this is a newbee question. I´d like to put the value enterd in the entryfield in a variable. I´m trying to build a calculator with python and TKinter, coding it in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.