developmentseed / developmentseed/async-tiff
#166 always goes the slow path for most types
- Dominant language
- Rust
- Stars
- 120
- Forks
- 15
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 9
Description
bytemuck's [`try_cast_vec`](https://docs.rs/bytemuck/latest/bytemuck/allocation/fn.try_cast_vec.html) for `Vec->Vec` or any different alignment will fail, regardless of whether the _allocation_ is aligned. [It is undefined behaviour in rust to deallocate with a different alignment than the allocation](https://internals.rust-lang.org/t/globalalloc-dealloc-is-too-restrictive-provide-additional-method-with-weaker-requirements/22620/2). So [this code](https://github.com/developmentseed/async-tiff/blob/1fd22d0aab4075e604e36f1ce53c5362b5ba7905/src/array.rs#L160) will always take the second path. That could be changed to
```rust
Ok(TypedArray::UInt16(
try_cast_slice(data[..])
.map(|s| s.to_vec()) // this copy already always happens
.unwrap_or_else(
|(_, data)| {
// Fallback to manual conversion when not aligned
data.chunks_exact(2)
.map(|b| u16::from_ne_bytes([b[0], b[1]]))
.collect()
},
)))
```
which will go the first path if the allocation is aligned.
Alternatively, this copy could be avoided with some notes in #203
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.