Tobias KoppersGitHubv-work-app[bot] <262237222+v-work-app[bot]@users.noreply.github.com>ClaudeTobias Koppers
9a4e4e7f64 trace-server: shrink_to_fit LazySortedVec after sorting (#93361)
### What?

In `turbopack-trace-server`, call `shrink_to_fit` on the inner storage
of `LazySortedVec<T>` immediately after the one-shot sort that runs the
first time the value is dereferenced.

### Why?

`LazySortedVec` is built as a write-only `SmallVec<[T; 1]>` during trace
ingestion (every `push` resets the inner `Once` so subsequent reads will
resort), and is then "frozen" the first time it's `Deref`'d: the
`Once::call_once` closure runs `sort()` once and the slice is handed out
unchanged for the rest of the program's lifetime.

Because the `SmallVec` grew via repeated `push`es, its capacity is
typically a power-of-two well above its final length. After the first
read, that overhead is dead memory we keep until the trace-server
process exits. For traces with many spans, this adds up across all the
`LazySortedVec`s on each span (e.g. the per-span event lists feeding the
bottom-up / aggregated views), so the resident set of an idle
trace-server is meaningfully larger than necessary.

Since the `Once` guarantees the vector is never mutated again after the
closure runs, the post-sort moment is the natural and only safe place to
release that excess capacity.

### How?

In the `Deref for LazySortedVec<T> where T: Ord` impl in
`turbopack/crates/turbopack-trace-server/src/lazy_sorted_vec.rs`, the
`once.call_once` closure now:

1. Reborrows the inner storage through the existing `UnsafeCell`
pointer.
2. Calls `sort()` (unchanged behavior).
3. Calls `shrink_to_fit()` on the same `SmallVec`.

The existing safety invariant — that the `&mut` reborrow inside
`call_once` is exclusive because `Deref` is the only code path that
touches the inner cell without a `&mut self`, and `Once` guarantees the
closure runs at most once before any reader observes the slice — covers
`shrink_to_fit` for free; it's just another exclusive mutation in the
same scope as the sort. The `Deref` return path is unchanged: callers
still receive a `&[T]` over the now-sorted, now-fitted backing storage.

No public API change. No behavior change for callers. No new
dependencies.

### Verification

- `cargo build -p turbopack-trace-server`
- `cargo clippy -p turbopack-trace-server`
- `cargo fmt`

No new tests: this is a pure memory-footprint optimization on a path
that is already exercised by every existing read of a `LazySortedVec`.

<!-- NEXT_JS_LLM_PR -->

Co-authored-by: v-work-app[bot] <262237222+v-work-app[bot]@users.noreply.github.com>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]>
2026-05-01 07:00:01 +02:00
2025-10-13 10:43:51 +02:00
2026-04-30 10:01:06 +02:00
2026-04-30 23:35:42 +00:00
…
2026-04-30 23:35:42 +00:00
2026-04-30 23:35:42 +00:00
…

Next.js logo

Next.js

Vercel logo NPM version License Join the community on GitHub

Getting Started

Used by some of the world's largest companies, Next.js enables you to create full-stack web applications by extending the latest React features, and integrating powerful Rust-based JavaScript tooling for the fastest builds.

Documentation

Visit https://nextjs.org/docs to view the full documentation.

Community

The Next.js community can be found on GitHub Discussions where you can ask questions, voice ideas, and share your projects with other people.

To chat with other community members you can join the Next.js Discord server.

Do note that our Code of Conduct applies to all Next.js community channels. Users are highly encouraged to read and adhere to it to avoid repercussions.

Contributing

Contributions to Next.js are welcome and highly appreciated. However, before you jump right into it, we would like you to review our Contribution Guidelines to make sure you have a smooth experience contributing to Next.js.

Good First Issues:

We have a list of good first issues that contain bugs that have a relatively limited scope. This is a great place for newcomers and beginners alike to get started, gain experience, and get familiar with our contribution process.


Security

If you believe you have found a security vulnerability in Next.js, we encourage you to responsibly disclose this and NOT open a public issue.

To participate in our Open Source Software Bug Bounty program, please email [email protected]. We will add you to the program and provide further instructions for submitting your report.

S
Description
The React Framework
Readme
2.4 GiB
Languages
JavaScript 55.6%
TypeScript 30%
Rust 12.9%
MDX 0.7%
CSS 0.7%