- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在浏览 Rust标准库。令我震惊的是,当闭包作为参数传递给函数时,它是在运行时传递的。例如来自 Iterator trait :
fn filter<'r>(self, predicate: 'r |&A| -> bool) -> Filter<'r, A, Self>
这里的'predicate'不是泛型参数,而是一个普通的运行时参数。但这是否意味着编译器不能内联对“predicate”的调用?这是故意的设计选择[例如避免代码膨胀]还是 Rust 语言没有提供在编译时传递闭包的方法?
最佳答案
你是正确的,闭包目前是有效的特征对象,也就是说,它们存储了一个指向它们实际代码的函数指针(类似于 C++ 中的 std::function
)。
这被认为是不充分的,并且不是最终的设计,实际上当前的工作正在 "unboxed closures" 上进行,它通过使 Rust 的闭包像 C++11 的闭包一样解决这个问题,其中每个闭包都有一个实现适当的唯一类型使其可调用的方法。 (当前的 Rust 提案包含 3 个完全灵活的特征(Fn
、FnMut
和 FnOnce
),请参阅上面的 RFC 链接了解更多详情)。
在那之后,filter
可能看起来像
fn filter<'r, F: FnMut<(&A,), bool)>(self, predicate: F) -> Filter<A, Self, F>
(可能有糖分,所以绑定(bind)可以写成 F: |&A| -> bool
,甚至可以写成 more sugar ,这样就可以直接写出类似 predicate: impl |&A| -> bool
的东西,而无需额外的类型参数(尽管这不适用于 .filter
,因为它需要将类型参数传递给返回类型)。)
在这个方案下,通过与 trait 对象工作的完全相同的机制,仍然可以有一个被删除的函数类型(例如停止代码膨胀,或者在某些数据结构中存储许多不同的闭包),编写类似predicate: &mut FnMut<(&A,), bool>
,但这些不会在迭代器适配器中使用。
另外,现在 LLVM 可能内联动态闭包,但这绝对不像静态分发的未装箱闭包那样容易或保证,例如
fn main() {
for _ in range(0, 100).filter(|&x| x % 3 == 0) {
std::io::println("tick") // stop the loop being optimised away
}
}
编译为以下优化的 LLVM IR(通过 rustc --emit=ir -O
):
; Function Attrs: uwtable
define internal void @_ZN4main20h9f09eab975334327eaa4v0.0E() unnamed_addr #0 {
entry-block:
%0 = alloca %str_slice, align 8
%1 = getelementptr inbounds %str_slice* %0, i64 0, i32 0
%2 = getelementptr inbounds %str_slice* %0, i64 0, i32 1
br label %match_else.i
match_else.i: ; preds = %loop_body.i.backedge, %entry-block
%.sroa.012.0.load1624 = phi i64 [ 0, %entry-block ], [ %3, %loop_body.i.backedge ]
%3 = add i64 %.sroa.012.0.load1624, 1
%4 = srem i64 %.sroa.012.0.load1624, 3
%5 = icmp eq i64 %4, 0
br i1 %5, label %match_else, label %loop_body.i.backedge
match_else: ; preds = %match_else.i
store i8* getelementptr inbounds ([4 x i8]* @str1233, i64 0, i64 0), i8** %1, align 8
store i64 4, i64* %2, align 8
call void @_ZN2io5stdio7println20h44016c4e880db7991uk11v0.11.0.preE(%str_slice* noalias nocapture nonnull %0)
br label %loop_body.i.backedge
loop_body.i.backedge: ; preds = %match_else, %match_else.i
%exitcond = icmp eq i64 %3, 100
br i1 %exitcond, label %join4, label %match_else.i
join4: ; preds = %loop_body.i.backedge
ret void
}
特别是,过滤器调用是完全内联的:它都在 match_else.i:
block 中,您可以看到 %4 = srem i64 ..., 3
调用是 % 3
代码段,而 icmp eq i64 %4, 0
是 == 0
位。
关于parameters - Rust 标准库闭包参数 : run time or compile time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224811/
在尝试 time 的 python 执行时,我发现在一条语句中两次调用 time.time() 时出现奇怪的行为。在语句执行期间获取time.time() 有一个非常小的处理延迟。 例如time.ti
我要疯了。对于我的生活,我无法弄清楚为什么以下代码会导致 Unity 在我按下播放键后立即卡住。这是一个空的项目,脚本附加到一个空的游戏对象。在控制台中,什么也没有出现,甚至没有出现初始的 Debug
我要疯了。对于我的生活,我无法弄清楚为什么以下代码会导致 Unity 在我按下播放键后立即卡住。这是一个空的项目,脚本附加到一个空的游戏对象。在控制台中,什么也没有出现,甚至没有出现初始的 Debug
我不明白为什么下面的结果是一样的。我预计第一个结果是指针地址。 func print(t *time.Time) { fmt.Println(t) // 2009-11-10 23:00:00
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help
当我有一个time.Time时: // January, 29th t, _ := time.Parse("2006-01-02", "2016-01-29") 如何获得代表 1 月 31 日的 ti
首先,我意识到不推荐使用 time with time zone。我要使用它是因为我将多个 time with time zone 值与我当前的系统时间进行比较,而不管是哪一天。 IE。用户说每天 0
长期以来,在 Rust 中精确测量时间的标准方法是 time crate 及其 time::precise_time_ns功能。但是,time crate 现在已被弃用,std 库有 std::tim
我正在我学校的一个科学集群上运行我的有限差分程序。该程序使用 openmpi 来并行化代码。 当程序连续运行时,我得到: real 78m40.592s user 78m34.920s s
尽管它们已被弃用并且有比 time 更好的模块(即 timeit),但我想知道这两个函数 time 之间的区别.clock() 和 time.time()。 从后者 (time.time()) 开始,
这个问题在这里已经有了答案: Python's time.clock() vs. time.time() accuracy? (16 个答案) 关闭 6 年前。 我认为两者都衡量时间量?但是他们返回
我正在尝试测试 http 请求处理代码块在我的 Flask Controller 中需要多长时间,这是我使用的示例代码: cancelled = [] t0 = time.time() t1 = ti
运行 python 的计算机时钟(Windows 或 Linux)时会发生什么自动更改并调用 time.time()? 我读到,当时钟手动更改为过去的某个值时,time.time() 的值会变小。 最
我有一个结构可能无法在其字段之一上设置 time.Time 值。测试无效性时,我不能使用 nil 或 0。time.Unix(0,0) 也不相同。我想到了这个: var emptyTime time.
我有一个打算用数据库记录填充的结构,其中一个日期时间列可以为空: type Reminder struct { Id int CreatedAt time.Time
问题陈述:通过匹配其百分比随机执行各种命令。比如执行 CommandA 50% 的时间和 commandB 25% 的时间和 commandC 15% 的时间等等,总百分比应该是 100%。 我的问题
我正在使用 laravel 6。我在同一个应用程序中有类似的 Controller 和类似的 View ,它工作正常。对比之后还是找不到错误。 Facade\Ignition\Exceptions\V
我需要用 ("%m/%d/%Y %H:%M:%S") 格式表示时间,我得到的浮点值是 time.time(). 我已经有了一个 time.time() 形式的值。例如,我已经有一个值,我每 0.3 秒
我正在使用以下方法获取 utc 日期时间: import datetime import time from pytz import timezone now_utc = datetime.datet
我在 Ubuntu 上使用 time.clock 和 time.time 为一段 python 代码计时: clock elapsed time: 8.770 s time elapsed time
我是一名优秀的程序员,十分优秀!