473,503 Members | 241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting to generics

Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I
am converting them to List<and Dictionary<respectively. It's a
pretty massive system, so there are a lot of casts. For instance.

ArrayList aaa = new ArrayList();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = (Customer) aaa[0];

With generics, it would be like this:

List<Customeraaa = new List<Customer>();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = aaa[0]; //e.g. without the cast

But, given that I simply changed declarations from ArrayList to List<>,
there is still a lot of code that has a cast, even though the cast is
from a strongly typed list:

Customer c = (Customer) aaa[0];
So changing declarations to generics was pretty easy, but the there are
about a zillion casts in the system. So I have 2 questions.

1. Is the compiler smart enough take out the unnecessary casts?
2. Is there a setting in VS2005 that will show me the unnecessary casts
when compiling?

Thanks.
Apr 11 '07 #1
10 1580
Frank,

I would think that the compiler could be smart enough to do this, but to
be sure, why not compile that code and look at the IL? You should be able
to tell pretty quickly if a cast is taking place.

As for finding the unnecessary casts, there is no such setting to do
this for you. You will have to find all the instances of (Customer) and
then remove them manually.

What you could do is a find and replace across all of your projects and
remove (Customer). Then, when you compile the project, you will see where
the compiler tells you that you have a type mismatch (because one is
assuming that you are casting from an interface or something like that).

Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you have a
number of these operations taking place, you will see a performance
enhancement. You will see a little bit of a performance enhancement with
reference types, but not as much.

With reference types you still want to make the push to use generics so
you can get compile-time checking of your type usage (as opposed to finding
errors at runtime because of invalid casts).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I
am converting them to List<and Dictionary<respectively. It's a pretty
massive system, so there are a lot of casts. For instance.

ArrayList aaa = new ArrayList();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = (Customer) aaa[0];

With generics, it would be like this:

List<Customeraaa = new List<Customer>();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = aaa[0]; //e.g. without the cast

But, given that I simply changed declarations from ArrayList to List<>,
there is still a lot of code that has a cast, even though the cast is from
a strongly typed list:

Customer c = (Customer) aaa[0];
So changing declarations to generics was pretty easy, but the there are
about a zillion casts in the system. So I have 2 questions.

1. Is the compiler smart enough take out the unnecessary casts?
2. Is there a setting in VS2005 that will show me the unnecessary casts
when compiling?

Thanks.

Apr 11 '07 #2
"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
1. Is the compiler smart enough take out the unnecessary casts?
To answer this, and other similar questions, you can compile the code
with and without the cast and then use ILDASM.exe to examine the MSIL that
the compiler has generated. This will demonstrate wether the cast has been
optimized away or not.
Apr 11 '07 #3
Alberto Poblacion wrote:
"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
>1. Is the compiler smart enough take out the unnecessary casts?

To answer this, and other similar questions, you can compile the code
with and without the cast and then use ILDASM.exe to examine the MSIL
that the compiler has generated. This will demonstrate wether the cast
has been optimized away or not.
Yep, the compiler is smart enough. I just checked.
Apr 11 '07 #4
Nicholas Paldino [.NET/C# MVP] wrote:
Frank,
Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you have a
number of these operations taking place, you will see a performance
enhancement. You will see a little bit of a performance enhancement with
reference types, but not as much.
No, there are inefficiencies in the ArrayList on many fronts from boxing
issues to memory issues. The comparison in memory usage in the link
below shows more than 4x improvement for using List<tover ArrayList on
the 64-bit system.

http://blogs.msdn.com/joshwil/archiv...13/112598.aspx
Apr 11 '07 #5
What you could do is a find and replace across all of your projects and
remove (Customer).
Just a note: watch out for disambiguations, specifically when the cast
is as a parameter to an overloaded method; i.e. if you have
SomeFunc(object) and SomeFunc(Customer), and you remove a cast it
might start calling the wrong method, which could (as an example) mean
calling itself iteratively.

Not that I still have bitter memories or anything...

Marc

Apr 11 '07 #6
Yes, more memory is required for each of the object references. If it
is a value type, then yes, you are going to have 64 bits for each object
reference for each boxed value type instance. For reference types, you are
going to have 64 bits for each of the object references as well, but thats
an issue with ^every^ object reference that you have in the app.

Now, this is an opinion, and goes a little bit beyond the scope of the
post, but it's my belief that this is something that doesn't matter in the
initial implementation of a design for a system. It's something you have to
be concerned with in the performance evaluation phase. I wouldn't even
worry about this initially (well, mostly because it's a non issue because
I'd be using the Generic versions of the data structures, but if I couldn't
use them, then I still wouldn't worry) because in a 64-bit environment, I am
going to have a virtual address space which is exponentially larger than the
address space on a 32-bit machine. So yes, while object references will
take up 64 bits, which is double the size of references on a 32 bit machine,
I'm not too concerned, because the virtual address space is going to be that
much larger.

Of course, we all know that virtual address space doesn't correspond to
physical memory, but going back to the second sentence of the first
paragraph, I feel that you are typically going to have 64-bit processors in
server environments, and in that case, you are going to have gobs of
physical memory to back up the virtual address space, which leads me to
believe it is a non-issue, at least in the initial implementation of a
design.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:u4*************@TK2MSFTNGP05.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
>Frank,
> Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you
have a number of these operations taking place, you will see a
performance enhancement. You will see a little bit of a performance
enhancement with reference types, but not as much.

No, there are inefficiencies in the ArrayList on many fronts from boxing
issues to memory issues. The comparison in memory usage in the link below
shows more than 4x improvement for using List<tover ArrayList on the
64-bit system.

http://blogs.msdn.com/joshwil/archiv...13/112598.aspx

Apr 12 '07 #7
Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you have
a number of these operations taking place, you will see a performance
enhancement. You will see a little bit of a performance enhancement with
reference types, but not as much.
Eliminating the run-time type check incurred by a downcast is pretty
significant.
Apr 12 '07 #8
On Apr 12, 2:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you have
a number of these operations taking place, you will see a performance
enhancement. You will see a little bit of a performance enhancement with
reference types, but not as much.

Eliminating the run-time type check incurred by a downcast is pretty
significant.
In how many situations? Usually when I fetch an object from a list, I
then want to *do* something with it - and that "something" is likely
to take significantly longer than the cast.

Of course, it depends on exactly how significant something needs to be
in order to be called significant, but for the vast majority of
programs I wouldn't expect to see much improvement in performance due
to just removing the runtime check.

Jon

Apr 12 '07 #9
Nicholas Paldino [.NET/C# MVP] wrote:
Yes, more memory is required for each of the object references. If it
is a value type, then yes, you are going to have 64 bits for each object
reference for each boxed value type instance. For reference types, you are
going to have 64 bits for each of the object references as well, but thats
an issue with ^every^ object reference that you have in the app.
Now, this is an opinion, and goes a little bit beyond the scope of the
post, but it's my belief that this is something that doesn't matter in the
initial implementation of a design for a system. It's something you have to
be concerned with in the performance evaluation phase. I wouldn't even
worry about this initially (well, mostly because it's a non issue because
I'd be using the Generic versions of the data structures, but if I couldn't
use them, then I still wouldn't worry) because in a 64-bit environment, I am
going to have a virtual address space which is exponentially larger than the
address space on a 32-bit machine. So yes, while object references will
take up 64 bits, which is double the size of references on a 32 bit machine,
I'm not too concerned, because the virtual address space is going to be that
much larger.
The problem is not the virtual address space, it's physical memory. My
application chews through a lot of memory. In 32-bit mode it would take
up to 1.5GB. When I recompiled it as is for AnyCPU and ran it on a
64-bit box, the memory usage doubled. That's The app is running on a
box with 6GB of RAM that also has SQL Server on it, so all of a sudden
processes got shifted into the paging file, which slowed everything to a
crawl.
Of course, we all know that virtual address space doesn't correspond to
physical memory, but going back to the second sentence of the first
paragraph, I feel that you are typically going to have 64-bit processors in
server environments, and in that case, you are going to have gobs of
physical memory to back up the virtual address space, which leads me to
believe it is a non-issue, at least in the initial implementation of a
design.
It's not the initial implementation. Things were good in the 32-bit
land (e.g. SQL Server 2000 and my app in 32-bit mode), however, once the
boxes got upgraded to 64-bit (e.g. Win2003 R2 64-bit and SQL Server 2005
64-bit), all of a sudden Sql Server needs more ram to operate properly
and so does my app (at least in 64-bit mode).

So for now, while I rip out ArrayList objects, the app keeps operating
in 32-bit mode on a 64-bit machine.

Regards
Apr 12 '07 #10
ReSharper displays unneeded casts as warnings.

///ark
Apr 12 '07 #11

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

Similar topics

27
2440
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
23
2519
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
2715
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
5947
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
9
2538
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
1
2418
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
5
9555
by: vtjumper | last post by:
I'm building a C# interface to an existing messaging system. The messaging system allows values of several types to be sent/recieved over the interface. What I want to do is use a generic...
5
8968
by: Lint Radley | last post by:
Hello, I am looking for a way to cast a byte to Int16. I get this error: "Unable to cast object of type 'System.Byte' to type 'System.Int16'" If you're curious, I am using a library for...
13
3792
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
7203
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
7089
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
7282
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,...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.