rust-lang / libs-team

The home of the library team
Apache License 2.0
127 stars 19 forks source link

Add methods for use cases that `align_to` does not cover #164

Closed oli-obk closed 8 months ago

oli-obk commented 1 year ago

Proposal

Problem statement

The align_to slice method is only useful for optimizations that want to process all elements of the original slice, but has an optimized path for larger or more-aligned elements. The function may fall back to not producing a middle (aligned) slice and instead just return the original slice in the head or tail slice (for which, according to align_to's docs, an implementation must exist).

There are many users in the ecosystem that use it for guaranteed alignment, which, while working most of the time, is not guaranteed by the API.

Related discussion: https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/align_to.20and.20other.20APIs.20with.20surprising.20const.20behavior

Motivation, use-cases

When you have a slice to primitives (like integers), it is sometimes convenient to convert said slice to a smaller or larger primitive. Especially common are conversions to and from [u8].

Solution sketches

Add two new slice methods (plus their mut counterparts):

Alternatives:

We can also remove the fuzziness of the align_to method, but that would mean it could never be used in const eval.

Links and related work

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

the8472 commented 1 year ago

Under which circumstances does code just ignore the head and tail? If it's about getting overaligned memory, wouldn't it be better to put it in an allocation API?

oli-obk commented 1 year ago

Essentially yes, but I believe also for scratch space on the stack (getting a [u8; 15] and finding an 8-byte aligned subslice in there and producing a &u64 to it. The first 5 search results on github for uses of align_to are ignoring the head slice:

https://github.com/PsichiX/Oxygengine/blob/adef2107dee0cb6f5d816d5dec277479e801c5e7/engine/ha-renderer-debugger/src/system.rs#L245 https://github.com/twitter/ccommon/blob/f0f44568f22cde41794ba218cad592a424696af9/rust/ccommon-sys/src/metric.rs#L42 https://github.com/akhramov/knast/blob/8c9f5f481a467a22c7cc47f11a28fe52536f9950/common_lib/src/lib.rs#L3 https://github.com/slint-ui/slint/blob/24500fbf6b7cf0529b277c7bca73357d7a5e561f/examples/opengl_underlay/main.rs#L95

the8472 commented 1 year ago

All those cases look like they go to the same or smaller alignment, so transmute_elements would work for that purpose.

aligned_subslice is about going to a higher alignment, I was asking about that case. Does anyone do that on memory they don't own? If not that maybe an allocation API would be better. Or an to_aligned::<T>() that asserts that there's no head and tail because one is slicing correctly into a prealigned buffer at the right offset and length.

ChayimFriedman2 commented 1 year ago

Maybe we can guarantee that aligned_to() will always return the biggest possible middle slice, but only not during const eval?

oli-obk commented 8 months ago

Has seen no activity in 1 year, let's take it off the T-libs pile