axiom-concurrency-profiling by charleswiltgen/axiom
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-concurrency-profiling使用 Instruments 分析和优化 Swift async/await 代码。
✅ 适合使用场景:
❌ 不适合使用场景:
| 轨道 | 信息 |
|---|---|
| Swift Tasks | 任务生命周期、父子关系 |
| Swift Actors | Actor 访问、争用可视化 |
| Thread States | 阻塞 vs 运行 vs 挂起 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
症状 : UI 冻结,主线程时间线占满
解决方案模式 :
// ❌ 在 MainActor 上进行繁重工作
@MainActor
class ViewModel: ObservableObject {
func process() {
let result = heavyComputation() // 阻塞 UI
self.data = result
}
}
// ✅ 卸载繁重工作
@MainActor
class ViewModel: ObservableObject {
func process() async {
let result = await Task.detached {
heavyComputation()
}.value
self.data = result
}
}
症状 : 任务意外串行化,并行工作顺序运行
解决方案模式 :
// ❌ 所有工作都通过 actor 串行化
actor DataProcessor {
func process(_ data: Data) -> Result {
heavyProcessing(data) // 所有调用者等待
}
}
// ✅ 将繁重工作标记为 nonisolated
actor DataProcessor {
nonisolated func process(_ data: Data) -> Result {
heavyProcessing(data) // 并行运行
}
func storeResult(_ result: Result) {
// 仅 actor 状态访问被串行化
}
}
更多修复方法 :
症状 : 任务已排队但未执行,任务执行中存在间隙
原因 : 阻塞调用耗尽了协作池
常见问题 :
// ❌ 阻塞协作线程
Task {
semaphore.wait() // 绝对不要这样做
// ...
semaphore.signal()
}
// ❌ 在异步上下文中进行同步文件 I/O
Task {
let data = Data(contentsOf: fileURL) // 阻塞
}
// ✅ 使用异步 API
Task {
let (data, _) = try await URLSession.shared.data(from: fileURL)
}
调试标志 :
SWIFT_CONCURRENCY_COOPERATIVE_THREAD_BOUNDS=1
检测异步上下文中的不安全阻塞。
症状 : 高优先级任务等待低优先级任务
// ✅ 为关键工作明确指定优先级
Task(priority: .userInitiated) {
await criticalUIUpdate()
}
Swift 使用与 CPU 核心数匹配的协作线程池:
| 方面 | GCD | Swift Concurrency |
|---|---|---|
| 线程数 | 无限增长 | 固定为核心数 |
| 阻塞 | 创建新线程 | 挂起,释放线程 |
| 依赖关系 | 隐藏 | 运行时跟踪 |
| 上下文切换 | 完整内核切换 | 轻量级延续 |
为什么阻塞是灾难性的 :
首先运行这些检查:
工作是否真正异步?
await)是否在 await 期间持有锁?
// ❌ 死锁风险
mutex.withLock {
await something() // 绝对不要!
}
任务在紧密循环中?
// ❌ 开销可能超过收益
for item in items {
Task { process(item) }
}
// ✅ 结构化并发
await withTaskGroup(of: Void.self) { group in
for item in items {
group.addTask { process(item) }
}
}
异步上下文中有 DispatchSemaphore?
withCheckedContinuation| 问题 | Instruments 中的症状 | 修复方法 |
|---|---|---|
| MainActor 过载 | 主线程上长蓝色条 | Task.detached, nonisolated |
| Actor 争用 | 高红蓝比 | 拆分 actors,使用 nonisolated |
| 线程耗尽 | 所有线程中存在间隙 | 移除阻塞调用 |
| 优先级反转 | 高优先级等待低优先级 | 检查任务优先级 |
| 任务过多 | 任务创建开销 | 使用任务组 |
与协作池安全 :
await, actors, task groupsos_unfair_lock, NSLock(短临界区)Mutex (iOS 18+)不安全(违反前进保证) :
DispatchSemaphore.wait()pthread_cond_waitThread.sleep()WWDC : 2022-110350, 2021-10254
文档 : /xcode/improving-app-responsiveness
技能 : axiom-swift-concurrency, axiom-performance-profiling, axiom-synchronization, axiom-lldb(交互式线程状态检查)
每周安装数
92
仓库
GitHub 星标数
601
首次出现
2026年1月21日
安全审计
安装于
opencode78
claude-code73
codex72
gemini-cli71
cursor71
github-copilot68
Profile and optimize Swift async/await code using Instruments.
✅ Use when:
❌ Don't use when:
| Track | Information |
|---|---|
| Swift Tasks | Task lifetimes, parent-child relationships |
| Swift Actors | Actor access, contention visualization |
| Thread States | Blocked vs running vs suspended |
Symptom : UI freezes, main thread timeline full
Solution patterns :
// ❌ Heavy work on MainActor
@MainActor
class ViewModel: ObservableObject {
func process() {
let result = heavyComputation() // Blocks UI
self.data = result
}
}
// ✅ Offload heavy work
@MainActor
class ViewModel: ObservableObject {
func process() async {
let result = await Task.detached {
heavyComputation()
}.value
self.data = result
}
}
Symptom : Tasks serializing unexpectedly, parallel work running sequentially
Solution patterns :
// ❌ All work serialized through actor
actor DataProcessor {
func process(_ data: Data) -> Result {
heavyProcessing(data) // All callers wait
}
}
// ✅ Mark heavy work as nonisolated
actor DataProcessor {
nonisolated func process(_ data: Data) -> Result {
heavyProcessing(data) // Runs in parallel
}
func storeResult(_ result: Result) {
// Only actor state access serialized
}
}
More fixes :
Symptom : Tasks queued but not executing, gaps in task execution
Cause : Blocking calls exhaust cooperative pool
Common culprits :
// ❌ Blocks cooperative thread
Task {
semaphore.wait() // NEVER do this
// ...
semaphore.signal()
}
// ❌ Synchronous file I/O in async context
Task {
let data = Data(contentsOf: fileURL) // Blocks
}
// ✅ Use async APIs
Task {
let (data, _) = try await URLSession.shared.data(from: fileURL)
}
Debug flag :
SWIFT_CONCURRENCY_COOPERATIVE_THREAD_BOUNDS=1
Detects unsafe blocking in async context.
Symptom : High-priority task waits for low-priority
// ✅ Explicit priority for critical work
Task(priority: .userInitiated) {
await criticalUIUpdate()
}
Swift uses a cooperative thread pool matching CPU core count:
| Aspect | GCD | Swift Concurrency |
|---|---|---|
| Threads | Grows unbounded | Fixed to core count |
| Blocking | Creates new threads | Suspends, frees thread |
| Dependencies | Hidden | Runtime-tracked |
| Context switch | Full kernel switch | Lightweight continuation |
Why blocking is catastrophic :
Run these checks first:
Is work actually async?
await)Holding locks across await?
// ❌ Deadlock risk
mutex.withLock {
await something() // Never!
}
Tasks in tight loops?
// ❌ Overhead may exceed benefit
for item in items {
Task { process(item) }
}
// ✅ Structured concurrency
await withTaskGroup(of: Void.self) { group in
for item in items {
group.addTask { process(item) }
}
}
DispatchSemaphore in async context?
withCheckedContinuation instead| Issue | Symptom in Instruments | Fix |
|---|---|---|
| MainActor overload | Long blue bars on main | Task.detached, nonisolated |
| Actor contention | High red:blue ratio | Split actors, use nonisolated |
| Thread exhaustion | Gaps in all threads | Remove blocking calls |
| Priority inversion | High-pri waits for low-pri | Check task priorities |
| Too many tasks | Task creation overhead | Use task groups |
Safe with cooperative pool :
await, actors, task groupsos_unfair_lock, NSLock (short critical sections)Mutex (iOS 18+)Unsafe (violate forward progress) :
DispatchSemaphore.wait()pthread_cond_waitThread.sleep() in TaskWWDC : 2022-110350, 2021-10254
Docs : /xcode/improving-app-responsiveness
Skills : axiom-swift-concurrency, axiom-performance-profiling, axiom-synchronization, axiom-lldb (interactive thread state inspection)
Weekly Installs
92
Repository
GitHub Stars
601
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode78
claude-code73
codex72
gemini-cli71
cursor71
github-copilot68
ESLint迁移到Oxlint完整指南:JavaScript/TypeScript项目性能优化工具
1,600 周安装