Passing an object reference 1000 times

D

Dennis Myrén

I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects, each
requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i can
use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of this
objects,
is i could check what type each one is, cast to that type to be able to
access a specific property
of it, and perform what otherwise that method Render does, suppressing that
method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are reference
types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
N

Nicholas Paldino [.NET/C# MVP]

Dennis,

I think that the assumption you are making is that when you are passing
a reference type around, you are copying it as you make the call passing the
object. This is not the case. You are passing a reference, which is very
small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code IN
the method, and not how you are passing the parameters. If you were to take
some performance numbers between passing it with and without the ref
keyword, I think you would find they are pretty much exactly the same.

Hope this helps.
 
D

Dennis Myrén

Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

I think that the assumption you are making is that when you are passing
a reference type around, you are copying it as you make the call passing
the object. This is not the case. You are passing a reference, which is
very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code IN
the method, and not how you are passing the parameters. If you were to
take some performance numbers between passing it with and without the ref
keyword, I think you would find they are pretty much exactly the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dennis Myrén said:
I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able to
access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are reference
types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
N

Nicholas Paldino [.NET/C# MVP]

Dennis,

Like I said before, I don't think that this is the issue. I think that
if you ran some performance tests between the two, you wouldn't find any
difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dennis Myrén said:
Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the call
passing the object. This is not the case. You are passing a reference,
which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code IN
the method, and not how you are passing the parameters. If you were to
take some performance numbers between passing it with and without the ref
keyword, I think you would find they are pretty much exactly the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dennis Myrén said:
I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able to
access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
D

Dennis Myrén

OK, maybe i should stick with the current signature.
It is just that, i think, if i can avoid creating 1000 32-bit integers, and
still provide the same functionality, it feels like i should.

Thank you once again, Nicholas.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Like I said before, I don't think that this is the issue. I think that
if you ran some performance tests between the two, you wouldn't find any
difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dennis Myrén said:
Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the call
passing the object. This is not the case. You are passing a reference,
which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code
IN the method, and not how you are passing the parameters. If you were
to take some performance numbers between passing it with and without the
ref keyword, I think you would find they are pretty much exactly the
same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able to
access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
N

Nicholas Paldino [.NET/C# MVP]

Dennis,

Well, think of it like this, SOMETHING has to be passed, right?
Assuming a reference and a reference to a reference are passed the same way,
it doesn't matter what you are doing, because you are always going to pass
something. If the weight for passing something by ref was different than by
val for reference types, then I would say look into it, but it isn't =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dennis Myrén said:
OK, maybe i should stick with the current signature.
It is just that, i think, if i can avoid creating 1000 32-bit integers,
and still provide the same functionality, it feels like i should.

Thank you once again, Nicholas.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Like I said before, I don't think that this is the issue. I think
that if you ran some performance tests between the two, you wouldn't find
any difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dennis Myrén said:
Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
in message Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the
call passing the object. This is not the case. You are passing a
reference, which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code
IN the method, and not how you are passing the parameters. If you were
to take some performance numbers between passing it with and without
the ref keyword, I think you would find they are pretty much exactly
the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have these tiny classes, implementing an interface through which
their method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able
to access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
D

Dennis Myrén

Thank you, Nicholas.

I think that you have now convienced me to stick with my current signature.

I am wishing you a happy weekend.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Well, think of it like this, SOMETHING has to be passed, right?
Assuming a reference and a reference to a reference are passed the same
way, it doesn't matter what you are doing, because you are always going to
pass something. If the weight for passing something by ref was different
than by val for reference types, then I would say look into it, but it
isn't =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dennis Myrén said:
OK, maybe i should stick with the current signature.
It is just that, i think, if i can avoid creating 1000 32-bit integers,
and still provide the same functionality, it feels like i should.

Thank you once again, Nicholas.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Like I said before, I don't think that this is the issue. I think
that if you ran some performance tests between the two, you wouldn't
find any difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the
call passing the object. This is not the case. You are passing a
reference, which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code
IN the method, and not how you are passing the parameters. If you
were to take some performance numbers between passing it with and
without the ref keyword, I think you would find they are pretty much
exactly the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have these tiny classes, implementing an interface through which
their method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such
objects, each requiring a call to that method to fulfill their
purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by
reference, although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when
i can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able
to access a specific property
of it, and perform what otherwise that method Render does,
suppressing that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
G

Guest

just think of it this way, if copying a 32 bit integer is truely a issue,
then microsoft would have implemented the whole thing very differently.
every time you call something in the BCL, you have to pass things around. we
better keep our fingers crossed that it's not a bottleneck. :)

Dennis Myrén said:
OK, maybe i should stick with the current signature.
It is just that, i think, if i can avoid creating 1000 32-bit integers, and
still provide the same functionality, it feels like i should.

Thank you once again, Nicholas.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Like I said before, I don't think that this is the issue. I think that
if you ran some performance tests between the two, you wouldn't find any
difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dennis Myrén said:
Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
in message Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the call
passing the object. This is not the case. You are passing a reference,
which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code
IN the method, and not how you are passing the parameters. If you were
to take some performance numbers between passing it with and without the
ref keyword, I think you would find they are pretty much exactly the
same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able to
access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 
D

Dennis Myrén

I now created a benchmark on the two approaches,
and i actually see the approach using instance passed by reference twice as
fast.

But both approaches is still extremely fast, so there is no significant
difference.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
OK, maybe i should stick with the current signature.
It is just that, i think, if i can avoid creating 1000 32-bit integers,
and still provide the same functionality, it feels like i should.

Thank you once again, Nicholas.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nicholas Paldino said:
Dennis,

Like I said before, I don't think that this is the issue. I think
that if you ran some performance tests between the two, you wouldn't find
any difference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dennis Myrén said:
Thank you for answering, Nicholas.

Actually, i do know i am passing a *reference* to the object; a 32-bit
integer.
However, it still a lot of 32-bit integers.
So, you think i should go for passing by value rather than by
reference(ref)?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
in message Dennis,

I think that the assumption you are making is that when you are
passing a reference type around, you are copying it as you make the
call passing the object. This is not the case. You are passing a
reference, which is very small compared to the actual object size.

If you are feeling slowdown, then it is most likely because of code
IN the method, and not how you are passing the parameters. If you were
to take some performance numbers between passing it with and without
the ref keyword, I think you would find they are pretty much exactly
the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have these tiny classes, implementing an interface through which
their method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,
each requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.

Let us say i changed the signature of the interface method to:
Render ( ref CosWriter writer ) ;
to pass an existing reference to a CosWriter instance(CosWriter is a
class).

Because the ref keyword is thought to be used to actually change that
parameter in the method to which it was passed,
would it be considered bad practice, passing an instance by reference,
although i will not change that parameter in the method?

I am not very keen on passing this instance by value that much, when i
can use the same reference.
I would earn a lot in both memory allocation and speed, would i not?

The only option i have to the Render method implementation on each of
this objects,
is i could check what type each one is, cast to that type to be able
to access a specific property
of it, and perform what otherwise that method Render does, suppressing
that method.
Could that be more efficient? I doubt.

Those properties is mostly System.Int32, in some cases they are
reference types.
So i would not earn anything from that, would i?


Please give me some guidelines, and i am very grateful.
 

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