No, there is no way to write to stdout directly, and that's intentional. WebAssembly code can only use imported functions to communicate with the environment.
The lowest-hanging fruit here is to move the HandleScope out of the loop. HandleScope creation/destruction is somewhat expensive, and you certainly don't need a fresh scope for every iteration. Beyond that, I think your other idea is on the right track: every transition from generated code (i.e., in this case, your Wasm code) and C++ code is going to have a cost that's comparatively higher than calling within those worlds (Wasm -> Wasm or C++ -> C++). One way to reduce the number of expensive calls would be to reserve some memory for the string representation, move the creation of the string to Wasm (for ints it's easy, for floats you can find some existing library and compile it to Wasm), and then have a single call out that reads the entire null-terminated string and prints it to stdout. You didn't say how you're embedding V8 and how much functionality you need from it. If you only need WebAssembly (no JavaScript), you could take a look at the Wasm C/C++ API <https://github.com/WebAssembly/wasm-c-api/> and our implementation of it <https://docs.google.com/document/d/1oFPHyNb_eXg6NzrE6xJDNPdJrHMZvx0LqsD6wpbd9vY/edit>, which gives you considerably faster calls between Wasm and C++ (in both directions) than the JS-style callbacks that the regular V8 API uses. (It's not possible to mix both APIs.) On Mon, Mar 23, 2020 at 9:50 AM Immanuel Haffner <haffner.imman...@gmail.com> wrote: > Hi all, > > I am compiling WebAssembly modules that perform computations and produce > results. These results (tuples of values of various types) I would like to > print to stdout. My current approach is importing a callback function to > the WebAssembly module instance that performs the printing: > > void print(const v8::FunctionCallbackInfo<v8::Value> &info) > { > for (int i = 0; i != info.Length(); ++i) { > v8::HandleScope handle_scope(info.GetIsolate()); > if (i != 0) std::cout << ','; > std::cout << *v8::String::Utf8Value(info.GetIsolate(), info[i]); > } > std::cout << '\n'; > } > > This callback prints an entire tuple in CSV format. > > While this works, it has some drawbacks for me. The biggest drawback is > performance! The callbacks from the VM to the host seem horribly slow > compared to native function calls. Is there something I can do about this? > Is there a way to speed up these callbacks from VM to the host? > > I tried an alternative solution, where the WebAssembly module writes the > results to its linear memory and the host reads them back from the memory, > interprets them, and prints them. This seems to be faster, actually. > However, it requires additional memory and the host must "interpret" the > values. This is in my scenario also a performance issue, because I don't > know the types of the result when compiling the host code. I only know the > types when compiling the WebAssembly module. Hence, in this approach there > is some interpretation overhead involved. > > Is there maybe a way to directly write to stdout from WebAssembly? > > Thanks & regards, > Immanuel > > -- > -- > v8-users mailing list > v8-users@googlegroups.com > http://groups.google.com/group/v8-users > --- > You received this message because you are subscribed to the Google Groups > "v8-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to v8-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/v8-users/b8840b18-820d-406d-a24b-b99545fbf09a%40googlegroups.com > <https://groups.google.com/d/msgid/v8-users/b8840b18-820d-406d-a24b-b99545fbf09a%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/CAKSzg3SohJtc7Wwo55r8_779y9eTgHb4pTJUJ2tm6TSm7XoCPg%40mail.gmail.com.