Proposal: Large arrays (original) (raw)

Reinier Zwitserloot reinier at zwitserloot.com
Tue Mar 24 09:53:22 PDT 2009


This change has serious impact on the JVM, and the language support
for java is clearly a patchy hack: The 'best' solution (however
unattainable that may be) is to just ensure that arrays can be long- indexed, and forego the double bracket notation. You also forgot to
mention you need rather a lot of work in the reflection API, because
you're introducing a new magic type here (there are the primitives,
arrays, and objects. You're adding a new one: large arrays).

I'm opposed to any solution where a clearly superior one that is
similar is obvious, and not clearly impossible to implement (even if
it will be implemented in java8 or beyond). If your proposal was
somehow compatible with such a future, that'd be okay, I guess, but it
isn't: If this double-bracket feature is added, then the extra type
will never go away.

If you're willing to eat up 20 of the precious opcode space, you can
instead propose something that is much simpler for all involved:

Any attempt to use arrays with a long instead of an int will work
fine, it'll just use a different opcodes. Internally, there are such
things as large arrays, but you can index into them using an int, and
you can index into a plain array with a long (with the usual result if
your long has a non-zero upper int: AIOOBException). For most other
things including reflection, there is no discernable difference
between large and non-large arrays. You can't grow arrays, so I don't
really understand why you'd want a distinction in the first place.
That's just implementation detail leaking through.

--Reinier Zwitserloot

On Mar 24, 2009, at 17:35, james lowden wrote:

Proposal: Large arrays AUTHOR(S): J. Lowden VERSION: 1.0 Initial version.

OVERVIEW FEATURE SUMMARY: Java arrays are accessed via 32-bit ints, resulting in a maximum theoretical array size of 2147483647 elements. While this is enough for most uses, some applications that need to handle large sequential data sets in memory would benefit from the ability to work with arrays indexed using 64-bit indices. As "simply" changing the current array syntax to use longs as indices rather than ints would have the potential to break a large amount of existing code, I'm not proposing doing any such thing. Instead, I'd like to see a separate but parallel array feature for creating and accessing both types of arrays. A rather boring example, which simply fills a byte array with hexadecimal "FF" values, is shown below: //int-indexed array byte [] foo = new byte [200000]; for (int i = 0; i < foo.length; i++)_ _foo [i] = (byte) 0xff;_ _//long-indexed array_ _byte [[]] foo2 = new byte [[20000000000L]];_ _for (long i = 0; i < foo2.length; i++)_ _foo2 [i] = (byte) 0xff;_ _Syntax for declaration, instantation, instanceof and casting would_ _use doubled square brackets to differentiate it from_ _current array access. Single brackets can still be used for access/_ _mutation as the compiler should have sufficient_ _information to decide whether it can treat a specific reference as_ _an int-indexed or long-indexed ("large") array. The length field_ _would be a long rather than an int. Like standard arrays, large_ _arrays would derive directly from_ _java.lang.Object._ _MAJOR ADVANTAGE:_ _Java gains the ability to easily work with large sequential data sets._ _MAJOR BENEFIT:_ _Declaring extremely large arrays simply isn't possible right now; in_ _cases where such a structure is desirable, something_ _has to be hacked together. For applications dealing with large data_ _sets, the current upper limit on array size is constricting, and_ _will only grow more so as 64-bit systems continue to become more_ _common and RAM less expensive._ _MAJOR DISADVANTAGE:_ _This can't be implemented well solely via a compiler patch; existing_ _VMs would likely need to be changed to support this_ _concept natively; new VM instructions parallel to the existing array_ _instructions would likely be required. Also, a class_ _parallel to java.util.Arrays for performing standard operations on_ _large arrays would likely be desirable (although_ _presumably fairly simple to implement.)_ _ALTERNATIVES:_ _Build your own class for storing long-indexes sequences, possibly as_ _an array-or-arrays._ _EXAMPLES_ _SIMPLE EXAMPLE:_ _// calculate the first 3 billion fibonacci numbers and store them in_ _an array_ _long [[]] fib = new long [[3000000000L]];_ _fib [0] = 0;_ _fib [1] = 0;_ _for (long i = 2; i < fib.length; i++)_ _fib [i] = fib [i - 1] + fib [i - 2];_ _ADVANCED EXAMPLE:_ _// this doesn't really do anything particular useful, but does serve_ _to show how casting and instanceof are handled_ _byte [] foo = new byte [400000];_ _byte [[]] blah = new byte [[40000000000L]];_ _Object temp = Math.random () < 0.5 ? foo : blah;_ _if (foo instanceof byte [[]])_ _System.out.println (((byte [[]]) foo).length);_ _else_ _System.out.println (((byte []) foo).length);_ _DETAILS_ _SPECIFICATION:_ _Syntax-wise, the following expressions become legal:_ _SomeType [[]] foo; // declares foo as a long-indexed "large"_ _array of SomeType,_ _// where sometype is either a primitive or reference type_ _foo = new SomeType [[somelongvalue]]; // instantiates a large_ _array of length_ _// somelongvalue, which is either a long_ _// or some type that can upconvert or unbox_ _// to a long_ _long somelong = foo.length; // large arrays will have a "length"_ _field, which is_ _// a long indicating the number of elements in the array_ _foo instanceof SomeType [[]] // returns true if foo is a large array_ _// of SomeType, false otherwise_ _(SomeType [[]]) foo // casts foo to a large array of SomeType._ _If foo isn't_ _// a large array of SomeType or a subclass of SomeType,_ _// throws a ClassCastException_ _Large arrays are not castable or assignable to or from int-indexed_ _arrays of the same element type._ _int [[]] p = new int [40000]; // compiler error_ _int [] p = new int [[760000]]; // compiler error_ _int [] foo = (int []) (new int [[4040404040L]]); // throws_ _ClassCastException_ _int [[]] foo = (int [[]]) (new int [4040]); // throws_ _ClassCastException_ _Canonical class names for large arrays would be generated in a_ _similar fashion to those for standard arrays, but_ _using the opposite square bracket. The following table shows some_ _examples._ _declared type class name_ _int [[]] ]I_ _int [[]][[]] ]]I_ _Object [[]] ]Ljava.lang.Object;_ _The same set of indicator character (i.e., 'I' for int, 'L' for_ _reference types, etc.) as are currently used for arrays_ _would be used for large arrays._ _A set of additional VM instructions parallel to the current array_ _instructions would be added to support this feature._ _The following table shows the current instruction, the proposed_ _instruction for large arrays, and any semantic_ _differences (reference http://java.sun.com/docs/books/jvms/secondedition/html/Instructions2.doc.html)_ _:_ _array instruction proposed instruction differences_ _aaload lxaaload index value becomes a long_ _aastore lxaastore index value becomes a long_ _anewarray lxanewarray count value becomes a long_ _arraylength lxarraylength return value becomes a long_ _baload lxbaload index value becomes a long_ _bastore lxbastore index value becomes a long_ _caload lxcaload index value becomes a long_ _castore lxcastore index value becomes a long_ _daload lxdaload index value becomes a long_ _dastore lxdastore index value becomes a long_ _faload lxfaload index value becomes a long_ _fastore lxfastore index value becomes a long_ _iaload lxiaload index value becomes a long_ _iastore lxiastore index value becomes a long_ _laload lxlaload index value becomes a long_ _lastore lxlastore index value becomes a long_ _multianewarray lxmultianewarray countN values become longs_ _newarray lxnewarray count value becomes a long_ _saload lxsaload index value becomes a long_ _sastore lxsastore index value becomes a long_ _COMPILATION:_ _Compilation would be parallel to the present mechanism for compiling_ _arrays, but would output the lx* VM instructions listed above for_ _large arrays instead of the current array instructions._ _TESTING:_ _Any test sufficient to test support for current arrays should work_ _for this as well._ _LIBRARY SUPPORT:_ _It would probably be desirable to create large array versions of the_ _various java.util.Arrays methods, whether that be done by adding_ _methods to the existing java.util.Arrays class or putting them_ _somewhere new. At some point in the future, large java.util.List might be a desirable library feature, but that is beyond the scope of this proposal. REFLECTIVE APIS: An additional class (LargeArray or something of that sort) would need to be added to the java.lang.reflect package. This would have a similar set of methods and constructors to java.lang.reflect.Array, but with long "index" and "length" parameters where appropriate. OTHER CHANGES: VM needs to support the above-listed large array instructions. MIGRATION: Existing "hacks" to provide this kind of behavior could be replaced with large arrays. COMPATIBILITY BREAKING CHANGES: None. This feature simple doesn't exist; the proposed declaration/ instantation syntax currently results in a compiler error. EXISTING PROGRAMS: No changes. REFERENCES EXISTING BUGS: 4880587 (64-Bit indexing for arrays) URL FOR PROTOTYPE (optional): None.



More information about the coin-dev mailing list