ChakraCore icon indicating copy to clipboard operation
ChakraCore copied to clipboard

No FastPath for new Array(constant)

Open kunalspathak opened this issue 10 years ago • 3 comments

Today we don't generate fast path for below case.

var x = new Array(5); // argument should be constant between 0 and 8 inclusive.

That is because in lowerer , we fetch the wrong source of instruction. I tried fixing it by extracting the right argument and based on that emit fast path. For below test case I saw 10% win.

var start = new Date().getTime();
function test() {
  var arr = [];
  for (var j = 0; j < 100; j++) {
    arr.push(new Array(5));
  }
  return arr;
}

for (var i = 0; i < 1000000; i++) {
  test();
}
var end = new Date().getTime();
var time = end - start;
print(time);

However, I later realized that Array ctor can take multiple parameters that are array elements and we need to emit code to initialize those elements in fast path. If we should/do not want to emit fast path if parameters are array elements (although less than or equal to 8), then my solution should work and I can submit the PR for this.

This was never caught because the only other code path that creates this fast path is NewScObjArrayNoArg.

I was also curious why we decided to emit fast path only for array length up to 8 and not beyond. On scanning through benchmarks, I noticed that this would improve Sunspider but other benchmarks like Octane, Jetstream, etc. won't be improved because they create Array from ctor of length around 1024 or greater.

kunalspathak avatar Jul 13 '16 02:07 kunalspathak

FYI - @LouisLaf , @curtisman

kunalspathak avatar Jul 13 '16 02:07 kunalspathak

On the second thought, I was wondering if we can also emit array creation fast path for below scenario

new Array(variable); // 0 <= variable <= 8;

We can have bound checks on argument passed to Array ctor and call helper if argument is beyond specific range. In acme-air benchmark, below are the js functions that call Array ctor specifying size through variable instead of constant.

array_no_fastpath

I traced the variable value passed to the Array ctor and found out all of them were less than 8. Optimizing this case should give us approx. 8% win.

  • index.js!proto.handle = 3
  • _stream_writable.js!onwrite = 8
  • _stream_writable.js!clearBuffer = 2
  • net.js!Socket.prototype._writeGeneric = 8
  • _stream_readable.js!Readable.prototype.read - less than 5 (through inlining nextTick)
  • _stream_readable.js!readableAddChunk - less than 5 (through inlining nextTick)
  • internal/process/next_tick.js!nextTick less than 5

kunalspathak avatar Jul 15 '16 17:07 kunalspathak

@kunalspathak , nice find that we are fetching the wrong argument! Getting src2 of the new array instruction and using it as length should work fine as long as you detect that it is the only argument passed to the constructor and call the helper if its not. Or, we can also walk the argument chain and count the number of arguments and emit the fastpath if the number is <= 8 (I don't know the reasoning behind '8').

rajatd avatar Jul 19 '16 18:07 rajatd