5

When I tried to benchmark the difference in execution time between the forward and the reverse loop iteration in server-side JavaScript (SSJS), a (strange) problem occured. While this example code

var i,n=9999;
var arr=new Array(n);
for (i=0;i<n;i++) arr[i]=i; // WORKS
i=n; while (i--) arr[i]=i; // WORKS

works fine, the following code

var i,n=10000; // changed n from 9999 to 10000
var arr=new Array(n);
for (i=0;i<n;i++) arr[i]=i; // WORKS
i=n; while (i--) arr[i]=i; // THROWS ArrayIndexOutOfBoundsException

throws an ArrayIndexOutOfBoundsException for the reverse iteration. The reverse while loop only works fine as long as the array length is lower than 10000. Can anyone tell me what's going on here?

xpages-noob
  • 1,569
  • 1
  • 10
  • 37
  • 2
    That's really interesting! I think you hit to a bug. SSJS stores arrays as a Java object and I think it keeps long arrays in chunks after a specific point (must be 10K). Your code throwed null pointer exception for me inside the "com.ibm.jscript.std.ArrayObject$EntrySlot.addEntry(ArrayObject.java:91)". When I analyze, the second loop gives the error at the element 8999. So if you remove the second array and just `print(arr[8000]);` it will still give the error. I hope some IBMer sees this question :) – Serdar Basegmez Aug 25 '14 at 12:56
  • I've seen it and independently reproduced the same as you Serdar, hitting the issue on element 8999. Strange indeed. – Brian Gleeson - IBM Aug 25 '14 at 12:59
  • @BrianGleeson-IBM: I don't know if you are or will be looking into this issue, but (in case you do) please share your findings with me/us by adding comments or an answer here. Up to now this issue hasn't caused any problems in my XPages applications because I'm usually not working with such large arrays. However, I frequently use the reverse iteration and if this cannot be fixed (via notes.ini, JVM settings etc.) I'll have to rewrite all the loops in order to eliminate this potential error. – xpages-noob Aug 25 '14 at 14:31
  • @xpages-noob I'd use java.util.ArrayList within SSJS code. Also raising a PMR would provide a quick hotfix, otherwise, you will wait a little bit longer than you might expect. – Serdar Basegmez Aug 26 '14 at 05:52
  • 1
    This issue is now being tracked as SPR#MKEE9NDKQX. The problem is in the core runtime, so if a fix is found it wont be part of an ExtLib release. It can only go into a new product release, a fix pack or a hot fix. – Brian Gleeson - IBM Aug 28 '14 at 08:20

2 Answers2

1

Not an answer, but as a workaround for such a bug, I recommend using java.util.ArrayList.

var i,n=10000;

var arr=new java.util.ArrayList(n); //changed to ArrayList

for (i=0;i<n;i++) arr.add(i); // changed to .add(value)
i=n; while (i--) arr.set(i, i); // changed to .set(i, value)
Serdar Basegmez
  • 3,355
  • 16
  • 20
  • Thanks for your answer. I know I could use ArrayLists or Vectors as a workaround, but I have a lot of prototype functions for the Array object and I frequently make use of them. It would be far less work for me to change all the loops from reverse to forward iteration than to make all my code work with ArrayLists or Vectors. – xpages-noob Aug 26 '14 at 07:06
0

A fix for this issue has been delivered to the XPages Core Runtime, and will be part of the next XPages product release. Thanks for bringing it to our attention

Brian Gleeson - IBM
  • 2,565
  • 15
  • 21