leaningtech / cheerp-meta

Cheerp - a C/C++ compiler for Web applications - compiles to WebAssembly and JavaScript
https://labs.leaningtech.com/cheerp
Other
1.02k stars 50 forks source link

What does the `cheerp-linear-heap-size` argument do? #155

Closed raulpy271 closed 1 year ago

raulpy271 commented 1 year ago

I expected the argument cheerp-linear-heap-size to especify the initial heap size to give users control to define how much memory is allocated when starting execution. However, I tried to use the argument and the result was different from what I expected.

For exemple, I created a simple "Hello World" program test.c, and compilled it with cheerp-linear-heap-size=100:

/opt/cheerp/bin/clang -cheerp-linear-heap-size=100 -cheerp-pretty-code test.c -o test.js

After it, I inserted the statement console.log(__heap.byteLength); inside the function _start in the test.js file. This statement will print the initial size of the heap. The result is as fallows:

$ node test.js
1114112
hello world

The second line is the size of the heap, which is a little more than 1MB, I expected to be 100MB because I used cheerp-linear-heap-size=100. I want to understand why I got a different result and what does the cheerp-linear-heap-size argument do.

About my system:

$ uname -a
Linux raul 5.15.0-78-generic #85~20.04.1-Ubuntu SMP Mon Jul 17 09:42:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ /opt/cheerp/bin/clang --version
Cheerp 3.0-1~focal clang version 16.0.0
Target: cheerp-leaningtech-webbrowser-wasm
Thread model: posix
InstalledDir: /opt/cheerp/bin

Content of test.c:

#include <stdio.h>

int main() {
    printf("hello world\n");
    return 0;
}
alexp-sssup commented 1 year ago

-cheerp-linear-heap-size= defines the maximum size of the heap. The size is grown dynamically as required up to the provided size. The initial allocation will be as small as possible.

For further support consider joining our Discord: https://discord.gg/3UfTSbWdYy