From 7ddfcdb2344d836a20256a5d16b725fb483c31fc Mon Sep 17 00:00:00 2001 From: sunface Date: Fri, 25 Feb 2022 10:32:56 +0800 Subject: [PATCH] add profile resources --- contents/SUMMARY.md | 5 +- contents/appendix/rust-versions/1.58.md | 2 +- contents/appendix/rust-versions/1.59.md | 101 ++++++++++++++++++++ contents/profiling/performance/allocator.md | 4 +- contents/profiling/performance/benchmark.md | 1 - contents/profiling/performance/string.md | 1 + 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 contents/appendix/rust-versions/1.59.md delete mode 100644 contents/profiling/performance/benchmark.md create mode 100644 contents/profiling/performance/string.md diff --git a/contents/SUMMARY.md b/contents/SUMMARY.md index 9aa9322f..4147c7af 100644 --- a/contents/SUMMARY.md +++ b/contents/SUMMARY.md @@ -203,10 +203,10 @@ - [内存布局 todo](profiling/memory/layout.md) - [虚拟内存 todo](profiling/memory/virtual.md) - [性能调优 doing](profiling/performance/intro.md) + - [字符串操作性能](profiling/performance/string.md) - [深入理解move](profiling/performance/deep-into-move.md) - [糟糕的提前优化 todo](profiling/performance/early-optimise.md) - [Clone和Copy todo](profiling/performance/clone-copy.md) - - [Benchmark性能测试(todo)](profiling/profiling/performance/benchmark.md) - [减少Runtime check(todo)](profiling/performance/runtime-check.md) - [CPU缓存性能优化 todo](profiling/performance/cpu-cache.md) - [计算性能优化 todo](profiling/performance/calculate.md) @@ -250,4 +250,5 @@ - [F-难点索引](appendix/difficulties.md) - [G-Rust版本说明](appendix/rust-version.md) - [H-Rust更新版本列表](appendix/rust-versions/intro.md) - - [1.58](appendix/rust-versions/1.58.md) \ No newline at end of file + - [1.58](appendix/rust-versions/1.58.md) + - [1.59](appendix/rust-versions/1.59.md) \ No newline at end of file diff --git a/contents/appendix/rust-versions/1.58.md b/contents/appendix/rust-versions/1.58.md index d17fe127..9fc93229 100644 --- a/contents/appendix/rust-versions/1.58.md +++ b/contents/appendix/rust-versions/1.58.md @@ -1,4 +1,4 @@ -# 1.58 +# Rust新版解读 | 1.58 | 重点: 格式化字符串捕获环境中的值 众所周知,Rust小版本发布非常频繁,6周就发布一次,因此通常不会有特别值得普通用户关注的内容,但是这次1.58版本不同,新增了(stable化了)一个非常好用的功能:在格式化字符串时捕获环境中的值。 diff --git a/contents/appendix/rust-versions/1.59.md b/contents/appendix/rust-versions/1.59.md new file mode 100644 index 00000000..14350428 --- /dev/null +++ b/contents/appendix/rust-versions/1.59.md @@ -0,0 +1,101 @@ +# Rust新版解读 | 1.59 | 重点: 内联汇编、解构式赋值 +Rust 团队于今天凌晨( 2022-02-25 )发布了最新的 1.59 版本,其中最引人瞩目的特性应该就是支持在代码中内联汇编了,一起来看看。 + +## 内联汇编( inline assembly ) +该特性对于需要底层控制的应用非常有用,例如想要控制底层执行、访问特定的机器指令等。 + +例如,如果目标平台是 `x86-64` 时,你可以这么写: +```rust +use std::arch::asm; + +// 使用 shifts 和 adds 实现 x 乘以 6 +let mut x: u64 = 4; +unsafe {`` + asm!( + "mov {tmp}, {x}", + "shl {tmp}, 1", + "shl {x}, 2", + "add {x}, {tmp}", + x = inout(reg) x, + tmp = out(reg) _, + ); +} +assert_eq!(x, 4 * 6); +``` + +大家发现没,这里的格式化字符串的使用方式跟我们平时的 `println!`、`format!` 并无区别, 除了 `asm!` 之外, `global_asm!` 宏也可以这么使用。 + +内联汇编中使用的汇编语言和指令取决于相应的机器平台,截至目前,Rust 支持以下平台的内联汇编: + +- x86 和 x86-64 +- ARM +- AArch64 +- RISC-V + +如果大家希望深入了解,可以看官方的 [Reference](https://doc.rust-lang.org/nightly/reference/inline-assembly.html) 文档,同时在 [Rust Exercise](https://zh.exercise.rs/unsafe/inline-asm) 中提供了更多的示例(目前正在翻译中..)。 + +## 解构式赋值( Destructuring assignments) +现在你可以在赋值语句的左式中使用元组、切片和结构体模式了。 +```rust +let (a, b, c, d, e); + +(a, b) = (1, 2); +[c, .., d, _] = [1, 2, 3, 4, 5]; +Struct { e, .. } = Struct { e: 5, f: 3 }; + +assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]); +``` + +这种使用方式跟 `let` 保持了一致性,但是需要注意,使用 `+=` 的赋值语句还不支持解构式赋值。 + +## const 泛型 + +#### 为参数设置默认值 +现在我们可以为 const 泛型参数设置默认值: +```rust +struct ArrayStorage { + arr: [T; N], +} + +impl ArrayStorage { + fn new(a: T, b: T) -> ArrayStorage { + ArrayStorage { + arr: [a, b], + } + } +} +``` + +#### 取消参数顺序的限制 +在之前版本中,类型参数必须要在所有的 const 泛型参数之前,现在,这个限制被放宽了,例如你可以这样交替排列它们: +```rust +fn cartesian_product< + T, const N: usize, + U, const M: usize, + V, F +>(a: [T; N], b: [U; M], f: F) -> [[V; N]; M] +where + F: FnMut(&T, &U) -> V +{ + // ... +} +``` + +## 减少二进制文件体积:删除 debug 信息 +对于受限的环境来说,减少编译出的二进制文件体积是非常重要的。 + +在之前,我们可以在二进制文件创建后,手动的来完成。现在 cargo 和 rustc 支持在链接( linked )后就删除 debug 信息,在 `Cargo.toml` 中新增以下配置: +```toml +[profile.release] +strip = "debuginfo" +``` + +以上配置会将 `release` 二进制文件中的 debug 信息移除。你也可以使用 `"symbols"` 或 `true` 选项来移除所有支持的 `symbol` 信息。 + +## 默认关闭增量编译 +1.59.0 版本默认关闭了增量编译的功能(你可以通过环境变量显式地启用:`RUSTC_FORCE_INCREMENTAL=1` ),这会降低已知bug [#94124](https://github.com/rust-lang/rust/issues/94124) 的影响,该 bug 会导致增量编译过程中的反序列化的误和 panic。 + +不过大家也不用担心,这个 bug 会在 1.60.0 版本修复,也就是六周后,增量编译会重新设置为默认开启,如果没有意外的话 :) + +## 稳定化的 API 列表 +一些方法和特征实现现在已经可以 stable 中使用,具体见[官方发布说明](https://blog.rust-lang.org/2022/02/24/Rust-1.59.0.html#stabilized-apis) \ No newline at end of file diff --git a/contents/profiling/performance/allocator.md b/contents/profiling/performance/allocator.md index bab9e468..4d1fad14 100644 --- a/contents/profiling/performance/allocator.md +++ b/contents/profiling/performance/allocator.md @@ -1,3 +1,5 @@ # 内存allocator todo -https://www.reddit.com/r/rust/comments/s28g4x/allocating_many_boxes_at_once/ \ No newline at end of file +https://www.reddit.com/r/rust/comments/s28g4x/allocating_many_boxes_at_once/ + +https://www.reddit.com/r/rust/comments/szza43/memory_freed_but_not_immediately/ \ No newline at end of file diff --git a/contents/profiling/performance/benchmark.md b/contents/profiling/performance/benchmark.md deleted file mode 100644 index 0ae35de8..00000000 --- a/contents/profiling/performance/benchmark.md +++ /dev/null @@ -1 +0,0 @@ -# benchmark \ No newline at end of file diff --git a/contents/profiling/performance/string.md b/contents/profiling/performance/string.md new file mode 100644 index 00000000..3e92bab2 --- /dev/null +++ b/contents/profiling/performance/string.md @@ -0,0 +1 @@ +https://www.reddit.com/r/rust/comments/t06hk7/string_concatenations_benchmarks_updated/ \ No newline at end of file