I'm running some simple, rudimentary benchmarking just to see how Generics impact performance. I'm certainly not a "generics" programmer so this is all based on my own experimentation...nothing concrete.
It would appear, in my case anyhow, that generics can definitely impact performance in a negitive way. If this is obvious to folks that are more familiar w/ them...then I apologize for stating the obvious.
I'm building my own blog application using J2EE + db4o (object database) and I wanted to use a generic method to convert the "ObjectSet" collection that db4o returns when querying the database into an ArrayList collection of my own object datatype...so as to be able to "bind" to a JSTL loop statement in a JSP.
Here's a couple of snippets to illustrate. I realize readers may not be familiar w/ db4o but these examples are so simple, any programmer could follow it.
db4o will return an ObjectSet when querying objects; Here is me querying the default "Blog" class for its entries (Entry class):
If I iterate through this ObjectSet collection reading one of the fields...it takes about 239 milliseconds with roughly 50,000 records.
Now, I created this generic method to pass in the Object set and create a List<IEntry> collection so I could have a collection that is usable in JSTL (can't remember which interface makes this possible, I'm new to Java!)
I use this method to convert the ObjectSet to a List like so:
Executing this in a loop the same as the aforementioned, ObjectSet-based collection takes about 750ms - more than double the amount of time.
I don't think this has anything to do with generics. If ObjectSetToList weren't using generics, it would likely have the same impact on performance.
Is ObjectSet basically working like an Iterator? If so, you could write an Adapter that simply wraps the ObjectSet, instead of copying its content.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Looks to me like your collection is backed by some data store which is impacting performance for whatever reason(s), since generics certainly won't - you can discover this yourself using a hex viewer, the VM Specification and a cup of coffee.
Generics have the potential to increase performance going forward however the current implementation has zero impact, positive or negative. Generics do not have a runtime impact at all. When the compiler is done with generic elements it throws them away. They have no manifestation in the class file, almost.
If erasure changes a signature inappropriately (substitues T when Object is a requirement) a bridge method is added. That would impact performance on that method in this case somewhere in the teeny to weeny range:
Tony, The ObjectSet itself is a simple iterator and in-and-of itself isn't slow at all, it's actually quite fast when compared to fetching records from a relational database.
I found a class bundled w/ the source code of db4o called ObjectSetCollection which extends AbstractList (though not generic out of the box) - Using this I'm actually getting better performance than the generic conversion function I wrote.