Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting optimization

 
paul wheaton
Trailboss
Pie
Posts: 21384
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack,
In your article at http://java.oreilly.com/news/javaperf_0900.html you talk about the great time savings of reducing casts of Object to String. What about casting an int to, say, a char?
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoiding any cast is a plus. Object type casts are basically a VM version of runnning 'instanceof' on the object. Primitive data type casts are data conversions. Because the primitive type cast consists of the VM mapping a bunch of bits of one size & format to a bunch of bits of another size & format, it is a pretty fast data conversion. But nevertheless, if you can manipulate code to avoid a cast completely, then that is one less operation the code needs to execute. HotSpot tries to eliminate casts wherever it can, even in some cases by speculatively assuming a data type and not bothering with the cast. If the assumption turns out to be wrong, then HotSpot actually has to deoptimize the code to get back to the slower, safer version.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic