6 ms·
The Java JIT has static type information to work with, the JS JIT can only infer type information via heroic efforts. Static types do mean something, especially
by seanmcdirmid 7y ago
The Java JIT has static type information to work with, the JS JIT can only infer type information via heroic efforts. Static types do mean something, especially when working with primitives and other unboxed data types.
- erik_seaberg 7y agoThis. Javascript engines have to do stuff like https://mathiasbynens.be/notes/shapes-ics https://mathiasbynens.be/notes/shapes-ics to assume many objects will have the same key names and types and create an optimized class layout, with some kind of expensive fallback if anyone ever randomly stores a key or value that violates the assumption. They can't even be sure you'll always be doing integer math (unless you resort to bit masking a la asm.js). Hinting (like __slots__ in Python) might have helped some.
- discreteevent 7y agoThis was one of the main motivations for writing dart. The V8 implementers were tired of having to write these kinds of hacks. Even though dart was initially a dynamically typed language, the shape of the objects was stable.
- pizlonator 7y agoYeah what you said.
- raverbashing 7y agoYou're not wrong, but I always wonder how the verbosity and crazy class hierarchy of Java code doesn't make things slower. I know that compilers are smart, but the API doesn't make things easier at all
- bosswipe 7y agoVerbosity makes the programmer slower not the program.
- raverbashing 7y agoYes I understand some verbosity is structural and won't change anything, for example SomeClass x = new SomeClass(); won't make anything slower (ignoring auto for now) Now, I read Java code and I can't help but wonder how things are harder For example, setters and getters, what would be a simple memory write becomes a function call. Not complaining when you actually need it. Reading the examples here (and those are not too bad) https://developer.android.com/reference/java/net/HttpURLConnection https://developer.android.com/reference/java/net/HttpURLConn... it seems you have to actually fight the API to get anything done Why do you need to cast the return of a url.openConnection() to a HttpURLConnection? (I mean, how many connection types exist?)
- pgrulich 7y agoSo for getters and setters a Jit can inline the function call and in the end you have a direct memory access. In your example you have to cast because URLConnection could be a HttpURLConnection or JarURLConnection. But again a good Jit would speculate that it is always a HttpURLConnection and deoptimize if not.
- imtringued 7y agoThe JVM also has full information about the class hierarchy at runtime. If you add a class that overrides a function it will deoptimize that overriden function to a virtual function but otherwise the JVM will just treat it as a static function and inline it if necessary.
- gnosek 7y ago> I mean, how many connection types exist? Two? https://developer.android.com/reference/java/net/URLConnection https://developer.android.com/reference/java/net/URLConnecti...
- vips7L 7y agoIn modern Java I don't see why you would directly use an HttpURLConnection. The stdlib provides an easy to use HttpClient. https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html https://docs.oracle.com/en/java/javase/11/docs/api/java.net....
- pdpi 7y agoThe Java standard library isn't particularly verbose or insane. It's the "enterprise java" world that deals with the sort of excesses you're thinking about.
- pcr910303 7y agoWell, you're right that JS JITs can only infer type information via heroic efforts, but AFAIK the Java compiler throws away any type information from the source code, which means that the JVM JIT needs to inver type information again from the byte code. Still, the JVM JIT is faster than JS due to reasons explained in the sibling comment of the parent one.
- BorisTheBrave 7y agoThis is not true, bytecode stores fields and classes as fully qualified strings: https://stackoverflow.com/a/17406592 https://stackoverflow.com/a/17406592
- pdpi 7y ago> but AFAIK the Java compiler throws away any type information from the source code You're thinking about generics. .class files preserve a whole bunch of type information (I'm building a .class decompiler in my free time, and I'm looking at that very same data in my debugger ATM).
- The_Colonel 7y agoYou probably know this, but it's not obvious to the random reader. Even generic types are available in the Java .class file and are accessible from the reflection API. Spring for example uses this quite heavily.
- cogman10 7y agoIt depends on where you are at. Type information is present in fields and class inheritance For example, a class like this `class Foo implements Bar<String>` Retains the fact that the generic type is a String. That information is completely lost at method invocation. So a method that takes a `Bar<String>` ultimately compiles to a method that takes a `Bar` and knows nothing of the String. To get that generic information down you have to engage in some fun tricks using either the class or field method I mentioned earlier. (Usually you do this with a second type parameter where it matters).
- masklinn 7y ago> The Java JIT has static type information to work with I don't know that the java JIT has type feedback. However regardless Java being statically typed means Java code is structurally closer to what a JIT wants and can analyse, it's much more difficult (and thus rarer) to fuck around with types and objects generated on the fly at runtime for instance, you're not going to add new attributes to an instance whereas that's just tuesday in javascript.
- titzer 7y agoJava JITs definitely do have type feedback. They profile the types of objects at virtual call sites and do guarded inlining, as well as removing expensive interface casts, etc. The JVM doesn't really have a distinction between classes generated on the fly and those that were on disk, as they all go through a classloader anyway. JVMs, for the most part, blow their brains out at the end of the day and start with zero the next VM run.