Unverified Commit cde6fad2 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

refactor(turbo-rcstr): drop unsupported 16-byte pointer width (#98355)

## What?

Removes turbo-rcstr's 16-byte/128-bit pointer representation and the
unused `atom_size_64` feature plumbing. Unsupported pointer widths now
fail with an error that names the supported 32-bit and 64-bit widths.

## Why?

This follows up on review feedback from #97577. The previous negative
cfg made unknown pointer widths fall through to the 64-bit
representation, while the 16-bit implementation and `atom_size_64`
features had no users. Making both supported widths explicit keeps
representation assumptions visible and produces a useful failure for
unsupported targets.

## How?

The narrow tagged-value layout is now specific to 32-bit targets, and
the pointer-sized layout is specific to 64-bit targets. `atom_size_128`
remains unchanged. The dead feature declarations are removed from
turbo-rcstr and its proc-macro crate.

## Verification

- `cargo test -p turbo-rcstr --lib`
- `cargo test -p turbo-rcstr --lib --target wasm32-wasip1-threads`
- `cargo check -p next-napi-bindings`
- `cargo check -p next-napi-bindings --target wasm32-wasip1-threads`
- `cargo clippy -p turbo-rcstr -p turbo-rcstr-macros -- -D warnings`
- `cargo clippy -p turbo-rcstr -p turbo-rcstr-macros --target
wasm32-wasip1-threads -- -D warnings`
- `cargo fmt --all -- --check`

<!-- NEXT_JS_LLM -->

<!-- fleet 7cfc7d6e-efc8-4ef3-b81c-b90f71291bb0 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent d940cd30
......@@ -9,11 +9,10 @@ proc-macro = true
bench = false
[features]
# Mirror the threshold-affecting features of `turbo-rcstr`. The consuming
# crate routes them via `turbo-rcstr/atom_size_*` -> `turbo-rcstr-macros/atom_size_*`
# Mirror the threshold-affecting feature of `turbo-rcstr`. The consuming crate
# routes it via `turbo-rcstr/atom_size_128` -> `turbo-rcstr-macros/atom_size_128`
# so the proc macro can compute the correct `MAX_INLINE_LEN` at expansion time
# instead of leaving an ambiguous range that needs a runtime const branch.
atom_size_64 = []
atom_size_128 = []
[lints]
......
......@@ -5,7 +5,6 @@ edition = "2024"
license = "MIT"
[features]
atom_size_64 = ["turbo-rcstr-macros/atom_size_64"]
atom_size_128 = ["turbo-rcstr-macros/atom_size_128"]
napi = ["dep:napi"]
......
......@@ -37,18 +37,16 @@
//! Reading the value back as an integer (`get_ptr`, `get_value`, `tag_byte`) only ever happens at
//! run time, where pointer → integer is perfectly legal.
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_64",
feature = "atom_size_128"
)))]
#[cfg(not(any(target_pointer_width = "32", feature = "atom_size_128")))]
use std::num::NonZeroUsize;
use std::{num::NonZeroU8, os::raw::c_void, ptr::NonNull, slice};
use self::raw_types::*;
use crate::TAG_MASK;
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("turbo-rcstr only supports 32-bit and 64-bit pointer widths");
#[cfg(feature = "atom_size_128")]
mod raw_types {
pub type RawTaggedValue = u128;
......@@ -57,26 +55,20 @@ mod raw_types {
/// The narrow-pointer arm: an 8-byte value built from a real pointer plus a byte payload, so that
/// both constructors stay const-evaluable. See the module docs.
#[cfg(all(
any(target_pointer_width = "32", target_pointer_width = "16"),
not(feature = "atom_size_128")
))]
#[cfg(all(target_pointer_width = "32", not(feature = "atom_size_128")))]
mod raw_types {
use std::ptr::NonNull;
pub type RawTaggedValue = u64;
/// Padding that brings the value up to 8 bytes, sized so that there is none left over.
#[cfg(target_pointer_width = "32")]
/// Padding that brings the value up to 8 bytes without leaving any implicit padding.
pub type Payload = [u8; 4];
#[cfg(target_pointer_width = "16")]
pub type Payload = [u8; 6];
// Both layouts are declared unconditionally so that the layout assertions for *both* are
// compiled on every target; only the alias below is conditional. Otherwise the big-endian
// invariant would never be checked on a little-endian host.
//
// `align(8)` is required, not cosmetic: a pointer field only forces 4-byte (or 2-byte)
// `align(8)` is required, not cosmetic: a pointer field only forces 4-byte
// alignment, but the value is read back as a `u64` to extract the tag, and wasm traps on an
// unaligned 64-bit load with `RuntimeError: operation does not support unaligned accesses`.
// Aligning to the width of the integer view keeps that read legal without adding padding.
......@@ -131,11 +123,7 @@ mod raw_types {
);
}
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_128"
)))]
#[cfg(all(target_pointer_width = "64", not(feature = "atom_size_128")))]
mod raw_types {
pub type RawTaggedValue = usize;
pub type RawTaggedNonZeroValue = std::ptr::NonNull<()>;
......@@ -167,10 +155,7 @@ impl TaggedValue {
pub const fn new_ptr<T>(value: NonNull<T>) -> Self {
// A pointer → pointer cast, which const evaluation permits. See the module docs for why
// the narrow-pointer arm cannot store a bare integer here.
#[cfg(all(
any(target_pointer_width = "32", target_pointer_width = "16"),
not(feature = "atom_size_128")
))]
#[cfg(all(target_pointer_width = "32", not(feature = "atom_size_128")))]
{
#[cfg(target_endian = "little")]
{
......@@ -202,11 +187,7 @@ impl TaggedValue {
}
}
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_128"
)))]
#[cfg(all(target_pointer_width = "64", not(feature = "atom_size_128")))]
{
Self {
value: value.cast(),
......@@ -216,12 +197,7 @@ impl TaggedValue {
#[inline(always)]
pub const fn new_tag(value: NonZeroU8) -> Self {
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_64",
feature = "atom_size_128"
)))]
#[cfg(not(any(target_pointer_width = "32", feature = "atom_size_128")))]
{
// A provenance-free integer → pointer conversion for the pointer representation.
Self {
......@@ -230,12 +206,7 @@ impl TaggedValue {
),
}
}
#[cfg(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_64",
feature = "atom_size_128"
))]
#[cfg(any(target_pointer_width = "32", feature = "atom_size_128"))]
{
// An integer → pointer transmute, which const evaluation permits.
let value = value.get() as RawTaggedValue;
......@@ -250,10 +221,7 @@ impl TaggedValue {
#[inline(always)]
pub fn get_ptr(&self) -> *const c_void {
#[cfg(all(
any(target_pointer_width = "32", target_pointer_width = "16"),
not(feature = "atom_size_128")
))]
#[cfg(all(target_pointer_width = "32", not(feature = "atom_size_128")))]
{
// The tag lives in the low bits of the pointer itself (`DYNAMIC_TAG` sets one), so
// mask it before use — otherwise a dynamic string's `Arc` is dereferenced two bytes
......@@ -265,11 +233,7 @@ impl TaggedValue {
{
(self.value.get() as usize & !(TAG_MASK as usize)) as _
}
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_128"
)))]
#[cfg(not(any(target_pointer_width = "32", feature = "atom_size_128")))]
{
self.value
.as_ptr()
......@@ -281,36 +245,20 @@ impl TaggedValue {
#[inline(always)]
fn get_value(&self) -> RawTaggedValue {
#[cfg(not(any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_64",
feature = "atom_size_128"
)))]
#[cfg(not(any(target_pointer_width = "32", feature = "atom_size_128")))]
{
// Read the pointer representation's address without exposing provenance.
self.value.addr().get()
}
#[cfg(all(
any(target_pointer_width = "32", target_pointer_width = "16"),
not(feature = "atom_size_128")
))]
#[cfg(all(target_pointer_width = "32", not(feature = "atom_size_128")))]
{
// The layout is guaranteed padding-free and 8-aligned above, so reading the whole
// value as an integer is well defined. Again, run time only.
unsafe { std::mem::transmute::<RawTaggedNonZeroValue, RawTaggedValue>(self.value) }
}
#[cfg(all(
any(
target_pointer_width = "32",
target_pointer_width = "16",
feature = "atom_size_64",
feature = "atom_size_128"
),
not(all(
any(target_pointer_width = "32", target_pointer_width = "16"),
not(feature = "atom_size_128")
))
any(target_pointer_width = "32", feature = "atom_size_128"),
not(all(target_pointer_width = "32", not(feature = "atom_size_128")))
))]
{
unsafe {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment