Skip to content

Commit ff35ce8

Browse files
Typos and incorporated changes.
1 parent 0274a17 commit ff35ce8

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

rust/arrow/README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ Generally, `unsafe` should only be used when a `safe` counterpart is not availab
116116
#### cache-line aligned memory management
117117

118118
The arrow format recommends storing buffers aligned with cache lines, and this crate adopts this behavior.
119-
However, Rust's global allocator does not allocates memory aligned with cache-lines. As such, many of the low-level operations related to memory management require `unsafe`.
119+
However, Rust's global allocator does not allocate memory aligned with cache-lines. As such, many of the low-level operations related to memory management require `unsafe`.
120120

121121
#### Interpreting bytes
122122

123123
The arrow format is specified in bytes (`u8`), which can be logically represented as certain types
124-
depending on the DataType.
124+
depending on the `DataType`.
125125
For many operations, such as access, representation, numerical computation and string manipulation,
126126
it is often necessary to interpret bytes as other physical types (e.g. `i32`).
127127

@@ -131,7 +131,7 @@ Usage of `unsafe` for the purpose of interpreting bytes in their corresponding t
131131

132132
The arrow format declares an ABI for zero-copy from and to libraries that implement the specification
133133
(foreign interfaces). In Rust, receiving and sending pointers via FFI requires usage of `unsafe` due to
134-
the impossibility of the compiler to derive the invariants (such as lifetime, null pointers, and pointer alignment), from the source code alone as they are part of the FFI contract.
134+
the impossibility of the compiler to derive the invariants (such as lifetime, null pointers, and pointer alignment) from the source code alone as they are part of the FFI contract.
135135

136136
#### IPC
137137

@@ -150,17 +150,31 @@ This requires accessing the values buffer e.g. `array.buffers()[0]`, picking the
150150
`[i * size_of<i32>(), (i + 1) * size_of<i32>()]`, and then transmuting it to `i32`. In safe Rust,
151151
this operation requires boundary checks that are detrimental to performance.
152152

153-
Usage of `unsafe` for performance reasons is justified only when the performance difference of a publicly available API is estatistically significantly larger than 10%, as demonstrated by a `bench`.
153+
Usage of `unsafe` for performance reasons is justified only when all other alternatives have been exhausted and the performance benefits are sufficiently large (e.g. >~10%).
154154

155155
### Considerations when introducing `unsafe`
156156

157157
Usage of `unsafe` in this crate *must*:
158158

159159
* not expose a public API as `safe` when there are necessary invariants for that API to be defined behavior.
160-
* have code documentation for why `safe` is not used / possible (e.g. `// 30% performance degradation if the safe counterpart is used, see bench X`)
161-
* have code documentation about which invariant the user needs to enforce to ensure no undefined behavior (e.g. `// this buffer must be constructed according to the arrow specification`)
160+
* have code documentation for why `safe` is not used / possible
161+
* have code documentation about which invariant the user needs to enforce to ensure [soundness](https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library), or which
162+
* invariant is being preserved.
162163
* if applicable, use `debug_assert`s to relevant invariants (e.g. bound checks)
163164

165+
Example of code documentation:
166+
167+
```rust
168+
// JUSTIFICATION
169+
// Benefit
170+
// Describe the benefit of using unsafe. E.g.
171+
// "30% performance degradation if the safe counterpart is used, see bench X."
172+
// Soundness
173+
// Describe why the code remains sound (according to the definition of rust's unsafe code guidelines). E.g.
174+
// "We bounded check these values at initialization and the array is immutable."
175+
let ... = unsafe { ... };
176+
```
177+
164178
# Publishing to crates.io
165179

166180
An Arrow committer can publish this crate after an official project release has

0 commit comments

Comments
 (0)