-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1da9d23
commit 45dabcd
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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。关闭该选项能够获得更快的执行速度和更小的字节码体积,但是也会让您的合约处于风险之中。 | ||
|
||
您可以根据自身的需求调整这些编译配置,但是调整之前您也必须明确调整后可能会造成的后果。一般而言,默认的编译配置已经足够应付大部分场景。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters