gpt4 book ai didi

f# - 单线程程序在 semaphore_wait_trap 中分析 15% 的运行时间

转载 作者:行者123 更新时间:2023-12-04 15:44:22 32 4
gpt4 key购买 nike

在使用单声道的 Mac OS 上,如果我编译和分析下面的程序,我会得到以下结果:

% fsharpc --nologo -g foo.fs -o foo.exe
% mono --profile=default:stat foo.exe
...
Statistical samples summary
Sample type: cycles
Unmanaged hits: 336 (49.1%)
Managed hits: 349 (50.9%)
Unresolved hits: 1 ( 0.1%)
Hits % Method name
154 22.48 Microsoft.FSharp.Collections.SetTreeModule:height ...
105 15.33 semaphore_wait_trap
74 10.80 Microsoft.FSharp.Collections.SetTreeModule:add ...
...

注意第二个条目, semaphore_wait_trap .
这是程序:
[<EntryPoint>]
let main args =
let s = seq { 1..1000000 } |> Set.ofSeq
s |> Seq.iter (fun _ -> ())
0

我看了 source for the Set module ,但我没有发现任何(明显的)锁定。

我的单线程程序是否真的花费了 15% 的执行时间来处理信号量?如果是,我可以让它不这样做并获得性能提升吗?

最佳答案

根据 Instruments,它是 sgen/gc 调用 semaphore_wait_trap:

enter image description here

Sgen is documented在收集时停止所有其他线程:

Before doing a collection (minor or major), the collector must stop all running threads so that it can have a stable view of the current state of the heap, without the other threads changing it



换句话说,当代码尝试分配内存并且需要 GC 时,它花费的时间会显示在 semaphore_wait_trap 下,因为那是您的应用程序线程。我怀疑单声道分析器不会分析 gc 线程本身,因此您看不到收集代码中的时间。

密切相关的输出实际上是 GC 摘要:
GC summary
GC resizes: 0
Max heap size: 0
Object moves: 1002691
Gen0 collections: 123, max time: 14187us, total time: 354803us, average: 2884us
Gen1 collections: 3, max time: 41336us, total time: 60281us, average: 20093us

如果您希望代码运行得更快,请不要经常收集。

sgen has dtrace probes开始,可以通过dtrace了解实际的收集成本。 .

关于f# - 单线程程序在 semaphore_wait_trap 中分析 15% 的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021730/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com