简体   繁体   English

Emscripten将C ++编译为JavaScript和Asm.js的结果很差

[英]Bad results with Emscripten compiling C++ to JavaScript & Asm.js

For a course at the university I have to demonstrate asm.js. 对于大学的课程,我必须演示asm.js。 I found a tutorial ( http://www.sitepoint.com/understanding-asm-js/ ) which was exactly what I was looking for. 我发现了一个教程( http://www.sitepoint.com/understanding-asm-js/ ),而这正是我想要的。 So I created the given C++-File and compiled it with Emscripten. 因此,我创建了给定的C ++文件并使用Emscripten对其进行了编译。 The result was a nearly 10000-lines long file. 结果是将近10000行的文件。 Nowhere to find the "use asm"-statement. 无处可找到“使用asm”语句。 And by comparison to the handwritten JavaScript-File it is much slower. 与手写JavaScript文件相比,它要慢得多。

I'm using a portable Emscripten-SDK-package and updated it before using it. 我正在使用便携式Emscripten-SDK软件包,并在使用前对其进行了更新。

How can I get Emscripten to generate good asm-Code? 我如何获得Emscripten生成良好的asm代码?


UPDATE: I found a different solution for my demonstration without Emscipten: https://gist.github.com/dherman/3d0b4733303eaf4bae5e . 更新:在没有Emscipten的情况下,我为演示找到了另一种解决方案: https ://gist.github.com/dherman/3d0b4733303eaf4bae5e。 Maybe someone need this to. 也许有人需要这个。

10k lines of javascript is quite modest considering it must include the functional equivalent to system libraries (libc, etc...) which live as separate files when you execute c++ compiled source - when browsers execute javascript its sandboxed and cannot access such system libs on the target computer (due to security, OS neutrality ...) for instance just do a ldd command on some dynamically linked c/c++ executable to gain an appreciation of what the bulk of those 10k lines of javascript are replacing: 考虑到它必须包含相当于系统库(libc等)的功能,因此10k行的javascript是相当适度的,当您执行c ++编译的源代码时,这些库作为单独的文件存在-当浏览器执行javascript的沙箱且无法访问上的此类系统库时例如,目标计算机(由于安全性,操作系统中立性等原因)只需对某些动态链接的c / c ++可执行文件执行ldd命令,即可了解这10,000行javascript替换中的大部分内容:

ldd /bin/ls 


linux-vdso.so.1 =>  (0x00007fff8c865000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f7b82854000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f7b8264b000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7b82285000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f7b82018000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7b81e14000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7b82aba000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f7b81c0e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7b819f0000)

this might give you more tips on using asm.js and emscripten: https://hacks.mozilla.org/2014/11/porting-to-emscripten/ 这可能会为您提供有关使用asm.js和emscripten的更多提示: https ://hacks.mozilla.org/2014/11/porting-to-emscripten/

From my testing Emscripten seems to only use asm.js, and put "use asm"; 从我的测试来看,Emscripten似乎只使用asm.js,并输入"use asm"; into the generated Javascript at optimization levels -O1 and above. 进入优化级别为-O1及更高级别的生成的Javascript。 So when compiling you need to pass -O1 (or a higher level than 1 ) to the compiler: 因此,在编译时,您需要将-O1 (或高于1级别)传递给编译器:

eemcc source.cpp -O1 -o target.js`

If you don't specifiy an optimization level, or pass -O0 : 如果您未指定优化级别,或通过-O0

eemcc source.cpp -O0 -o target.js`

then "use asm"; 然后"use asm"; doesn't get put into the generated Javascript. 不会放入生成的Javascript中。

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

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