Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics speed

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)

Example:



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.

Any advice?

Thanks!

-v
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Langer on Type Erasure:

http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20the%20type%20erasure%20of%20a%20parameterized%20method?

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:

http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20a%20bridge%20method?
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very informative, thanks guys!

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic