News for hackers. Stuff that matters.
Re:JITs are un-ergonomic by pcr910303 on Sunday March 29, 2020 @03:55AM (#22717193)
Well, 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.

Re:JITs are un-ergonomic by imtringued on Sunday March 29, 2020 @09:23AM (#22718783)
Java is only throwing generic type information away in the declaration of classes and methods. The code that is accessing generic types is compiled with casts to the expected type. List<String>.get(1) will generate a cast to String therefore nothing is actually missing in the generated code. It is only missing when you use reflection to e.g. deserialize a List via Jackson by passing List<String>.class. That unfortunately won't work because the generic type parameter is not part of the declaration, only the generated code.
Re:JITs are un-ergonomic by pdpi on Sunday March 29, 2020 @05:03AM (#22717429)
> 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).

Re:JITs are un-ergonomic by The_Colonel on Sunday March 29, 2020 @08:56AM (#22718605)
You 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.

Re:JITs are un-ergonomic by cogman10 on Sunday March 29, 2020 @09:15AM (#22718720)
It 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).

Re:JITs are un-ergonomic by pizlonator on Sunday March 29, 2020 @09:27AM (#22718819)
Java JITs don’t throw away type information. The bytecode carries strong types, though you have to do some work to get them out (some abstract interpretation). But that’s something you can do statically; no need for profiling or speculation.

In JS you can only get the types by profiling.

Re:JITs are un-ergonomic by titzer on Sunday March 29, 2020 @09:43AM (#22718933)
Generics are erased in Java. Not so in the CLR.
Re:JITs are un-ergonomic by pizlonator on Sunday March 29, 2020 @10:23AM (#22719203)
Yeah but the thread is about types, not generic types specifically.

Also it’s not really true that genetics are erased. There’s that horrible thing javac does so that the VM can support reflection for generics. I get your point though.

Re:JITs are un-ergonomic by titzer on Sunday March 29, 2020 @10:34PM (#22724703)
Do you mean the passing of classes as additional arguments? I haven't seen the horrible thing.

Overall I think C# got this one right. Generics are right there in the binaries.

bUt ActuALLy, I think the rightest right thing is to do specialization up to representation at link time (or compile time), when the whole program is available, ala MLton. Virgil does this. Of course this is not possible in a dynamic code loading environment but I only have so many fucks to give in this life.

Re:JITs are un-ergonomic by pizlonator on Monday March 30, 2020 @12:13AM (#22725182)
No, I meant that the Class format has metadata about what the genetics were so some java.lang.reflect thing can query about what the genetic type was. I don’t think it comes with soundness guarantees.

I agree C# got this right.

I agree that doing specialization up to link time is ideal from a certain standpoint.

Re:JITs are un-ergonomic by username90 on Sunday March 29, 2020 @10:54AM (#22719472)
All generics in Java are of type Object from the start, you can't call any methods on them not implemented by Object. So it is wrong to say they are erased, they were never there to start with.
Re:JITs are un-ergonomic by titzer on Sunday March 29, 2020 @04:46PM (#22722570)
You can add a constraint to a generic declaration, which is an upper (or lower) bound on the allowable type arguments.

e.g.

  class A<Y extends X> {
    // in this scope, Y is known to be of at least type X,
    // so, we can call methods on expressions of type Y
    // that belong to type X (and not Object)
    Y m() { ... }
  }
Type arguments are omitted from usage sites. The technique is literally called erasure in papers and documentation.

  a = new A<Foo>();
  f = a.m(); // should return a Foo, in bytecode returns Y
             // and compiler inserts a cast from Y -> Foo
Generic code is slower in Java because of these extra casts. To get back (most of) the performance, the JVM has to inline enough methods to be able to track the types from start to finish. It can't always.
Re:JITs are un-ergonomic by vbezhenar on Sunday March 29, 2020 @11:44AM (#22719883)
And that's why Java is slower than it could be. If I'm using String[] array, it can't contain anything but String objects, so JVM does not have to check type everytime I'm accessing that array. That's not true for ArrayList<String>, where compiler must check returned object type from `get(index)` (because it really is Object and can contain anything), but it could be true with better language design.
Re:JITs are un-ergonomic by BorisTheBrave on Sunday March 29, 2020 @04:17AM (#22717263)
This is not true, bytecode stores fields and classes as fully qualified strings: https://stackoverflow.com/a/17406592