Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecmascript): support dynamic tenary operator jsvalue #6452

Merged
merged 2 commits into from
Nov 17, 2023
Merged

Conversation

kwonoj
Copy link
Contributor

@kwonoj kwonoj commented Nov 14, 2023

Description

context: https://vercel.slack.com/archives/C03EWR7LGEN/p1699990767027949

Currently, if the input source have a non-statically analyzable tenary operator

await import(
              process.env.NEXT_RUNTIME === 'edge'
                ? 'next/dist/compiled/@vercel/og/index.edge.js'
                : 'next/dist/compiled/@vercel/og/index.node.js'
)

it becomes to the output like

(await __turbopack_require__("[project]/packages/next/dist/compiled/@vercel/og/index.edge.js [rsc] (ecmascript, loader)")(__turbopack_import__))

that takes first value of tenary regardless of the condition. In the analyze phase if tenary is dynamic it retuns JsValue::Alternatives, and it takes the first available value immediately.

PR changes its behavior by having new type of JsValue for tenary - if condExpr is not statically analyzable. I tried to use existing JsValue but that doesn't seem to work easily out of the box. However I may miss some obvious so PR might be refactored more simple way instead.

Closes PACK-1963

Copy link

vercel bot commented Nov 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-nonmonorepo 🔄 Building (Inspect) Visit Preview 💬 Add feedback Nov 17, 2023 0:21am
examples-vite-web 🔄 Building (Inspect) Visit Preview 💬 Add feedback Nov 17, 2023 0:21am
9 Ignored Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-cra-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am
turbo-site ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 0:21am

Copy link
Contributor

github-actions bot commented Nov 14, 2023

✅ This change can build next-swc

Copy link
Contributor

github-actions bot commented Nov 15, 2023

🟢 CI successful 🟢

Thanks

Copy link
Contributor

Linux Benchmark for e99af59

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 24.83ms ± 0.72ms 24.15ms ± 0.99ms -2.71%
bench_hmr_to_eval/Turbopack CSR/1000 modules 23.44ms ± 0.86ms 23.85ms ± 0.72ms +1.74%
bench_startup/Turbopack CSR/1000 modules 1255.24ms ± 5.50ms 1239.51ms ± 19.46ms -1.25%

Copy link
Contributor

github-actions bot commented Nov 15, 2023

⚠️ Turbopack Benchmark CI failed ⚠️

The following steps have failed in CI:

  • Turbopack Rust benchmark tests (linux)
  • Turbopack Rust benchmark tests (mac/win, non-blocking)

See workflow summary for details

if test.is_truthy() == Some(true) {
*value = take(cons);
true
} else if test.is_falsy() == Some(true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just test.is_truty() == Some(false), right? We can this into a branch above and save some cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, this is borrowing existing references from elsewhere (I recall it's binary operation or something)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this can be a match on is_truty

@@ -1224,6 +1246,12 @@ impl JsValue {
op.joiner(),
b.explain_internal_inner(hints, indent_depth, depth, unknown_depth),
),
JsValue::Tenary(_, test, cons, alt) => format!(
"({}?{}:{})",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more spacing here would be nice

@@ -1,5 +1,7 @@
const a = import(true ? "a" : "b")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing your tenary support and should evaluate to a

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this already works before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tenary passes evaluation at

3c2d29c#diff-2c7634641094d97670b756458b73ab7bc5f6fba2da97471427a6c95795b8562dR429, while dynamic tenary evaluation doesn't seem to pass, so it goes to jsvalue:alternatives.

@@ -3,10 +3,23 @@
0 -> 2 call = import*0*("a")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already seem to work?

Copy link
Contributor

Linux Benchmark for ce0ac01

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 21.75ms ± 0.86ms 21.91ms ± 0.67ms +0.72%
bench_hmr_to_eval/Turbopack CSR/1000 modules 21.23ms ± 0.84ms 21.22ms ± 0.86ms -0.05%
bench_startup/Turbopack CSR/1000 modules 1165.22ms ± 4.04ms 1142.94ms ± 11.23ms -1.91%

Copy link
Contributor

Linux Benchmark for 4ef8aa7

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 23.21ms ± 0.98ms 23.21ms ± 1.01ms +0.02%
bench_hmr_to_eval/Turbopack CSR/1000 modules 22.62ms ± 0.95ms 22.62ms ± 0.97ms +0.01%
bench_startup/Turbopack CSR/1000 modules 1160.77ms ± 5.82ms 1146.10ms ± 10.45ms -1.26%

Copy link
Contributor

Linux Benchmark for 94f2585

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 22.20ms ± 0.94ms 21.92ms ± 0.86ms -1.28%
bench_hmr_to_eval/Turbopack CSR/1000 modules 21.66ms ± 0.78ms 21.54ms ± 0.89ms -0.56%
bench_startup/Turbopack CSR/1000 modules 1155.48ms ± 6.75ms 1133.01ms ± 13.49ms -1.94%

Copy link
Contributor

Linux Benchmark for fb3cd66

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 23.52ms ± 0.92ms 23.18ms ± 0.93ms -1.44%
bench_hmr_to_eval/Turbopack CSR/1000 modules 22.52ms ± 0.99ms 23.19ms ± 1.12ms +2.95%
bench_startup/Turbopack CSR/1000 modules 1157.48ms ± 6.38ms 1170.01ms ± 24.62ms +1.08%

@kwonoj kwonoj merged commit 2cf42a4 into main Nov 17, 2023
47 of 49 checks passed
@kwonoj kwonoj deleted the jsvalue-tenary branch November 17, 2023 00:44
sokra added a commit that referenced this pull request Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants