PC Review


Reply
Thread Tools Rate Thread

code more efficient for generic containers when they contain valuestype

 
 
puzzlecracker
Guest
Posts: n/a
 
      18th Oct 2008
Allegedly, they are more efficient than their non-generic counterparts
because they avoid boxing.

How is it possible to avoid boxing whenever generic code is used?
Wouldn't autoboxing still require whenever open type is expended and
used for value types as in the example below:

List<int> list=new List<int>();

list.Add(5); //Wouldn't this auto-box into Integer?

Thanks
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 3:14 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 18 Oct 2008 11:59:50 -0700, puzzlecracker <ironsel2...@gmail.com>
> wrote:
>
> > Allegedly, they are more efficient than their non-generic counterparts
> > because they avoid boxing.

>
> Not allegedly. They are.
>
> > How is it possible to avoid boxing whenever generic code is used?
> > Wouldn't autoboxing still require whenever open type is expended and
> > used for value types as in the example below:

>
> > List<int> list=new List<int>();

>
> > list.Add(5); //Wouldn't this auto-box into Integer?

>
> No. For value types, a whole new instance of the type is created for each
> different type used as a type parameter for the class. So, when you use
> List<int>, you get a version of List<T> that uses unboxed ints inside.
>
> Using the Add() method as an example, here's a simplified version of
> what's going on. The class might look something like this:
>
> class List<T>
> {
> // Default storage length is 4 elements
> private T[] _rgt = new T[4];
> private _ctCur;
> private _ctMax = 4;
>
> public void Add(T t)
> {
> if (_ctCur == _ctMax)
> {
> // reallocate and copy to make room (code not shown here)
> }
>
> _rgt[_ctCur++] = t;
> }
> }
>
> When you use the class as List<int>, a new type is created where "T" is
> replaced by "int" everywhere. So the class winds up being like this:
>
> class List<int>
> {
> // Default storage length is 4 elements
> private int[] _rgt = new int[4];
> private _ctCur;
> private _ctMax = 4;
>
> public void Add(int t)
> {
> if (_ctCur == _ctMax)
> {
> // reallocate and copy to make room (code not shown here)
> }
>
> _rgt[_ctCur++] = t;
> }
> }
>
> And you'll note that in the above, there's no need to box the parameter to
> the Add() method when it's called. It simply is passed and used as any
> other value type might be.
>
> Note that the reified generic type is handled different for value types
> and reference types. For a reference type, there's no need to create a
> whole new reified version of the generic type for each different reference
> type used; the same code can be reused, and internally it just deals with
> the references. But in order to avoid boxing of value types, a new type
> has to be built for each different value type that is used as a type
> parameter for the generic type. If you had a List<int> and a List<double>
> in your program, that would result in two completely different reified
> types being created from the generic type.
>
> Pete


Good explanation...
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      20th Oct 2008
Peter Duniho wrote:
> On Sat, 18 Oct 2008 11:59:50 -0700, puzzlecracker
> <(E-Mail Removed)> wrote:
>
>> How is it possible to avoid boxing whenever generic code is used?
>> Wouldn't autoboxing still require whenever open type is expended and
>> used for value types as in the example below:
>>
>> List<int> list=new List<int>();
>>
>> list.Add(5); //Wouldn't this auto-box into Integer?

>
> No. For value types, a whole new instance of the type is created for
> each different type used as a type parameter for the class. So, when
> you use List<int>, you get a version of List<T> that uses unboxed
> ints inside.

[snip]
>
> Note that the reified generic type is handled different for value
> types and reference types. For a reference type, there's no need to
> create a whole new reified version of the generic type for each
> different reference type used; the same code can be reused, and
> internally it just deals with the references. But in order to avoid
> boxing of value types, a new type has to be built for each different
> value type that is used as a type parameter for the generic type. If
> you had a List<int> and a List<double> in your program, that would
> result in two completely different reified types being created from
> the generic type.


IOW, .NET generics with value type parameters don't use erasure. Since Java
does use erasure for all generics, .NET performance is considerably better
in this area.

I'm comparing with Java because of the words "open type" and "Integer" (with
a capital I). .NET doesn't have a type called Integer, it's System.Int32.
OTOH in Java it would be java.lang.Integer

>
> Pete



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# generic containers from a "C++ perspective" Giovanni Dicanio Microsoft C# .NET 33 28th Mar 2008 05:36 PM
efficient code yaniv d Microsoft Access Forms 0 31st May 2006 10:00 PM
forward declarations and generic containers omellet@gmail.com Microsoft VC .NET 2 17th Jan 2006 02:51 PM
This code is efficient =?Utf-8?B?ZnJhbg==?= Microsoft C# .NET 13 20th Dec 2005 02:54 PM
Re: More efficient code Rob Bovey Microsoft Excel Programming 1 9th Jul 2003 04:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:14 AM.