In this post, I'd like to touch upon perceived performance overheads. The pragmatic programmer (section 26) has this to say about wrapper (forwarding) methods introduced to break method chains:
These wrapper methods will impose both a runtime cost and a space overhead...Note that the number of method calls is the same whether one uses forwarding or not. To use the example in the book:
aSelection.getRecorder().getLocation().getTimeZone(); //3 method calls
when wrapped gives...
aSelection.getTimeZone(); // still 3 calls.
- aSelection.getTimeZone
- recorder.getTimeZone() in Selection and
- location.getTimeZone() in Recorder
Extensive use of forwarding methods may also have a noticeable effect on stack usage patterns in multithreaded environments (like a web server). Without forwarding, the stack goes wind-unwind-wind-unwind-wind-unwind as each method returns before you make the next call. With forwarding, the stack goes wind-wind-wind-unwind-unwind-unwind. This may push up the peak memory usage per thread.
However, for all practical purposes all of the above effects are minor compared to the not-so-minor gains in encapsulation.
2 comments:
I think it is important to realize that method calls are also inlined by modern compilers quite a bit. In practice, you don't really need to worry about the overhead of method calls.
@Robin
Polymorphic calls cannot be inlined. For all practical purposes though, I agree that one needn't worry about the overhead of method calls.
Post a Comment
Note: Only a member of this blog may post a comment.