emscripten-core / emscripten-core/emscripten

Replace GROWABLE_HEAPS with native functions

Open
#18,589 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I was playing around with your benchmark, and i think i came up with an option to reduce the overhead of `warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly`

Instead of always relying on the array views, have a native function to call into which can cast the memory address, since this is on the wasm side no need to check if the underlying buffer has changed.

Here is the new benchmark.cpp

```cpp
#include
#include
#include
#include

extern "C" {
EMSCRIPTEN_KEEPALIVE uint8_t view_uint8(void* address) { return reinterpret_cast(address)[0]; }
}

int main() {
auto start = std::chrono::steady_clock::now();
size_t total = 0;
for (const char* str : { "hello", "world", "!", "test", "ing", "strings", "many", "of", "them" }) {
total += EM_ASM_INT({
var total = 0;
var value;
for (var i = 0; i < 1000000; ++i) {
value = AsciiToString($0);
total += value.length;
}
console.log(value);
return total;
}, str);
}
auto ms = std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count() ;
std::cout << total << " in " << ms << " ms " << total/ms << " /ms \n";
}
```

NO_ALLOW_MEMORY_GROWTH

```bash
~/emscripten/benchmark ((3.1.7))# em++ -Os -pthread -o -sNO_ALLOW_MEMORY_GROWTH -o benchmark.js benchmark.cpp
~/emscripten/benchmark ((3.1.7))# node --experimental-wasm-threads --experimental-wasm-bulk-memory benchmark.js
hello
world
!
test
ing
strings
many
of
them
35000000 in 293 ms 119453 /ms
```

ALLOW_MEMORY_GROWTH

```bash
~/emscripten/benchmark ((3.1.7))# em++ -Os -pthread -sALLOW_MEMORY_GROWTH -o benchmark.js benchmark.cpp
em++: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
~/emscripten/benchmark ((3.1.7))# node --experimental-wasm-threads --experimental-wasm-bulk-memory benchmark.js
hello
world
!
test
ing
strings
many
of
them
35000000 in 1328 ms 26355 /ms
```

Swapping out AsciiToString with:
```cpp
function AsciiToString(ptr){
var str="";
while(1) {
var ch=Module['_view_uint8'](ptr++>>0) >>> 0;
if(!ch) return str;
str+=String.fromCharCode(ch)
}
}
```

New Results!

```bash
~/emscripten/benchmark ((3.1.7))# node --experimental-wasm-threads --experimental-wasm-bulk-memory benchmark.js
hello
world
!
test
ing
strings
many
of
them
35000000 in 408 ms 85784 /ms
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.