-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[turbopack] Minimal implementation of local Vcs (#68469)
*This is a migrated PR. This was in the turbo repository before the next.js merge.* **Migrated From:** vercel/turborepo#8780 # Description Local Vcs store task-local values inside of the current task's state. The `SharedReference` holding onto their values is dropped when the task exits. The contents of a local Vc must still use a refcounted `SharedReference`/`triomphe::Arc` because they can be turned into `ReadRef` objects (https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks/struct.ReadRef.html), which require a `'static` lifetime. We can experiment with adding a lifetime to `ReadRef` in a future iteration, but there's little advantage to doing this until we have local tasks. # Limitations This implements *just enough* to create and read local Vcs (with a test!). There are many things that are still unimplemented with `todo!()`. Most notably: - We can't resolve a local `Vc` to a `ResolvedVc`. - There's no way to return or pass a local `Vc` as an argument yet. - For safety, we should only allow construction of local `Vc`s in functions where we know that the return value is a `ResolvedValue`. Grand plan: https://www.notion.so/vercel/Resolved-Vcs-Vc-Lifetimes-Local-Vcs-and-Vc-Refcounts-49d666d3f9594017b5b312b87ddc5bff # Memory Usage - This increases the size of `Vc`/`RawVc` from 96 bits to 128 bits. With clever packing this could (in theory) be solved. However, that it increase the number of machine words (2x 64bit), so it might not have any real impact after alignment happens. - This increase the size of `CurrentTaskState`. I suspect this isn't too bad as there should be a smallish number of tasks active at any given time. **I was not able to measure any change to peak memory/heap usage.** Built using ``` cargo build --release -p next-build-test ``` Ran heaptrack on a single page (`/sink`) in `shadcn/ui` with: ``` cd ~/ui/apps/www heaptrack ~/nextpack/target/release/next-build-test run sequential 1 1 '/sink' ``` And analyzed the results with ``` heaptrack --analyze /home/bgw.linux/ui/apps/www/heaptrack.next-build-test.3066837.zst | tail -20 ``` ### Before ``` total runtime: 130.25s. calls to allocation functions: 48553541 (372786/s) temporary memory allocations: 3863919 (29666/s) peak heap memory consumption: 1.13G peak RSS (including heaptrack overhead): 2.69G total memory leaked: 4.96M suppressed leaks: 7.24K ``` ``` total runtime: 135.48s. calls to allocation functions: 48619554 (358863/s) temporary memory allocations: 3883888 (28667/s) peak heap memory consumption: 1.12G peak RSS (including heaptrack overhead): 2.70G total memory leaked: 4.71M suppressed leaks: 7.24K ``` ### After ``` total runtime: 157.20s. calls to allocation functions: 48509638 (308579/s) temporary memory allocations: 3902883 (24827/s) peak heap memory consumption: 1.13G peak RSS (including heaptrack overhead): 2.70G total memory leaked: 4.86M suppressed leaks: 7.24K ``` ``` total runtime: 130.25s. calls to allocation functions: 48553541 (372786/s) temporary memory allocations: 3863919 (29666/s) peak heap memory consumption: 1.13G peak RSS (including heaptrack overhead): 2.69G total memory leaked: 4.96M suppressed leaks: 7.24K ``` # Testing Instructions ``` cargo nextest r -p turbo-tasks -p turbo-tasks-memory ```
- Loading branch information
1 parent
39c0f29
commit c29f5a8
Showing
8 changed files
with
239 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![feature(arbitrary_self_types)] | ||
|
||
use turbo_tasks::Vc; | ||
use turbo_tasks_testing::{register, run, Registration}; | ||
|
||
static REGISTRATION: Registration = register!(); | ||
|
||
#[turbo_tasks::value] | ||
struct Wrapper(u32); | ||
|
||
#[turbo_tasks::value(transparent)] | ||
struct TransparentWrapper(u32); | ||
|
||
#[tokio::test] | ||
async fn store_and_read() { | ||
run(®ISTRATION, async { | ||
let a: Vc<u32> = Vc::local_cell(42); | ||
assert_eq!(*a.await.unwrap(), 42); | ||
|
||
let b = Wrapper(42).local_cell(); | ||
assert_eq!((*b.await.unwrap()).0, 42); | ||
|
||
let c = TransparentWrapper(42).local_cell(); | ||
assert_eq!(*c.await.unwrap(), 42); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn store_and_read_generic() { | ||
run(®ISTRATION, async { | ||
// `Vc<Vec<Vc<T>>>` is stored as `Vc<Vec<Vc<()>>>` and requires special | ||
// transmute handling | ||
let cells: Vc<Vec<Vc<u32>>> = | ||
Vc::local_cell(vec![Vc::local_cell(1), Vc::local_cell(2), Vc::cell(3)]); | ||
|
||
let mut output = Vec::new(); | ||
for el in cells.await.unwrap() { | ||
output.push(*el.await.unwrap()); | ||
} | ||
|
||
assert_eq!(output, vec![1, 2, 3]); | ||
}) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.