5 ms·
It seems to me that eliminating primitives could be a really bad idea. If you have a class: public class CompoundObject { int i; int j;
by yummyfajitas 15y ago
It seems to me that eliminating primitives could be a really bad idea. If you have a class:
public class CompoundObject {
int i;
int j;
double k;
}
This class's data members are all a few bytes away from each other, so once the object is loaded into the CPU cache, all operations should be fast. With boxed types, you might get 3 cache misses when accessing i,j,k.
On the other hand, referential transparency can help here. Given referential transparency, the compiler can transparently unbox Integers and translate method calls. So if Oracle does it right, rather than slowing down unboxed code, this might just speed up boxed code.
- cynicalkane 15y agoThey don't say how they're implementing it. Given the compiler always knows the actual type of a primitive at compile-time, they can probably implement most of the Object functionality by plugging in the class meta-info at compile-time. When a primitive would need to be boxed, Java programmers are already used to the performance implications of this. The only object functionality I can think of that would be hard to implement on primitives is wait/notify/synchronization, but I don't actually know how those are done in the JVM either. Even if these things turn out to be impossible to optimize at compile time, they're probably then betting on JIT functionality to detect primitive objects that are only used as primitives.
- repsilat 15y agoThe "boxed primitive" types (Integer and so on) should be `final`. This means no inheriting from them, which means no polymorphism, which means their sizes and all operations on them are known statically. This means you can store them as values in the object with no fuss, and you can inline all of their access functions.
- cynicalkane 15y agoJava objects have functionality beyond dispatch tables. Synchronization is one. You can also have x != y where x and y are different objects with the same value.
- Someone 15y agoFor integers, implementations are allowed to have unique instances for every value (and must have unique ones for some small values). A conforming implementation can extend that to 'all integers' (It might break existing programs, but such programs would not be standard conforming). I do not know whether the standard currently allows for code that is guaranteed to produce Double's with equal values that are not object.Equal to each other, but I doubt that making that impossible will introduce many problems.
- tsotha 15y ago>You can also have x != y where x and y are different objects with the same value. May be true, but it's not guaranteed one way or the other. In the Oracle JVM the smaller integers are cached such that new Integer(10) == new Integer(10) but new Integer(1000) != new Integer(1000)
- teraflop 15y agoActually, "new Integer()" is guaranteed to always return a new, distinct object. The caching only comes into play during autoboxing conversions, or when you call Integer.valueOf().
- tsotha 15y agoAh, you're right. I was thinking of autoboxing, i.e. Integer a; Integer b; a = 10; b = 10; a == b; // true a = 1000; b = 1000; a == b; // false
- kingkilr 15y agoThe only issue that comes up is synchronization, because it requires maintaining identity, localized escape analysis alleviates a lot of the burden here, but the fact remains that anytime you escape an Integer you'll need to allocate it, because you might want to synchronize on it, without the synchronization issue you can come up with schemes to avoid allocating even if the object does escape. Every other issue can be handled by a compiler optimization (either AOT, or JIT).
- rkalla 15y agoWas absolutely concerned about this as well, but then say the timeline they are talking about (almost a decade) and decided to keep my concern in check, at least for another 6 years. I either imagine hardware progressing so far in the next decade that this is absolutely moot or the language changing directions and some surprise coming out of left field to change this gameplan.
- rayiner 15y agoYou don't need to use a uniform field representation, and the representation in the object doesn't need to match the representation on the stack/in registers. For example Dylan language doesn't have primitives as such. The d2c compiler passes objects around as a two-element struct containing a type tag and a value (in practice values get passed around in a pair of registers). Now, if you have a field with the declaration <object>, and you store a <double> in there, then the field will consist of two words: a type tag and the double itself. However, if the field is declared as a <double>, then the field will be stored as just the double, and the field accessor code will add the (constant) type tag as the field is read.
- j_baker 15y agoC# accomplishes this with structs: http://weblogs.asp.net/dixin/archive/2007/12/20/understanding-net-primitive-types.aspx http://weblogs.asp.net/dixin/archive/2007/12/20/understandin... Such a thing could be done in Java if Oracle added structs. They're basically just "unboxed objects". I believe there's more to it than that, but that's the basic idea.