Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting performance in Java

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?

like image 920
deamon Avatar asked Dec 01 '22 11:12

deamon


2 Answers

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).

like image 163
Michael Borgwardt Avatar answered Dec 03 '22 01:12

Michael Borgwardt


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.

like image 35
RichN Avatar answered Dec 03 '22 00:12

RichN