是有类型的数组在JavaScript中的优势是,他们的工作在C相同或相似的? [英] Are the advantages of Typed Arrays in JavaScript is that they work the same or similar in C?

查看:118
本文介绍了是有类型的数组在JavaScript中的优势是,他们的工作在C相同或相似的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用类型化数组在JavaScript中玩耍。

I've been playing around with Typed Arrays in JavaScript.

var buffer = new ArrayBuffer(16);
var int32View = new Int32Array(buffer);

我想像正常阵列( [1,257,真])在JavaScript中有表现不佳,因为它们的值可以是任何类型的,因此,达成一项在内存偏移是不平凡的。

I imagine normal arrays ([1, 257, true]) in JavaScript have poor performance because their values could be of any type, therefore, reaching an offset in memory is not trivial.

我原本以为JavaScript数组下标工作一样的对象(因为它们有很多相似之处),并分别散列映射为主,需要基于哈希查找。但是,我还没有发现多少可靠的信息证实了这一点。

I originally thought that JavaScript array subscripts worked the same as objects (as they have many similarities), and were hash map based, requiring a hash based lookup. But I haven't found much credible information to confirm this.

所以,我认为,为什么类型化数组执行这么好,是因为他们喜欢用C正常阵列,在那里他们总是输入工作的原因。给定初始code上面的例子,并希望得到类型数组在10值...

So, I'd assume the reason why Typed Arrays perform so well is because they work like normal arrays in C, where they're always typed. Given the initial code example above, and wishing to get the 10th value in the typed array...

var value = int32View[10];


  • 的类型是的Int32 ,所以每个值必须包含 32 位或 4 字节。

  • 下标为 10

  • 因此,在该值的内存位置是<数组offset> +(4×10),然后读 4 字节来获得的总价值。

    • The type is Int32, so each value must consist of 32 bits or 4 bytes.
    • The subscript is 10.
    • So the location in memory of that value is <array offset> + (4 * 10), and then read 4 bytes to get the total value.
    • 基本上,我只是想确认我的假设。是我解决这个正确的思想,如果没有,请详细说明。

      I basically just want to confirm my assumptions. Is my thoughts around this correct, and if not, please elaborate.

      我检查了 V8源来看看,如果我能回答它自己,但我的C生锈,我不太熟悉C ++。

      I checked out the V8 source to see if I could answer it myself, but my C is rusty and I'm not too familiar with C++.

      推荐答案

      类型化数组是由WebGL的标准委员会设计,性能方面的原因。通常情况下JavaScript数组是通用的,可以容纳对象,其他数组等等 - 和元素不一定在内存中连续的,就像他们会在C WebGL的要求缓冲区在存储器中的顺序,因为这是底层的C API如何期待他们。如果不使用类型数组,传递一个普通的数组到的WebGL功能需要大量的工作:每个元素都必须进行检查,类型检查,如果这是正确的事情(如浮点),那么它复制到一个单独的顺序类似C缓冲区,然后传递顺序缓冲到C API。哎哟 - 大量的工作!对于性能敏感的应用程序的WebGL,这可能导致帧率大幅下降。

      Typed Arrays were designed by the WebGL standards committee, for performance reasons. Typically Javascript arrays are generic and can hold objects, other arrays and so on - and the elements are not necessarily sequential in memory, like they would be in C. WebGL requires buffers to be sequential in memory, because that's how the underlying C API expects them. If Typed Arrays are not used, passing an ordinary array to a WebGL function requires a lot of work: each element must be inspected, the type checked, and if it's the right thing (e.g. a float) then copy it out to a separate sequential C-like buffer, then pass that sequential buffer to the C API. Ouch - lots of work! For performance-sensitive WebGL applications this could cause a big drop in the framerate.

      在另一方面,像你这样的问题表明,类型化数组已经在他们背后的幕后存储使用顺序类似C的缓冲区。当你写一个类型数组,你确实是分配给幕后类似C的阵列。为WebGL的的目的,这意味着缓冲器可直接由相应的C API一起使用。

      On the other hand, like you suggest in the question, Typed Arrays use a sequential C-like buffer already in their behind-the-scenes storage. When you write to a typed array, you are indeed assigning to a C-like array behind the scenes. For the purposes of WebGL, this means the buffer can be used directly by the corresponding C API.

      请注意你的内存地址计算是不是很够:在浏览器的必须同时界限检查阵列,以prevent超出范围的访问。这与任何种类的Javascript阵列的发生,但在许多情况下,聪明的Javascript引擎可以省略校验时,它可以证明的索引值是已经界限之内(如从0循环到阵列的长度)。它也有检查数组索引确实是一个数字,而不是一个字符串或其他什么东西!但它在本质上是像你描述的,采用C类地址。

      Note your memory address calculation isn't quite enough: the browser must also bounds-check the array, to prevent out-of-range accesses. This has to happen with any kind of Javascript array, but in many cases clever Javascript engines can omit the check when it can prove the index value is already within bounds (such as looping from 0 to the length of the array). It also has to check the array index is really a number and not a string or something else! But it is in essence like you describe, using C-like addressing.

      但是... 这还不是全部!在某些情况下,聪明的JavaScript引擎能也演绎出普通的JavaScript数组的类型。在像V8引擎,如果你让一个普通的JavaScript数组,只有专卖店它漂浮,V8可以乐观地决定它是float数组和优化code它产生了点。然后,性能可以等同于类型数组。所以类型数组实际上不是要达到最大的性能:只使用数组predictably(每件同一类型)和一些发动机可以优化为以​​及

      BUT... that's not all! In some cases clever Javascript engines can also deduce the type of ordinary Javascript arrays. In an engine like V8, if you make an ordinary Javascript array and only store floats in it, V8 may optimistically decide it's an array of floats and optimise the code it generates for that. The performance can then be equivalent to typed arrays. So typed arrays aren't actually necessary to reach maximum performance: just use arrays predictably (with every element the same type) and some engines can optimise for that as well.

      那么,为什么类型化阵列还需要存在吗?

      So why do typed arrays still need to exist?


      • 像推断数组类型的优化是的非常复杂的。如果V8演绎一个普通的阵列只有在它漂浮,那么你存储一个对象中的元素,的它必须去优化的再生和code,使得阵列再次通用。这是了不起的成就,这一切都透明地工作。类型化数组是简单得多:他们保证是一种类型,而你只是不能存储其他东西状物体在他们

      • 的优化是保证永远不会发生;你可以仅存储漂浮在一个普通的数组,但发动机可以决定因各种原因没有去优化它。

      • 它们更简单的方法等,不太复杂的JavaScript引擎可以很容易地实现它们的事实。他们并不需要所有的高级deoptimisation支持。

      • 即使有真先进的发动机,证明的优化可以使用是非常困难的,有时是不可能的。一个类型数组显著简化证明的水平引擎需要能够围绕它进行最佳化。从类型数组返回的值肯定是某种类型的,和发动机可以优化其结果是类型。从一个普通的阵列返回的值在理论上可以具有任何类型,并且发动机可能无法证明其总是具有相同的类型的结果,并因此产生较少有效的code。因此,围绕一个类型数组code更容易优化。

      • 类型数组删除的机会,犯了一个错误。你就不能小心存放一个对象,突然得到性能差远了。

      • Optimisations like deducing the type of arrays is really complicated. If V8 deduces an ordinary array has only floats in it, then you store an object in an element, it has to de-optimise and regenerate code that makes the array generic again. It's quite an achievement that all this works transparently. Typed Arrays are much simpler: they're guaranteed to be one type, and you just can't store other things like objects in them.
      • Optimisations are never guaranteed to happen; you may store only floats in an ordinary array, but the engine may decide for various reasons not to optimise it.
      • The fact they're much simpler means other less-sophisticated javascript engines can easily implement them. They don't need all the advanced deoptimisation support.
      • Even with really advanced engines, proving optimisations can be used is extremely difficult and can sometimes be impossible. A typed array significantly simplifies the level of proof the engine needs to be able to optimise around it. A value returned from a typed array is certainly of a certain type, and engines can optimise for the result being that type. A value returned from an ordinary array could in theory have any type, and the engine may not be able to prove it will always have the same type result, and therefore generates less efficient code. Therefore code around a typed array is more easily optimised.
      • Typed arrays remove the opportunity to make a mistake. You just can't accidentally store an object and suddenly get far worse performance.

      因此​​,简而言之,普通数组在理论上快同样的类型数组。但类型数组使它更容易达到最佳性能。

      So, in short, ordinary arrays can in theory be equally fast as typed arrays. But typed arrays make it much easier to reach peak performance.

      这篇关于是有类型的数组在JavaScript中的优势是,他们的工作在C相同或相似的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆