简体   繁体   English

V8可以安装在嵌入式芯片中吗?

[英]Can V8 be installed in an embedded chip?

In a channel I frequent someone asked the following question:在我经常看到的一个频道中,有人问了以下问题:

This company thats trying to hire me is doing vanilla javascript and jquery.这家试图雇用我的公司正在做普通的 javascript 和 jquery。 No react, or any other frameworks.没有反应,或任何其他框架。 JQuery is know for being the hackiest shit and isn't it weird that in 2020 they're still not using anything better? JQuery 是众所周知的最骇人听闻的狗屎,在 2020 年他们仍然没有使用更好的东西,这不是很奇怪吗? Would you pick up such offer?你会接受这样的提议吗? [1] [1]

This got me thinking: surely, if a company is doing anything in vanilla, it has a good reason for that and it's pretty popular to shove javascript into places that do not make sense to run it.这让我想到:当然,如果一家公司正在做任何事情,它有一个很好的理由,将 javascript 推到运行它没有意义的地方是很受欢迎的。 Point of Sale systems, embedded systems, they don't have many resources to spare.销售点系统、嵌入式系统,它们没有多少资源可供使用。 As a result you cut every corner possible to get more performance out of the chip you're trying to use.因此,您尽可能地切入每一个角落,以从您尝试使用的芯片中获得更多性能。 Such thought reminded me of the time when Mark Zuckerberg said that "Facebook wasted too much time trying to implement mobile applications in javascript and HTML5 instead of native" [2].这样的想法让我想起了马克扎克伯格说过“Facebook浪费了太多时间试图在 javascript 和 HTML5 而不是原生实现移动应用程序”[2]。 I can't seem to find the source, but I did hear some rumors that he was promptly proven wrong by some company which knew ins and outs of how javascript works on mobile devices.我似乎无法找到消息来源,但我确实听到了一些谣言,说他被一些公司迅速证明是错误的,这些公司了解 javascript 如何在移动设备上工作的来龙去脉。 And that thought caused me to start pondering about the following:这个想法让我开始思考以下问题:

  1. How much code can the embedded chips contain?嵌入式芯片可以包含多少代码?
  2. Would it be possible to embed an entire V8 engine into a chip to get javascript running on them at "on par" (or near on par) performance that you would with C/C++?是否有可能将整个 V8 引擎嵌入到芯片中,以使 javascript 在它们上以“与 C/C++ 一样”(或接近于同等)性能运行?
  3. Is it really that much of a stigma to do vanilla javascript nowadays?现在做香草javascript真的有那么大的耻辱吗?

The last question is more of a rhetorical one.最后一个问题更像是一个修辞问题。 But I will appreciate any input.但我会很感激任何意见。


  1. The question of course is edited to suit the audience.当然,这个问题经过编辑以适合观众。
  2. https://techcrunch.com/2012/09/11/mark-zuckerberg-our-biggest-mistake-with-mobile-was-betting-too-much-on-html5/ https://techcrunch.com/2012/09/11/mark-zuckerberg-our-biggest-mistake-with-mobile-was-betting-too-much-on-html5/

V8 developer here. V8 开发人员在这里。 As commenters have pointed out, this question is a bit too vague to have a solid answer (and is probably also not really on topic here), but I can offer a few thoughts.正如评论者所指出的,这个问题有点太模糊了,无法给出一个可靠的答案(而且这里可能也不是真正的主题),但我可以提供一些想法。

V8 has certain hard requirements, eg: V8 有一定的硬性要求,例如:

  • it needs a couple of megabytes of memory just to start up, so it won't run on microcontrollers that have less than that它需要几兆字节的 memory 才能启动,因此它无法在少于该值的微控制器上运行
  • for normal operation, it needs permission (from the OS/kernel) to allocate executable memory.对于正常操作,它需要权限(来自操作系统/内核)来分配可执行 memory。 Recent versions support "jitless" mode, which avoids this, at the cost of a performance penalty (no generation of optimized code) -- how much that matters depends on your workload.最近的版本支持“jitless”模式,可以避免这种情况,但代价是性能损失(不生成优化代码)——这有多重要取决于您的工作量。
  • the code it generates targets certain minimum CPU features, eg SSE2 on x86 and armv6+vfpv2 on ARM.它生成的代码针对某些最低 CPU 功能,例如 x86 上的 SSE2 和 ARM 上的 armv6+vfpv2。 This minimum applies even to "jitless" mode, because that executes code that V8 generated at build time and included in its binary.这个最小值甚至适用于“jitless”模式,因为它执行 V8 在构建时生成并包含在其二进制文件中的代码。 (With sufficient engineering investment, it would be technically possible to modify its code generation backend to target lower hardware requirements.) (如果有足够的工程投资,技术上可以修改其代码生成后端以针对较低的硬件要求。)
  • it officially supports running on Windows, MacOS, Linux, Android, Fuchsia;正式支持在Windows、MacOS、Linux、Android、Fuchsia上运行; and there's community support for a few other operating systems.还有一些其他操作系统的社区支持。 If your embedded device has a different OS, or no kernel at all, you'd have to make adaptations.如果您的嵌入式设备具有不同的操作系统,或者根本没有 kernel,则您必须进行调整。

Aside from that, it's just a question of how fast you want things to be.除此之外,这只是您希望事情多快的问题。 Platform compatibility requirements aside, almost any software runs on almost any hardware, if you're willing to wait long enough.除了平台兼容性要求之外,如果您愿意等待足够长的时间,几乎所有软件都可以在几乎任何硬件上运行。 Ever seen a Raspberry Pi boot a regular Linux distro, or Windows XP on a Pentium-133?曾经见过 Raspberry Pi 在 Pentium-133 上启动常规 Linux 发行版或 Windows XP 吗? It works, but it's slow;它有效,但速度很慢; There's a reason people spend lots of money on faster hardware ;-)人们在更快的硬件上花费大量金钱是有原因的;-)

A few more considerations in this regard:在这方面还有一些考虑:

  • In many cases, raw execution speed does not matter so much.在许多情况下,原始执行速度并不重要。 For example, when the heavy lifting is done in native modules, it's often fine to use a relatively slow scripting language as "glue" between those modules.例如,当在本地模块中完成繁重的工作时,通常可以使用相对较慢的脚本语言作为这些模块之间的“粘合剂”。
  • V8 is not targeted at severely resource-constrained environments, but there are other JavaScript engines that specifically target microcontrollers. V8 并非针对资源严重受限的环境,但还有其他专门针对微控制器的 JavaScript 引擎。 Many of them only support a limited feature set (eg only ES3 or ES5, no modern EcmaScript features) in order to save memory and complexity.它们中的许多只支持有限的功能集(例如,只有 ES3 或 ES5,没有现代 EcmaScript 功能)以节省 memory 和复杂性。
  • If a given use case does have strict performance requirements on weak hardware, then it makes sense to use low-level languages.如果给定的用例确实对弱硬件有严格的性能要求,那么使用低级语言是有意义的。 Many microcontrollers are programmed in C dialects (ie subsets of C) for this reason.由于这个原因,许多微控制器都使用 C 方言(即 C 的子集)进行编程。

Speaking of C/C++ vs JavaScript: it's hard to compare.说到 C/C++ 与 JavaScript:很难比较。 The short answer is that C is faster, but it depends.简短的回答是 C 更快,但这取决于。 You can craft microbenchmarks where dynamically compiled code is at an advantage, so JavaScript will be faster.您可以在动态编译代码具有优势的情况下制作微基准,因此 JavaScript 会更快。 Usually, language features like automatic garbage collection cost a few percent of performance compared to manual memory management.通常,与手动 memory 管理相比,自动垃圾收集等语言功能会花费百分之几的性能。 Mitigating the slowness of dynamic languages by JIT-compiling them similarly costs a bit of time, just because it's extra work that the CPU has to do.通过 JIT 编译来缓解动态语言的缓慢性同样需要花费一些时间,因为这是 CPU 必须做的额外工作。 On sufficiently fast hardware, all these overheads are typically small enough not to matter, but when you're running against the limits of what your hardware can do at all (while still meeting user expectations about performance), then that assessment might change.在足够快的硬件上,所有这些开销通常都足够小,无关紧要,但是当您运行硬件完全可以做的限制时(同时仍然满足用户对性能的期望),那么评估可能会改变。

This is too many questions in one for a complete answer but.对于一个完整的答案,这是一个太多的问题,但是。

  1. Would it be possible to embed an entire V8 engine into a chip to get javascript running on them at "on par" (or near on par) performance that you would with C/C++?是否有可能将整个 V8 引擎嵌入到芯片中,以使 javascript 在它们上以“与 C/C++ 一样”(或接近于同等)性能运行?

No V8 can't run on a microcontroller.没有 V8 不能在微控制器上运行。 Maybe a microprocessor.也许是微处理器。 But YES you can run javascript on a microcontroller.但是是的,您可以在微控制器上运行 javascript。 Espruino is a great example of that. Espruino就是一个很好的例子。 I don't know about the speed of it but it seems to be capable of filling the hobbyist space for programming microcontrollers.我不知道它的速度,但它似乎能够填补业余爱好者对微控制器进行编程的空间。 Just as micropython does.就像 micropython 一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM