Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1325: Translation for MemorySafety.md⭐️⭐️⭐️⭐️⭐️ #1383

Open
wants to merge 3 commits into
base: swift-6-beta-translation
Choose a base branch
from

Conversation

NSCruiser
Copy link
Collaborator

#close: #1325

@NSCruiser NSCruiser linked an issue Sep 9, 2024 that may be closed by this pull request
@yongfrank yongfrank changed the title Translation for MemorySafety.md 1325: Translation for MemorySafety.md⭐️⭐️⭐️⭐️⭐️ Sep 22, 2024
@pujiaxin33 pujiaxin33 self-requested a review September 27, 2024 01:47
or pass an argument to a function.
For example,
the following code contains both a read access and a write access:
在做为变量赋值、给函数传参这样的事情时,你的代码会访问内存。举个例子,下面的代码包含了一次读操作和一次写操作:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在做为变量赋值、给函数传参这样的事情时,你的代码会访问内存。
建议优化为:
内存的访问会发生在你给变量赋值时,或者传递参数给函数时。

you can read any information from the budget
and get a correct answer,
as shown in the figure below.
当代码中的不同部分试图访问同一块内存区域时,访问冲突就有可能出现。对一块内存区域的同时访问可能导致程序出现无法预测或不稳定的行为。Swift 中有许多修改数值的方式,其中一些会横跨多行代码,这意味着修改某个数值的过程本身也有可能产生对此数值的访问。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当代码中的不同部分试图访问同一块内存区域时,访问冲突就有可能出现。
建议优化为:
当多个不同地方的代码同时试图访问同一块内存区域时,访问冲突就有可能出现。

> the conflicting access discussed here can happen
> on a single thread and
> *doesn't* involve concurrent or multithreaded code.
但是,在你向预算表添加项目的过程中,它会短暂地处于一个临时、不合法的状态,因为预算总额此时还没有被更新以反映这些新添加的项目。在项目添加的过程中读取到的总额是不正确的。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不合法的状态
建议优化为:
错误的状态

For a list of those functions, see the `stdatomic(3)` man page.
### 内存访问的特点

在访问冲突的语境下,我们需要考虑内存访问的三个特点:此次访问是读还是写、访问的时长、被访问内存区域的位置。特别地,两次内存访问会在满足以下所有条件时产生冲突:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特别地,两次内存访问会在满足以下所有条件时产生冲突:
建议优化为:
特别是两次内存访问会在满足以下所有条件时产生冲突:

of the same function
produces a conflict.
For example:
对于 in-out 参数保持长期写访问的另一个后果是,往同一个函数的多个 in-out 参数里传入同一个变量也会产生冲突。例如:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文章中的“长期访问”和“长时访问”建议统一用一个术语。

they access different locations in memory.
Even though the two write accesses overlap in time,
they don't conflict.
在上面的例子中,通过调用 `shareHealth(with:)` 方法来将 Oscar 的生命值分享给 Maria 并不会造成冲突。因为 `oscar` 是变值方法中 `self` 所对应的值,所以方法执行过程存在对于 `oscar` 的写访问;在相同的时间窗口内,方法对于 `maria` 也会有写访问,因为 `maria` 是作为 in-out 参数传入的。尽管这两次写访问在时间上发生了重叠,它们并不冲突。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为 oscar 是变值方法中 self 所对应的值
建议优化为:
因为 oscar 是 mutating 方法中 self 所对应的值

For example,
overlapping write accesses to the elements of a tuple
produces a conflict:
结构体、元组、枚举这样的类型是由多个独立的值构成的(例如结构题的属性、元组的元素)。它们都是值类型,所以修改值的任何一部分都是对于整个值的修改。这意味着对于其中任何一个属性的读或写访问,都需要对于整个值的访问。例如,对于元组元素的重叠写访问会造成冲突:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例如结构题的属性
更正为:
例如结构体的属性

for overlapping write accesses
to the properties of a structure
that's stored in a global variable.
在上方的示例中,因为出现了对于 `playerInformation` 的重叠写访问,对元组中的元素调用 `balance(_:_:)` 就会产生冲突。`playerInformation.health` 和 `playerInformation.energy` 都被作为 in-out 参数被传入了,这意味着 `balance(_:_:)` 在执行期间需要保持对它们的写访问,而这又意味着 `balance(_:_:)` 需要保持两次对 `playerInformation` 整体的重叠写访问,这样一来就发生了访问冲突。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在上方的示例中
建议优化为:
在上面的示例中

is changed to a local variable instead of a global variable,
the compiler can prove that overlapping access
to stored properties of the structure is safe:
在实践中,大多数时候对于结构体属性的重叠访问是安全的。举个例子,如果上方例子中的变量 `holly` 是一个本地变量而非全局变量,编译器可以「证明」对于该结构体的属性的重叠访问是安全的:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「证明」
建议去掉两边的符号
证明

it doesn't allow the access.
在上方的例子中,Oscar 的生命值和能量值被作为两个 in-out 参数传递给了 `balance(_:_:)`。此时编译器能够证明内存是安全的,因为这两个属性并不会以任何方式交互。

要保证内存安全,限制结构体属性的重叠访问并不总是必要的。内存安全性是我们想获得的一种保证,但是「独占访问」是相比「内存安全」更加严格的一种要求 —— 这意味着即使有些代码违反了「独占访问」的原则,它也可以是内存安全的。只要编译器可以「证明」这种非专属的访问是内存安全的,Swift 就会允许这样的代码存在。特别地,在以下条件满足时,编译器就可以证明对结构体属性的重叠访问是安全的:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特别地,
建议优化为:
特别是

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Under Review
Development

Successfully merging this pull request may close these issues.

LanguageGuide / memory-safety.md ⭐️⭐️⭐️⭐️⭐️
3 participants