From a62bb30b3258c1e3bbcbe945ee6a08f47b9adfd0 Mon Sep 17 00:00:00 2001 From: laizy Date: Sat, 16 Mar 2019 12:33:02 +0800 Subject: [PATCH] set stack size to reduce initial memory in generated wasm file (#38) --- README.md | 3 ++- README_CN.md | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0dc67eb..6866244 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,9 @@ pub fn invoke() { ``` 4. compile contract: +since the default stack size is 1M, which is too large for contract, we need reduce it, 32kb is enough for most usecase. ``` -cargo build --release --target wasm32-unknown-unknown +RUSTFLAGS="-C link-arg=-zstack-size=32768" cargo build --release --target wasm32-unknown-unknown ``` ## Procedural Macros diff --git a/README_CN.md b/README_CN.md index 0b65e57..b904847 100644 --- a/README_CN.md +++ b/README_CN.md @@ -41,7 +41,9 @@ pub fn invoke() { runtime::ret(b"hello, world"); } ``` -4. 合约编译:`cargo build --release --target wasm32-unknown-unknown` +4. 合约编译: +由于rustc编译默认设置的栈内存大小是1M,这对于大型软件程序来说比较合理,但对合约来说是巨大的浪费,因此在编译时设置下栈的大小,32kb对于绝大多数合约来说是够用的。 +`RUSTFLAGS="-C link-arg=-zstack-size=32768" cargo build --release --target wasm32-unknown-unknown` ## 代码生成宏 合约的编写往往是先从输入参数的字节数组中解析出具体调用的方法和调用参数,然后跳转到对应的函数中执行,最终将执行结果序列化为字节数组返回。这和web服务器从网络上获取字节流,解析出具体的请求,执行对应的处理函数并将结果序列化为字节流发送回网络的工作方式是类似的。因此可以像web开发框架一样将这样的固定又繁琐工作统一处理,使合约的开发者专注于合约本身功能的开发。`ontio_std`提供了代码生成宏,可以根据合约接口在编译期自动生成辅助性代码。采用代码生成宏的合约基本结构如下: