473,799 Members | 3,209 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<resp ectively. 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<Customeraa a = 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 1628
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.co m

"Frank Rizzo" <no**@none.comw rote in message
news:uj******** ******@TK2MSFTN GP04.phx.gbl...
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I
am converting them to List<and Dictionary<resp ectively. 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<Customeraa a = 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.comw rote in message
news:uj******** ******@TK2MSFTN GP04.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.comw rote in message
news:uj******** ******@TK2MSFTN GP04.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(Custom er), 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.co m

"Frank Rizzo" <no**@none.comw rote in message
news:u4******** *****@TK2MSFTNG P05.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.no spamwrote:
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

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

Similar topics

27
2464
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 still no can do... Any info would be great!
23
2557
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 vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
12
2747
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 it defines the language more completely. Great to use.
9
5987
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 Experience Only, Please. ...
9
2579
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) example: In Project RoObjDefs: RoPerson.cls file: Public Property Get FirstName() as String Public Property Get LastName() as String <end of file RoPerson.cls> RoPersons.cls file Public Function Count() as Integer
1
2439
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 "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
5
9634
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 class to produce values in the system. For instance I could create class MsgGenericValue<UInt16>() which would represent an unsigned value on the interface.
5
8984
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 accessing some specialized image types. It seems I am returned an array of object's (declared as
13
3838
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4141
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 we have to send another system

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.