Easy 10x performance boost for TypedArray.prototype.fill()? (in special cases)

46 views
Skip to first unread message

Ashton Six

unread,
Jun 3, 2022, 5:34:01 AM6/3/22
to v8-dev
Hi,

I noticed `new Int8Array(int32.buffer).fill(-1)` is equivalent to `int32.fill(-1)` but 10x faster, since [Uint, Int]8.fill() uses memset whereas every other implementation of TypedArray.fill() uses a for loop (https://github.com/llvm/llvm-project/blob/8bb1dbbf7544eaac3afab8d1f91b71f383dab903/libcxx/include/algorithm#L1987-L2005). Benchmark: https://jsbench.me/1zl3y8q6q7/1.

Is it worth making this optimisation part of v8 for TypedArray.fill(0), Int16Array.fill(-1) and Int32Array.fill(-1)?

Regards,
Ashton

Marja Hölttä

unread,
Jun 3, 2022, 5:56:26 AM6/3/22
to v8-...@googlegroups.com
Interesting, thanks for making us / me aware of this.

It sounds like a generally reasonable optimization; at least in the fill(0) case. Not sure how common fill(-1) is but if it doesn't increase the code complexity too much, why not.


--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/3e9d1775-4c4b-4b6a-8d96-74d412e2d52an%40googlegroups.com.


--


Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Liana Sebastian.

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


Diese E-Mail ist vertraulich. Falls sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde.

    

This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.

Leszek Swirski

unread,
Jun 3, 2022, 7:10:14 AM6/3/22
to v8-...@googlegroups.com
I guess we could do it for anything where all two/four bytes of the fill value are identical? Probably (aside from 0) an unusual case like you say, but the complexity of checking it should be low enough.

Jakob Kummerow

unread,
Jun 3, 2022, 7:33:14 AM6/3/22
to v8-dev
I guess 0 is by far the most common case.


Locally, I'm seeing 1.5x - 2x improvement, far from 10x but clearly measurable for large arrays.

Ashton Six

unread,
Jun 3, 2022, 8:27:30 AM6/3/22
to v8-dev
Cool, glad this has worked out.

I decided to play around a bit more and figured out a faster implementation of TypedArray.prototype.fill() for arbitrary values BTW (3x improvement for me):

```
function fill(arr, value) {
  arr[0] = value
  for (let i = 1; i < arr.length; i *= 2) {
    arr.copyWithin(i, 0, i)
  }
  return arr
}
```

This works out because `TypedArray.prototype.copyWithin()` uses memmove. Maybe worth doing for 16/32/64-bit typed arrays, perhaps even for plain old javascript arrays?

Updated benchmark: https://jsbench.me/1zl3y8q6q7/2

Claudia

unread,
Jun 7, 2022, 1:30:00 PM6/7/22
to v8-dev
Have you all also considered using `rep stos{b,w,d,q}` on x86 for this? While it's highly architecture-specific (ARM's also got a variant on the way, but it's not here yet), it might be able to work for similar effect as a universal optimization.
Reply all
Reply to author
Forward
0 new messages