Re: [JS-internals] Growing arrays

2014-07-17 Thread Igor Bukanov
Well, with Firefox 31 using Array(length).fill(0) to construct the the array could be the best solution as it preallocates the array in one go. On 18 July 2014 00:42, Nicholas Nethercote wrote: > In this case, maybe not, but there are many similar cases where using > a typed array is less feasibl

Re: [JS-internals] Growing arrays

2014-07-17 Thread Nicholas Nethercote
On Thu, Jul 17, 2014 at 1:14 AM, Katelyn Gadd wrote: > What happens if you combine 'new Array(length)' + 'array[length - 1] = > '? That seems like it would produce the optimal > behavior in all cases. Thanks for the suggestion! This does work for SpiderMonkey, at least in some case, but it might

Re: [JS-internals] Growing arrays

2014-07-17 Thread Nicholas Nethercote
On Thu, Jul 17, 2014 at 1:35 AM, Jan de Mooij wrote: > > ArrayObject::EagerAllocationMaxLength (currently 2048) determines when > |new Array(length)| will eagerly allocate. Maybe if it's > 2048 we > could allocate a bit more than 10 elements.. Thanks! I've written a patch so that it allocates min

Re: [JS-internals] Growing arrays

2014-07-17 Thread Terrence Cole
On 07/16/2014 11:40 PM, Nicholas Nethercote wrote: > On a related note: ObjectElements::VALUES_PER_HEADER is 2. Is this > because 2 * sizeof(HeapSlot) is 16 bytes, which is equal to > sizeof(ObjectElements)? Yes. > Nick > > On Wed, Jul 16, 2014 at 11:34 PM, Nicholas Nethercote > wrote: >> On W

Re: [JS-internals] Growing arrays

2014-07-17 Thread Kannan Vijayan
On 2014-07-17, 4:35 AM, Jan de Mooij wrote: On Thu, Jul 17, 2014 at 8:34 AM, Nicholas Nethercote wrote: Oh wait. It depends on the value of |length|. If it's 2050 or less, then the right-sized array is allocated immediately. Otherwise, it allocates 10 elements and then does the doubling thing.

Re: [JS-internals] Growing arrays

2014-07-17 Thread Nicolas B. Pierron
On 07/16/2014 11:08 PM, Nicholas Nethercote wrote: So then I tried reverting that change and inserting this line just before the loop: array[length - 1] = 0; And now it avoids the doubling allocations -- the array elements are allocated once, at the right size. But it feels dirty, and I don'

Re: [JS-internals] Growing arrays

2014-07-17 Thread Mike Shaver
I would expect that for some values of length it would be deemed "too sparse" and no longer treated as a single underlying vector. On Thursday, July 17, 2014, Katelyn Gadd wrote: > What happens if you combine 'new Array(length)' + 'array[length - 1] = > '? That seems like it would produce the op

Re: [JS-internals] Growing arrays

2014-07-17 Thread Jan de Mooij
On Thu, Jul 17, 2014 at 8:34 AM, Nicholas Nethercote wrote: > Oh wait. It depends on the value of |length|. If it's 2050 or less, > then the right-sized array is allocated immediately. Otherwise, it > allocates 10 elements and then does the doubling thing. I guess > there's a limit to how much we'

Re: [JS-internals] Growing arrays

2014-07-17 Thread Katelyn Gadd
What happens if you combine 'new Array(length)' + 'array[length - 1] = '? That seems like it would produce the optimal behavior in all cases. On Wed, Jul 16, 2014 at 11:40 PM, Nicholas Nethercote wrote: > On a related note: ObjectElements::VALUES_PER_HEADER is 2. Is this > because 2 * sizeof(Heap

Re: [JS-internals] Growing arrays

2014-07-16 Thread Nicholas Nethercote
On a related note: ObjectElements::VALUES_PER_HEADER is 2. Is this because 2 * sizeof(HeapSlot) is 16 bytes, which is equal to sizeof(ObjectElements)? Nick On Wed, Jul 16, 2014 at 11:34 PM, Nicholas Nethercote wrote: > On Wed, Jul 16, 2014 at 11:08 PM, Nicholas Nethercote > wrote: >> Hi, >> >>

Re: [JS-internals] Growing arrays

2014-07-16 Thread Nicholas Nethercote
On Wed, Jul 16, 2014 at 11:08 PM, Nicholas Nethercote wrote: > Hi, > > Here's some code from pdf.js: > > function stringToArray(str) { > var length = str.length; > var array = []; > for (var i = 0; i < length; ++i) { > array[i] = str.charCodeAt(i); > } > return array; > } > > This ki

[JS-internals] Growing arrays

2014-07-16 Thread Nicholas Nethercote
Hi, Here's some code from pdf.js: function stringToArray(str) { var length = str.length; var array = []; for (var i = 0; i < length; ++i) { array[i] = str.charCodeAt(i); } return array; } This kind of code shows up in a number of places in pdf.js. Pretty simple -- filling in an arr