Re:Java 8: New features in ConcurrentHashMap
by ygra on Thursday January 19, 2017 @05:20PM
(#13439993)
I'd say C# makes methods not virtual by default because it's a saner default. When writing a class you shouldn't make everything virtual, that should be a conscious decision to actually design a class for extensibility.
As an example of what they can do, take de-virtualisation. Virtual method calls are expensive. Java method calls are virtual by default. The JVM profiles method calls and analyses the class hierarchy to discover which ones can be de-virtualised. That's a big win. C# requires programmers to manually specify which methods are virtual because .NET doesn't use profile-guided JIT compilation. In some cases (admittedly I've only seen artificial examples) this optimisation is so powerful you can write Java programs that completely trash C++ programs, e.g. a program that uses a command line switch to pick a subclass of a virtual base class and then runs method calls on that object in a tight loop. Java will devirtualise the call based on the observation that only one target is ever used, then inline it, then do loop opts on the inlined version.