Skip to content

Commit

Permalink
update doc of compile options
Browse files Browse the repository at this point in the history
  • Loading branch information
vita-dounai committed Sep 22, 2020
1 parent 1da9d23 commit 45dabcd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/in_depth/compile_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 编译选项

若您仔细研究过Liquid项目根目录下的`Cargo.toml`文件,您可能会注意到该配置文件中有一个特殊的节(section)——`[profile.release]`,此节存放了合约的编译及优化配置:

```toml
[profile.release]
panic = "abort"
lto = true
opt-level = "z"
overflow-checks = true
```

其中:

- `panic = "abort"`,当发生panic时,Rust程序的默认行为是执行堆栈展开,用于执行各个栈上对象的析构函数。此处用于更改此默认行为,使得当panic发生时,合约直接终止,而无需执行堆栈展开的代码,有助于减少合约字节码的体积。由于合约在虚拟机中执行,当合约执行终止时,所有的资源都将会被宿主环境回收,因此无需担心资源泄露的问题。

- `lto = true`,用于开启链接时优化(Link Time Optimization)。开启后链接器将会对整个合约进行分析及跨模块的优化,有助于减少合约字节码的体积。

- `opt-level = "z"`,用于指定编译器的优化等级,`z`级别的优化将会更专注于缩小字节码的体积。

- `overflow-checks = true`,用于开启算数溢出检查。开启后,Rust编译器会在代码中每一处执行算数运算的地方插入溢出检查代码。当合约的运算过程中出现算数溢出时,会直接引发panic。关闭该选项能够获得更快的执行速度和更小的字节码体积,但是也会让您的合约处于风险之中。

您可以根据自身的需求调整这些编译配置,但是调整之前您也必须明确调整后可能会造成的后果。一般而言,默认的编译配置已经足够应付大部分场景。
1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Liquid是一种专用于为区块链底层平台[FISCO BCOS](https://fisco-bcos-
docs/in_depth/method.md
docs/in_depth/event.md
docs/in_depth/types.md
docs/in_depth/compile_options.md
.. toctree::
:hidden:
Expand Down

0 comments on commit 45dabcd

Please sign in to comment.