refactor(turbopack): drop the generic on `new_atom`, and stop it copying an owned string (#98520)
### What? Follow-up to the review comments on #98497. `new_atom` loses its generic parameter and takes a `Cow<'_, str>`, which as a side effect stops it copying a string it was handed ownership of. `FileSystemPath`'s `get_relative_request_to` stops hand-rolling a conversion the standard `From` impl already does, and the two tests added for it in `path.rs` are replaced by one that covers what that layer actually adds. ### Why? @bgw noted that `get_relative_request_to` was matching on the `Cow` it gets back only to call `.into()` in both arms, and wondered whether `impl From<Cow<'_, str>> for RcStr` was missing a fast path for the owned case. It was not missing one, and it never needed one — but an owned string *was* being copied, for a different reason. `new_atom` was generic over `T: AsRef<str> + Into<String>` and opened with `let text = text.as_ref();`, which shadows the owned value. From that point on only a `&str` was in scope, so building the `Box<str>` allocated a fresh buffer and copied into it, dropping the `String` that a `Cow::Owned` had just handed over. So this is not a fast path being added. It is an accidental pessimisation being removed: the generic plus the `as_ref()` were exactly what stopped the standard conversion from doing the right thing on its own. Whether that copy ever mattered in practice is a separate question, and one this does not attempt to answer. The other two comments were about the tests: they re-ran the case table that already lives in `turbo-unix-path`, which is where the path computation itself is implemented and tested — "60 LOC to test a 3 LOC branch". ### How? `new_atom` takes a `Cow<'_, str>`. All five of its callers already passed one, so nothing else changes, and ownership now survives to where the `Box<str>` is built — `Box<str>`'s own `From<Cow<'_, str>>` moves an owned buffer in and copies only a borrowed one. The inline-atom path is untouched, since it reads through a `&str` either way and does not allocate. Contents-based assertions cannot distinguish a moved buffer from a copied one, so the test compares the data pointer before and after the conversion. It uses a string longer than `MAX_INLINE_LEN`, because an inline atom stores its bytes in the tagged value by design and could never preserve a pointer, and it asserts the atom really is dynamic so it cannot pass vacuously if that boundary ever moves. It fails on `canary` today. `get_relative_request_to` collapses to a single `.into()`. Its sibling `get_relative_path_to` keeps its match on purpose: one arm uses `std::ptr::eq` to hand back a clone of the `RcStr` the path already holds, which the blanket conversion cannot do. That arm predates #98497 and is an optimisation rather than the redundancy that was flagged. For the tests, `turbo-unix-path` remains the place where the relative path computation is covered, untouched. What is left here is only what this layer adds on top: that each method reaches for the form it names, and that neither returns a path across filesystems. Keeping the first of those is a judgement call — a method delegating to the wrong free function is precisely the bug #98497 existed to fix, and it is invisible to the lower-level table — so it is asserted with one descendant pair rather than a table. Happy to drop it entirely if you would rather. To check that the smaller test still earns its place, `get_relative_request_to` was temporarily pointed at the plain-path function; the test fails as it should. <!-- NEXT_JS_LLM --> <!-- fleet 3acc7e63-52a8-4472-89cd-b1bbb8ad59fa --> --------- Co-authored-by:vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment