Posted in

Swift 6 引入全新可选数据竞争安全模式_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Swift 6、Data-Race Safe Mode、Concurrent Programming、Compiler Mode、Migration Guidelines

2. 总结:Swift 6 引入新的可选数据竞争安全模式,整合了多个版本的相关特性。此模式能在编译时识别数据竞争,因可能需改现有代码所以可选。苹果还提供迁移项目的指南及应对大量警告的方法,鼓励开发者及时采用新的并发特性。

3. 主要内容:

– Swift 6 新特性

– 推出新的数据竞争安全模式,通过编译时静态检测器帮助开发无数据竞争的并发程序

– 发展历程

– 从 Swift 5.5 到 5.10 逐步引入相关特性,最终在 Swift 6 中整合为新的编译模式

– 模式特点

– 可选加入,因可能需改现有代码,开发者可自行决定启用时机和模块

– 迁移指南

– 逐个迁移模块,从依赖较少的开始,考虑有不安全全局状态或简单可发送类型的模块

– 若警告过多,可使用更细粒度的开关聚焦特定问题

– 最终建议

– 及时更新代码库,采用新的并发特性

思维导图:

文章地址:https://www.infoq.com/news/2024/06/swift-6-data-race-safety-mode/?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global

文章来源:infoq.com

作者:Sergio De Simone

发布时间:2024/6/28 0:00

语言:英文

总字数:558字

预计阅读时间:3分钟

评分:83分

标签:swift 6 数据竞争安全模式,开发,Swift,移动,苹果


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

In his WWDC 2024 talk, Apple’s Languages and Runtimes team lead and Swift core team member Ted Kremenek introduced Swift 6 new data-race safe mode, which promises to help developers create concurrent programs free of data races thanks to a new compile-time static detector.

As Kremenek explains, the road to data-race safety has been paved across several Swift versions, starting with the introduction of async/await and Actors in Swift 5.5, sendable distributed actors in Swift 5.6, custom executors and isolation assertions in Swift 5.9, and finally,full-data isolation and isolated globals in Swift 5.10.

All those features come together in a new opt-in compiler mode that enables full data-race safety. Swift 6 makes concurrent programming dramatically easier, says Kremenek, by identifying data-race conditions at compile time to prevent different parts of the code from accessing and modifying shared data.

The reason for this new mode being opt-in is that data-race safety may require changes to existing code— albeit in many cases they will be “narrow”, says Kremenek —so developers can decide when it is best to enable the new mode to tackle any data-race issues their code might have.

Alternatively, developers can opt-in to the new mode on a per-module basis, which is possible by enabling the compiler’s actor isolation and Sendable checking as warnings while still using the Swift 5 language mode.

Apple also provided a few guidelines to help developers through the process of migrating an existing project to Swift 6 and deal in an orderly way with the seemingly huge amount of warnings that the compiler may generate.

The main idea is to migrate modules one by one, starting with those that are less depended upon by other modules. This will make most of the required changes local to the module. Another useful criterion forselecting which modules to start with is considering whether any modules include unsafe global state or trivially-Sendable types, since those can cause many warnings across a project.

If the number of warnings is still too high to be tackled comfortably, the Swift compiler provides three finer-grained switches to focus on specific types of problems. You can enable them one at a time as a progressive path towards enabling complete concurrency checking. They include removing Actor Isolation Inference caused by Property Wrappers, applying strict concurrency for global variables, and inferring Sendable for methods and key path literals.

While it is well understood how global variables may be a cause of problems in a concurrent context, the other two compiler options address subtler behaviors that are specific to Swift. In particular, property wrappers may change how the Swift compiler infers isolation for the container classin a rather opaque way to the developer and this would bring unexpected potential issues (or warnings, when the static checker is enabled). Similarly, inferring sendability for partial and unapplied methods, as well as key path literals, addresses a few corner cases. You can get the full discussion in the linked Swift Evolution Proposals.

As a final note, if your Swift codebase is not yet up-to-date with the concurrency features brought by the latest language releases, now is the time to start using them. Apple suggests adopting advanced concurrency support incrementally, starting with wrapping callback-based functions to make them directly usable from an async context, adopting isolation for your classes temporarily using internal-only isolation, and so on.