- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我在 Rust 中有以下结构:
struct Num {
pub num: i32;
}
impl Num {
pub fn new(x: i32) -> Num {
Num { num: x }
}
}
impl Clone for Num {
fn clone(&self) -> Num {
Num { num: self.num }
}
}
impl Copy for Num { }
impl Add<Num> for Num {
type Output = Num;
fn add(self, rhs: Num) -> Num {
Num { num: self.num + rhs.num }
}
}
然后我有以下代码片段:
let a = Num::new(0);
let b = Num::new(1);
let c = a + b;
let d = a + b;
这是有效的,因为 Num
被标记为 Copy
。否则,第二次添加将是一个编译错误,因为 a
和 b
在第一次添加时已经被移动到 add
函数中(我想想)。
问题是发出的程序集做了什么。当调用 add
函数时,是否将参数的两个副本复制到新的堆栈帧中,或者 Rust 编译器是否足够聪明,知道在这种情况下,没有必要进行该复制?
如果 Rust 编译器不够智能,并且实际上像 C++ 中带有按值传递参数的函数一样进行复制,那么在重要的情况下如何避免性能开销?
上下文是我正在实现一个矩阵类(只是为了学习),如果我有一个 100x100 矩阵,我真的不想每次尝试乘法或加法时都调用两个副本。
最佳答案
The question is what the emitted assembly does
无需猜测;你可以看看。让我们使用这段代码:
use std::ops::Add;
#[derive(Copy, Clone, Debug)]
struct Num(i32);
impl Add for Num {
type Output = Num;
fn add(self, rhs: Num) -> Num {
Num(self.0 + rhs.0)
}
}
#[inline(never)]
fn example() -> Num {
let a = Num(0);
let b = Num(1);
let c = a + b;
let d = a + b;
c + d
}
fn main() {
println!("{:?}", example());
}
将其粘贴到 Rust Playground 中,然后选择 Release 模式并查看 LLVM IR:
搜索结果以查看示例
函数的定义:
; playground::example
; Function Attrs: noinline norecurse nounwind nonlazybind readnone uwtable
define internal fastcc i32 @_ZN10playground7example17h60e923840d8c0cd0E() unnamed_addr #2 {
start:
ret i32 2
}
没错,这是在编译时进行了全面评估,并一直简化为一个简单的常量。现在的编译器非常好。
也许您想尝试一些不那么硬编码的东西?
#[inline(never)]
fn example(a: Num, b: Num) -> Num {
let c = a + b;
let d = a + b;
c + d
}
fn main() {
let something = std::env::args().count();
println!("{:?}", example(Num(something as i32), Num(1)));
}
生产
; playground::example
; Function Attrs: noinline norecurse nounwind nonlazybind readnone uwtable
define internal fastcc i32 @_ZN10playground7example17h73d4138fe5e9856fE(i32 %a) unnamed_addr #3 {
start:
%0 = shl i32 %a, 1
%1 = add i32 %0, 2
ret i32 %1
}
糟糕,编译器看到我们基本上是在做 (x + 1) * 2,所以它在这里做了一些棘手的优化以达到 2x + 2。让我们尝试更难的事情......
#[inline(never)]
fn example(a: Num, b: Num) -> Num {
a + b
}
fn main() {
let something = std::env::args().count() as i32;
let another = std::env::vars().count() as i32;
println!("{:?}", example(Num(something), Num(another)));
}
生产
; playground::example
; Function Attrs: noinline norecurse nounwind nonlazybind readnone uwtable
define internal fastcc i32 @_ZN10playground7example17h73d4138fe5e9856fE(i32 %a, i32 %b) unnamed_addr #3 {
start:
%0 = add i32 %b, %a
ret i32 %0
}
一个简单的add
指令。
真正的收获是:
is the Rust compiler smart enough to know that in this case, it's not necessary to do that copying?
正如您刚才看到的,Rust 编译器加上 LLVM 非常智能。通常,当它知道不需要操作数时,可能删除副本。它是否适用于您的情况很难回答。
即使是这样,您也可能不希望通过堆栈传递大项目,因为它总是可能需要被复制。
请注意,您不必为值实现复制,您可以选择仅通过引用允许它:
impl<'a, 'b> Add<&'b Num> for &'a Num {
type Output = Num;
fn add(self, rhs: &'b Num) -> Num {
Num(self.0 + rhs.0)
}
}
事实上,您可能想要实现添加它们的两种方式,也许还需要实现值/引用的所有 4 种排列!
关于rust - Rust 中的复制语义真的变成了内存中的副本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663478/
我已经阅读了这个答案https://stackoverflow.com/a/45486495/1108891 ,它演示了元组类型推断。经过一些实验,我遇到了这种情况。 当我们的 tuple 函数有 T
我想删除零, 我喜欢这个模型,如果我输入 1000 然后额外的表显示从 1 到 1000 的所有数字,每个数字都会检查并删除零。 示例:如果我输入 10然后输出如 1 2 3 .....8 9 1(1
鉴于我对PowerShell的了解仍在开发中,请与我一起提出任何建议/答案。 因此,在我工作的地方我们工作的公司拥有大量日文机器,需要注册Intune。但是,我们正在运行的脚本无法在其计算机上运行,
我刚刚制作了一个将路径保存到 INI 文件中的小程序。 但是在输出中,路径是这样写的: C:\\Windows 我想这样写: C:\Windows 我用 string.replace 尝试了很多方法,
所以我尝试 std::replace(diff_path.begin(), diff_path.end(), "\\", "/"); 但它似乎无法在我的 Visual Studio 上编译.怎么办 -
我使用以下代码每 30 秒自动抓取/设置最新的页面标题: setInterval(function() { var data = "http://mysite.com/mypage
我希望有两个 View 是组成集的一部分。每个 View 中的数据最好在 UITableView 中表示。然后,我想添加一个手势来使 View 在屏幕上闪烁,并引入另一个类似的 View ,并带有页面
我正在尝试开发一个小游戏,但我遇到了以下问题:我有一个伪类“Cannon”,每个 Cannon 都有一个存储它应该守卫的区域的数组和一个存储“入侵者”的数组进入其中一个戒备区。我创建了下一个函数作为
当我从应用程序中进行插入时,所有 ★(星号)都会变成“â…” 如何阻止这种情况发生? *如果我直接通过 phpmyadmin 插入它,它就可以工作,但使用这个 php 时则不行: connect_er
我遇到了一个奇怪的问题,将 NSDictionary 存储到 NSUserDefaults,然后检索它会将其转换为 NSCFString。 这是我保存数据的地方: - (void)saveProgre
我正在尝试像这样向 coinbase api 发出请求 $url = "https://api.gdax.com/products/BTC-USD/candles?start=".date($form
我在 HTTP header 中使用 if-modified-since 来决定是否应该下载文件。应用程序已经过测试,一切正常,但现在当我询问我的 NSHTTPURLResponse 实例 respo
我向串口发送0xFF 结果是 0x3F。 所有其他字节都是正确的。 情况是这样的…… 外部盒子将这些字节发送到 PC... 0xFF, 0x0D, 0x00, 0x30, 0x31, 0x53, 0x
所以我在我的 Next JS 应用程序中遇到了这个奇怪的问题,我导入了谷歌字体,如下所示 在我的浏览器中显示的不是 href,而是 data-href="...",所以问题是谷歌无法将此识别为链接
我试图通过将 QRect 变成 QPolygon 来检查 QPolygon 和 QRect 之间的碰撞。但是,矩形也可能有我添加的旋转,所以我想知道如何将 QRect 变成 QPolygon 并考虑到
我正在尝试写一个 Conduit使用 attoparsec解析器。具体来说,给定 parseOne :: Parser T , 我想构建一个 Conduit ByteString m T重复地将解析器
标记内的超链接
我正在尝试添加 和 所以实际的文字出现在我的页面上。不是链接。 所以我希望在我的页面上显示实际的 HTML,如下所示: 目前,出现了一个死图像...我想 单独阻止了这一点,只是显示了普通的html?
最近发现一些路由器设备包含后门,some of which可以通过单个UDP数据包加以利用。我意识到其中一些后门不一定是恶意的,因为我在自己的产品中也做了同样的事情以进行故障排除:打开套接字将心跳数据
我知道我可以将 iOS 设备变成 iBeacons ( Can an iOS7 device act as an iBeacon? )。不幸的是,我只有一台设备,我的信标还没有到达。所以我想知道如何将
有没有人尝试过将 MAC 变成 iBeacon。我已经为 iOS 设备完成了此操作,并且想要一个类似的带有一些 UI 的 MAC 独立应用程序。我听说 Mavericks 上的新 API 支持 iBe
我是一名优秀的程序员,十分优秀!