Reputation: 92397
I've a Servlet filter which performs the following type cast:
HttpServletRequest httpRequest = (HttpServletRequest) req;
At present my filter is responsible for two tasks that would be better separated into two filters. If I'd split the logic into two filters I'd need two casts.
What is the performance impact of such a cast? Is it worth to accept this performance degradation for a better architecture?
Upvotes: 4
Views: 3125
Reputation: 6231
Performance impact will be negligible (compared to the total work done). Java does a lot of casting, like in the collection framework, hence the engineers already optimized it well. An additional casting won't change much, besides, readability (maintainability?) is more important.
Upvotes: 5
Reputation: 55524
The cast has almost no performance impact. (Edited, thanks to commentors)
As long as you have no real performance problem, always go for better architecture.
Upvotes: 3
Reputation: 346260
What is the performance impact of such a cast?
As compared to handling a HTTP request? Absolutely none. If you were doing it in a deeply nested loop that does little else it might matter, but not when it's done once for a task that involves things that are literally millions of times more work (like doing DB requests or accessing the harddisk).
Upvotes: 12