tag:blogger.com,1999:blog-2061058624088955252.post1469233861327916429..comments2022-03-26T05:14:03.759-07:00Comments on Corey Sunwold: Polymorphism and Complex ConditionalsCorey Sunwoldhttp://www.blogger.com/profile/03034494611188304767noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2061058624088955252.post-24424495770672015142010-02-14T09:32:46.926-08:002010-02-14T09:32:46.926-08:00I've never quite wrapped my mind around the id...I've never quite wrapped my mind around the idea that taking conditional logic off one page and spraying it around on five or six other pages is easier to maintain.Bob Fosterhttps://www.blogger.com/profile/03976384333510572229noreply@blogger.comtag:blogger.com,1999:blog-2061058624088955252.post-75789447548517548992010-02-12T13:13:37.062-08:002010-02-12T13:13:37.062-08:00While MacIver makes an interesting observations, a...While MacIver makes an interesting observations, advances in JITs could lead to even the small branch count case to be faster, especially if the VM is using runtime profiling to optimize common code paths at runtime.Anonymoushttps://www.blogger.com/profile/01960822887826165278noreply@blogger.comtag:blogger.com,1999:blog-2061058624088955252.post-20622065731541523652010-02-12T10:25:44.529-08:002010-02-12T10:25:44.529-08:00I second what David R. MacIver says about performa...I second what David R. MacIver says about performance. However, a system should be initially implemented using polymorphism. Choosing how to implement the polymorphism control flow is an optimization. One that the compiler or runtime might be able to make for you :-)<br /><br />I wonder if such an optimization pass can be coded into a Monad instance?Corey O'Connorhttps://www.blogger.com/profile/05514322369153813168noreply@blogger.comtag:blogger.com,1999:blog-2061058624088955252.post-54571865496368749472010-02-12T00:30:32.058-08:002010-02-12T00:30:32.058-08:00I did some benchmarking of this on the JVM a while...I did some benchmarking of this on the JVM a while ago, comparing the cost of virtual methods to a sequence of if(x instanceof Foo) calls (the context was implementation of pattern matching in Scala. Either way a compiler would be generating it, so maintainability is a non-issue). The conclusion seemed to be:<br /><br />* For one or two branches, the compiler turned the virtual method call into the sequence of tests anyway so they were identical.<br />* For a small number > 2 branches the sequence of conditionals was actually faster.<br />* For a large number (I think it kicked in at around 8 but this is going to be machine specific) the virtual method call is faster.<br /><br />This isn't a terribly surprising result. The performance of the conditional is linear in the number of branches, whileas the performance of the virtual call is constant but has higher constant factors due to cache and pipeline behaviour (it's an indirect jump).David R. MacIverhttps://www.blogger.com/profile/17522579015536144620noreply@blogger.com