Proposal: Large arrays (original) (raw)

Jeremy Manson jeremy.manson at gmail.com
Tue Mar 24 10:49:22 PDT 2009


This is actually a pretty big change. I doubt they will let you do bytecode changes. Also, library changes would include everything that touches an array, like all of the Collections.toArray classes. Also, as part of such a change, collections would need to add support for more than 2^31-1 elements, and NIO Buffers would, too.

Instead of bytecode changes, this could be implemented as an array of arrays for the moment (even though that is kind of ugly).

That's not to say that I don't wish that this existed, though.

Jeremy

On Tue, Mar 24, 2009 at 9:35 AM, james lowden <jl0235 at yahoo.com> 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